blob: 3b26322530ca4c44eca94d6fef9305a613aff105 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package gojay
import (
"io"
"sync"
)
var encPool = sync.Pool{
New: func() interface{} {
return NewEncoder(nil)
},
}
var streamEncPool = sync.Pool{
New: func() interface{} {
return Stream.NewEncoder(nil)
},
}
func init() {
for i := 0; i < 32; i++ {
encPool.Put(NewEncoder(nil))
}
for i := 0; i < 32; i++ {
streamEncPool.Put(Stream.NewEncoder(nil))
}
}
// NewEncoder returns a new encoder or borrows one from the pool
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: w}
}
// BorrowEncoder borrows an Encoder from the pool.
func BorrowEncoder(w io.Writer) *Encoder {
enc := encPool.Get().(*Encoder)
enc.w = w
enc.buf = enc.buf[:0]
enc.isPooled = 0
enc.err = nil
enc.hasKeys = false
enc.keys = nil
return enc
}
// Release sends back a Encoder to the pool.
func (enc *Encoder) Release() {
enc.isPooled = 1
encPool.Put(enc)
}
|