1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00
json-iterator/pool.go

43 lines
956 B
Go
Raw Permalink Normal View History

2017-06-17 08:36:05 +02:00
package jsoniter
2017-06-17 16:42:11 +02:00
import (
"io"
)
2017-06-17 11:14:34 +02:00
2017-07-09 10:09:23 +02: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 11:14:34 +02:00
func (cfg *frozenConfig) BorrowStream(writer io.Writer) *Stream {
2018-02-22 04:29:29 +02:00
stream := cfg.streamPool.Get().(*Stream)
stream.Reset(writer)
return stream
2017-06-17 08:36:05 +02:00
}
2017-06-17 11:14:34 +02:00
func (cfg *frozenConfig) ReturnStream(stream *Stream) {
2018-04-07 13:40:08 +02:00
stream.out = nil
stream.Error = nil
stream.Attachment = nil
2018-02-22 04:29:29 +02:00
cfg.streamPool.Put(stream)
2017-06-17 08:36:05 +02:00
}
2017-06-17 11:14:34 +02:00
func (cfg *frozenConfig) BorrowIterator(data []byte) *Iterator {
2018-02-22 04:29:29 +02:00
iter := cfg.iteratorPool.Get().(*Iterator)
iter.ResetBytes(data)
return iter
2017-06-17 08:36:05 +02:00
}
2017-06-17 11:14:34 +02:00
func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
iter.Error = nil
iter.Attachment = nil
2018-02-22 04:29:29 +02:00
cfg.iteratorPool.Put(iter)
2017-06-17 08:36:05 +02:00
}