mirror of
https://github.com/json-iterator/go.git
synced 2025-05-19 21:53:05 +02:00
use sync.Pool
This commit is contained in:
parent
d346ea6e55
commit
a3fdd37b9a
13
config.go
13
config.go
@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
"github.com/v2pro/plz/reflect2"
|
"github.com/v2pro/plz/reflect2"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config customize how the API should behave.
|
// Config customize how the API should behave.
|
||||||
@ -66,8 +67,16 @@ func (cfg Config) Froze() API {
|
|||||||
objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
|
objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
|
||||||
onlyTaggedField: cfg.OnlyTaggedField,
|
onlyTaggedField: cfg.OnlyTaggedField,
|
||||||
disallowUnknownFields: cfg.DisallowUnknownFields,
|
disallowUnknownFields: cfg.DisallowUnknownFields,
|
||||||
streamPool: make(chan *Stream, 16),
|
}
|
||||||
iteratorPool: make(chan *Iterator, 16),
|
api.streamPool = &sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return NewStream(api, nil, 512)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
api.iteratorPool = &sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return NewIterator(api)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
api.initCache()
|
api.initCache()
|
||||||
encoderExtension := EncoderExtension{}
|
encoderExtension := EncoderExtension{}
|
||||||
|
@ -16,8 +16,8 @@ type frozenConfig struct {
|
|||||||
decoderCache sync.Map
|
decoderCache sync.Map
|
||||||
encoderCache sync.Map
|
encoderCache sync.Map
|
||||||
extensions []Extension
|
extensions []Extension
|
||||||
streamPool chan *Stream
|
streamPool *sync.Pool
|
||||||
iteratorPool chan *Iterator
|
iteratorPool *sync.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *frozenConfig) initCache() {
|
func (cfg *frozenConfig) initCache() {
|
||||||
|
@ -17,8 +17,8 @@ type frozenConfig struct {
|
|||||||
decoderCache map[uintptr]ValDecoder
|
decoderCache map[uintptr]ValDecoder
|
||||||
encoderCache map[uintptr]ValEncoder
|
encoderCache map[uintptr]ValEncoder
|
||||||
extensions []Extension
|
extensions []Extension
|
||||||
streamPool chan *Stream
|
streamPool *sync.Pool
|
||||||
iteratorPool chan *Iterator
|
iteratorPool *sync.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *frozenConfig) initCache() {
|
func (cfg *frozenConfig) initCache() {
|
||||||
|
34
pool.go
34
pool.go
@ -17,43 +17,25 @@ type StreamPool interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *frozenConfig) BorrowStream(writer io.Writer) *Stream {
|
func (cfg *frozenConfig) BorrowStream(writer io.Writer) *Stream {
|
||||||
select {
|
stream := cfg.streamPool.Get().(*Stream)
|
||||||
case stream := <-cfg.streamPool:
|
stream.Reset(writer)
|
||||||
stream.Reset(writer)
|
return stream
|
||||||
return stream
|
|
||||||
default:
|
|
||||||
return NewStream(cfg, writer, 512)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *frozenConfig) ReturnStream(stream *Stream) {
|
func (cfg *frozenConfig) ReturnStream(stream *Stream) {
|
||||||
stream.Error = nil
|
stream.Error = nil
|
||||||
stream.Attachment = nil
|
stream.Attachment = nil
|
||||||
select {
|
cfg.streamPool.Put(stream)
|
||||||
case cfg.streamPool <- stream:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *frozenConfig) BorrowIterator(data []byte) *Iterator {
|
func (cfg *frozenConfig) BorrowIterator(data []byte) *Iterator {
|
||||||
select {
|
iter := cfg.iteratorPool.Get().(*Iterator)
|
||||||
case iter := <-cfg.iteratorPool:
|
iter.ResetBytes(data)
|
||||||
iter.ResetBytes(data)
|
return iter
|
||||||
return iter
|
|
||||||
default:
|
|
||||||
return ParseBytes(cfg, data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
|
func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
|
||||||
iter.Error = nil
|
iter.Error = nil
|
||||||
iter.Attachment = nil
|
iter.Attachment = nil
|
||||||
select {
|
cfg.iteratorPool.Put(iter)
|
||||||
case cfg.iteratorPool <- iter:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user