| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package json |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"io" |
| 10 |
"math/bits" |
| 11 |
"sort" |
| 12 |
"sync" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
var ( |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
bufferedEncoderPool = &sync.Pool{New: func() any { return new(Encoder) }} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
streamingEncoderPool = &sync.Pool{New: func() any { return new(Encoder) }} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
bytesBufferEncoderPool = &sync.Pool{New: func() any { return new(Encoder) }} |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
type bufferStatistics struct { |
| 39 |
strikes int |
| 40 |
prevLen int |
| 41 |
} |
| 42 |
|
| 43 |
func getBufferedEncoder(o EncodeOptions) *Encoder { |
| 44 |
e := bufferedEncoderPool.Get().(*Encoder) |
| 45 |
if e.buf == nil { |
| 46 |
s. |
| 47 |
|
| 48 |
|
| 49 |
n := 1 << bits.Len(uint(e.bufStats.prevLen|63)) |
| 50 |
e.buf = make([]byte, 0, n) |
| 51 |
} |
| 52 |
e.reset(e.buf[:0], nil, o) |
| 53 |
return e |
| 54 |
} |
| 55 |
func putBufferedEncoder(e *Encoder) { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
%. |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
switch { |
| 71 |
case cap(e.buf) <= 4<<10: |
| 72 |
e.bufStats.strikes = 0 |
| 73 |
case cap(e.buf)/4 <= len(e.buf): |
| 74 |
e.bufStats.strikes = 0 |
| 75 |
case e.bufStats.strikes < 4: |
| 76 |
e.bufStats.strikes++ |
| 77 |
default: |
| 78 |
e.bufStats.strikes = 0 |
| 79 |
e.bufStats.prevLen = len(e.buf) |
| 80 |
e.buf = nil |
| 81 |
} |
| 82 |
bufferedEncoderPool.Put(e) |
| 83 |
} |
| 84 |
|
| 85 |
func getStreamingEncoder(w io.Writer, o EncodeOptions) *Encoder { |
| 86 |
if _, ok := w.(*bytes.Buffer); ok { |
| 87 |
e := bytesBufferEncoderPool.Get().(*Encoder) |
| 88 |
e.reset(nil, w, o) |
| 89 |
return e |
| 90 |
} else { |
| 91 |
e := streamingEncoderPool.Get().(*Encoder) |
| 92 |
e.reset(e.buf[:0], w, o) |
| 93 |
return e |
| 94 |
} |
| 95 |
} |
| 96 |
func putStreamingEncoder(e *Encoder) { |
| 97 |
if _, ok := e.wr.(*bytes.Buffer); ok { |
| 98 |
bytesBufferEncoderPool.Put(e) |
| 99 |
} else { |
| 100 |
if cap(e.buf) > 64<<10 { |
| 101 |
e.buf = nil |
| 102 |
} |
| 103 |
streamingEncoderPool.Put(e) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
var ( |
| 108 |
|
| 109 |
bufferedDecoderPool = &sync.Pool{New: func() any { return new(Decoder) }} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
streamingDecoderPool = &sync.Pool{New: func() any { return new(Decoder) }} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
bytesBufferDecoderPool = bufferedDecoderPool |
| 120 |
) |
| 121 |
|
| 122 |
func getBufferedDecoder(b []byte, o DecodeOptions) *Decoder { |
| 123 |
d := bufferedDecoderPool.Get().(*Decoder) |
| 124 |
d.reset(b, nil, o) |
| 125 |
return d |
| 126 |
} |
| 127 |
func putBufferedDecoder(d *Decoder) { |
| 128 |
bufferedDecoderPool.Put(d) |
| 129 |
} |
| 130 |
|
| 131 |
func getStreamingDecoder(r io.Reader, o DecodeOptions) *Decoder { |
| 132 |
if _, ok := r.(*bytes.Buffer); ok { |
| 133 |
d := bytesBufferDecoderPool.Get().(*Decoder) |
| 134 |
d.reset(nil, r, o) |
| 135 |
return d |
| 136 |
} else { |
| 137 |
d := streamingDecoderPool.Get().(*Decoder) |
| 138 |
d.reset(d.buf[:0], r, o) |
| 139 |
return d |
| 140 |
} |
| 141 |
} |
| 142 |
func putStreamingDecoder(d *Decoder) { |
| 143 |
if _, ok := d.rd.(*bytes.Buffer); ok { |
| 144 |
bytesBufferDecoderPool.Put(d) |
| 145 |
} else { |
| 146 |
if cap(d.buf) > 64<<10 { |
| 147 |
d.buf = nil |
| 148 |
} |
| 149 |
streamingDecoderPool.Put(d) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
var stringsPools = &sync.Pool{New: func() any { return new(stringSlice) }} |
| 154 |
|
| 155 |
type stringSlice []string |
| 156 |
|
| 157 |
|
| 158 |
func getStrings(n int) *stringSlice { |
| 159 |
s := stringsPools.Get().(*stringSlice) |
| 160 |
if cap(*s) < n { |
| 161 |
*s = make([]string, n) |
| 162 |
} |
| 163 |
*s = (*s)[:n] |
| 164 |
return s |
| 165 |
} |
| 166 |
|
| 167 |
func putStrings(s *stringSlice) { |
| 168 |
if cap(*s) > 1<<10 { |
| 169 |
*s = nil |
| 170 |
} |
| 171 |
stringsPools.Put(s) |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
func (ss *stringSlice) Sort() { |
| 176 |
|
| 177 |
sort.Sort(ss) |
| 178 |
} |
| 179 |
|
| 180 |
func (ss *stringSlice) Len() int { return len(*ss) } |
| 181 |
func (ss *stringSlice) Less(i, j int) bool { return lessUTF16((*ss)[i], (*ss)[j]) } |
| 182 |
func (ss *stringSlice) Swap(i, j int) { (*ss)[i], (*ss)[j] = (*ss)[j], (*ss)[i] } |