1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-23 11:37:32 +02:00
json-iterator/pool.go

43 lines
956 B
Go
Raw Normal View History

2017-06-17 14:36:05 +08:00
package jsoniter
2017-06-17 22:42:11 +08:00
import (
"io"
)
2017-06-17 17:14:34 +08:00
2017-07-09 16:09:23 +08:00
// IteratorPool a thread safe pool of iterators with same configuration
type IteratorPool interface {
BorrowIterator(data []byte) *Iterator
ReturnIterator(iter *Iterator)
}
// StreamPool a thread safe pool of streams with same configuration
type StreamPool interface {
BorrowStream(writer io.Writer) *Stream
ReturnStream(stream *Stream)
}
2017-06-17 17:14:34 +08:00
func (cfg *frozenConfig) BorrowStream(writer io.Writer) *Stream {
2018-02-22 10:29:29 +08:00
stream := cfg.streamPool.Get().(*Stream)
stream.Reset(writer)
return stream
2017-06-17 14:36:05 +08:00
}
2017-06-17 17:14:34 +08:00
func (cfg *frozenConfig) ReturnStream(stream *Stream) {
2018-04-07 21:40:08 +10:00
stream.out = nil
stream.Error = nil
stream.Attachment = nil
2018-02-22 10:29:29 +08:00
cfg.streamPool.Put(stream)
2017-06-17 14:36:05 +08:00
}
2017-06-17 17:14:34 +08:00
func (cfg *frozenConfig) BorrowIterator(data []byte) *Iterator {
2018-02-22 10:29:29 +08:00
iter := cfg.iteratorPool.Get().(*Iterator)
iter.ResetBytes(data)
return iter
2017-06-17 14:36:05 +08:00
}
2017-06-17 17:14:34 +08:00
func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
iter.Error = nil
iter.Attachment = nil
2018-02-22 10:29:29 +08:00
cfg.iteratorPool.Put(iter)
2017-06-17 14:36:05 +08:00
}