File tree 4 files changed +23
-32
lines changed
4 files changed +23
-32
lines changed Original file line number Diff line number Diff line change 5
5
"io"
6
6
"unsafe"
7
7
"github.com/v2pro/plz/reflect2"
8
+ "sync"
8
9
)
9
10
10
11
// Config customize how the API should behave.
@@ -66,8 +67,16 @@ func (cfg Config) Froze() API {
66
67
objectFieldMustBeSimpleString : cfg .ObjectFieldMustBeSimpleString ,
67
68
onlyTaggedField : cfg .OnlyTaggedField ,
68
69
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
+ },
71
80
}
72
81
api .initCache ()
73
82
encoderExtension := EncoderExtension {}
Original file line number Diff line number Diff line change @@ -16,8 +16,8 @@ type frozenConfig struct {
16
16
decoderCache sync.Map
17
17
encoderCache sync.Map
18
18
extensions []Extension
19
- streamPool chan * Stream
20
- iteratorPool chan * Iterator
19
+ streamPool * sync. Pool
20
+ iteratorPool * sync. Pool
21
21
}
22
22
23
23
func (cfg * frozenConfig ) initCache () {
Original file line number Diff line number Diff line change @@ -17,8 +17,8 @@ type frozenConfig struct {
17
17
decoderCache map [uintptr ]ValDecoder
18
18
encoderCache map [uintptr ]ValEncoder
19
19
extensions []Extension
20
- streamPool chan * Stream
21
- iteratorPool chan * Iterator
20
+ streamPool * sync. Pool
21
+ iteratorPool * sync. Pool
22
22
}
23
23
24
24
func (cfg * frozenConfig ) initCache () {
Original file line number Diff line number Diff line change @@ -17,43 +17,25 @@ type StreamPool interface {
17
17
}
18
18
19
19
func (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
27
23
}
28
24
29
25
func (cfg * frozenConfig ) ReturnStream (stream * Stream ) {
30
26
stream .Error = nil
31
27
stream .Attachment = nil
32
- select {
33
- case cfg .streamPool <- stream :
34
- return
35
- default :
36
- return
37
- }
28
+ cfg .streamPool .Put (stream )
38
29
}
39
30
40
31
func (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
48
35
}
49
36
50
37
func (cfg * frozenConfig ) ReturnIterator (iter * Iterator ) {
51
38
iter .Error = nil
52
39
iter .Attachment = nil
53
- select {
54
- case cfg .iteratorPool <- iter :
55
- return
56
- default :
57
- return
58
- }
40
+ cfg .iteratorPool .Put (iter )
59
41
}
You can’t perform that action at this time.
0 commit comments