1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-20 11:28:49 +02:00

#57 reuse stream and iterator

This commit is contained in:
Tao Wen 2017-06-17 14:23:02 +08:00
parent 3d5f6d3a4a
commit 17bd91fd71

View File

@ -23,6 +23,8 @@ type frozenConfig struct {
decoderCache unsafe.Pointer decoderCache unsafe.Pointer
encoderCache unsafe.Pointer encoderCache unsafe.Pointer
extensions []ExtensionFunc extensions []ExtensionFunc
streamPool chan *Stream
iteratorPool chan *Iterator
} }
var ConfigDefault = Config{}.Froze() var ConfigDefault = Config{}.Froze()
@ -41,6 +43,8 @@ func (cfg Config) Froze() *frozenConfig {
frozenConfig := &frozenConfig{ frozenConfig := &frozenConfig{
sortMapKeys: cfg.SortMapKeys, sortMapKeys: cfg.SortMapKeys,
indentionStep: cfg.IndentionStep, indentionStep: cfg.IndentionStep,
streamPool: make(chan *Stream, 16),
iteratorPool: make(chan *Iterator, 16),
} }
atomic.StorePointer(&frozenConfig.decoderCache, unsafe.Pointer(&map[string]Decoder{})) atomic.StorePointer(&frozenConfig.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
atomic.StorePointer(&frozenConfig.encoderCache, unsafe.Pointer(&map[string]Encoder{})) atomic.StorePointer(&frozenConfig.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
@ -153,7 +157,8 @@ func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
} }
func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) { func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
stream := NewStream(cfg, nil, 256) stream := cfg.borrowStream()
defer cfg.returnStream(stream)
stream.WriteVal(v) stream.WriteVal(v)
if stream.Error != nil { if stream.Error != nil {
return nil, stream.Error return nil, stream.Error
@ -164,7 +169,8 @@ func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error { func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
data := []byte(str) data := []byte(str)
data = data[:lastNotSpacePos(data)] data = data[:lastNotSpacePos(data)]
iter := ParseBytes(cfg, data) iter := cfg.borrowIterator(data)
defer cfg.returnIterator(iter)
iter.ReadVal(v) iter.ReadVal(v)
if iter.head == iter.tail { if iter.head == iter.tail {
iter.loadMore() iter.loadMore()
@ -178,20 +184,11 @@ func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
return iter.Error return iter.Error
} }
func (cfg *frozenConfig) NewEncoder(writer io.Writer) *AdaptedEncoder {
stream := NewStream(cfg, writer, 512)
return &AdaptedEncoder{stream}
}
func (cfg *frozenConfig) NewDecoder(reader io.Reader) *AdaptedDecoder {
iter := Parse(cfg, reader, 512)
return &AdaptedDecoder{iter}
}
func (cfg *frozenConfig) UnmarshalAnyFromString(str string) (Any, error) { func (cfg *frozenConfig) UnmarshalAnyFromString(str string) (Any, error) {
data := []byte(str) data := []byte(str)
data = data[:lastNotSpacePos(data)] data = data[:lastNotSpacePos(data)]
iter := ParseBytes(cfg, data) iter := cfg.borrowIterator(data)
defer cfg.returnIterator(iter)
any := iter.ReadAny() any := iter.ReadAny()
if iter.head == iter.tail { if iter.head == iter.tail {
iter.loadMore() iter.loadMore()
@ -207,7 +204,8 @@ func (cfg *frozenConfig) UnmarshalAnyFromString(str string) (Any, error) {
func (cfg *frozenConfig) UnmarshalAny(data []byte) (Any, error) { func (cfg *frozenConfig) UnmarshalAny(data []byte) (Any, error) {
data = data[:lastNotSpacePos(data)] data = data[:lastNotSpacePos(data)]
iter := ParseBytes(cfg, data) iter := cfg.borrowIterator(data)
defer cfg.returnIterator(iter)
any := iter.ReadAny() any := iter.ReadAny()
if iter.head == iter.tail { if iter.head == iter.tail {
iter.loadMore() iter.loadMore()
@ -223,7 +221,8 @@ func (cfg *frozenConfig) UnmarshalAny(data []byte) (Any, error) {
func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error { func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
data = data[:lastNotSpacePos(data)] data = data[:lastNotSpacePos(data)]
iter := ParseBytes(cfg, data) iter := cfg.borrowIterator(data)
defer cfg.returnIterator(iter)
typ := reflect.TypeOf(v) typ := reflect.TypeOf(v)
if typ.Kind() != reflect.Ptr { if typ.Kind() != reflect.Ptr {
// return non-pointer error // return non-pointer error
@ -241,3 +240,13 @@ func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
} }
return iter.Error return iter.Error
} }
func (cfg *frozenConfig) NewEncoder(writer io.Writer) *AdaptedEncoder {
stream := NewStream(cfg, writer, 512)
return &AdaptedEncoder{stream}
}
func (cfg *frozenConfig) NewDecoder(reader io.Reader) *AdaptedDecoder {
iter := Parse(cfg, reader, 512)
return &AdaptedDecoder{iter}
}