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
|
2017-06-18 16:21:54 +02:00
|
|
|
stream.Error = nil
|
2017-11-08 05:41:45 +02:00
|
|
|
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) {
|
2017-06-18 16:21:54 +02:00
|
|
|
iter.Error = nil
|
2017-11-08 05:41:45 +02:00
|
|
|
iter.Attachment = nil
|
2018-02-22 04:29:29 +02:00
|
|
|
cfg.iteratorPool.Put(iter)
|
2017-06-17 08:36:05 +02:00
|
|
|
}
|