File tree Expand file tree Collapse file tree 4 files changed +23
-32
lines changed Expand file tree Collapse file tree 4 files changed +23
-32
lines changed Original file line number Diff line number Diff line change 55 "io"
66 "unsafe"
77 "github.com/v2pro/plz/reflect2"
8+ "sync"
89)
910
1011// Config customize how the API should behave.
@@ -66,8 +67,16 @@ func (cfg Config) Froze() API {
6667 objectFieldMustBeSimpleString : cfg .ObjectFieldMustBeSimpleString ,
6768 onlyTaggedField : cfg .OnlyTaggedField ,
6869 disallowUnknownFields : cfg .DisallowUnknownFields ,
69- streamPool : make (chan * Stream , 16 ),
70- iteratorPool : make (chan * Iterator , 16 ),
70+ }
71+ api .streamPool = & sync.Pool {
72+ New : func () interface {} {
73+ return NewStream (api , nil , 512 )
74+ },
75+ }
76+ api .iteratorPool = & sync.Pool {
77+ New : func () interface {} {
78+ return NewIterator (api )
79+ },
7180 }
7281 api .initCache ()
7382 encoderExtension := EncoderExtension {}
Original file line number Diff line number Diff line change @@ -16,8 +16,8 @@ type frozenConfig struct {
1616 decoderCache sync.Map
1717 encoderCache sync.Map
1818 extensions []Extension
19- streamPool chan * Stream
20- iteratorPool chan * Iterator
19+ streamPool * sync. Pool
20+ iteratorPool * sync. Pool
2121}
2222
2323func (cfg * frozenConfig ) initCache () {
Original file line number Diff line number Diff line change @@ -17,8 +17,8 @@ type frozenConfig struct {
1717 decoderCache map [uintptr ]ValDecoder
1818 encoderCache map [uintptr ]ValEncoder
1919 extensions []Extension
20- streamPool chan * Stream
21- iteratorPool chan * Iterator
20+ streamPool * sync. Pool
21+ iteratorPool * sync. Pool
2222}
2323
2424func (cfg * frozenConfig ) initCache () {
Original file line number Diff line number Diff line change @@ -17,43 +17,25 @@ type StreamPool interface {
1717}
1818
1919func (cfg * frozenConfig ) BorrowStream (writer io.Writer ) * Stream {
20- select {
21- case stream := <- cfg .streamPool :
22- stream .Reset (writer )
23- return stream
24- default :
25- return NewStream (cfg , writer , 512 )
26- }
20+ stream := cfg .streamPool .Get ().(* Stream )
21+ stream .Reset (writer )
22+ return stream
2723}
2824
2925func (cfg * frozenConfig ) ReturnStream (stream * Stream ) {
3026 stream .Error = nil
3127 stream .Attachment = nil
32- select {
33- case cfg .streamPool <- stream :
34- return
35- default :
36- return
37- }
28+ cfg .streamPool .Put (stream )
3829}
3930
4031func (cfg * frozenConfig ) BorrowIterator (data []byte ) * Iterator {
41- select {
42- case iter := <- cfg .iteratorPool :
43- iter .ResetBytes (data )
44- return iter
45- default :
46- return ParseBytes (cfg , data )
47- }
32+ iter := cfg .iteratorPool .Get ().(* Iterator )
33+ iter .ResetBytes (data )
34+ return iter
4835}
4936
5037func (cfg * frozenConfig ) ReturnIterator (iter * Iterator ) {
5138 iter .Error = nil
5239 iter .Attachment = nil
53- select {
54- case cfg .iteratorPool <- iter :
55- return
56- default :
57- return
58- }
40+ cfg .iteratorPool .Put (iter )
5941}
You can’t perform that action at this time.
0 commit comments