Skip to content

Commit a3fdd37

Browse files
committed
use sync.Pool
1 parent d346ea6 commit a3fdd37

File tree

4 files changed

+23
-32
lines changed

4 files changed

+23
-32
lines changed

config.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
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{}

config_with_sync_map.go

+2-2
Original file line numberDiff line numberDiff 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

2323
func (cfg *frozenConfig) initCache() {

config_without_sync_map.go

+2-2
Original file line numberDiff line numberDiff 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

2424
func (cfg *frozenConfig) initCache() {

pool.go

+8-26
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,25 @@ type StreamPool interface {
1717
}
1818

1919
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
2723
}
2824

2925
func (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

4031
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
4835
}
4936

5037
func (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
}

0 commit comments

Comments
 (0)