pool.go 938 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package jsoniter
  2. import (
  3. "io"
  4. )
  5. // IteratorPool a thread safe pool of iterators with same configuration
  6. type IteratorPool interface {
  7. BorrowIterator(data []byte) *Iterator
  8. ReturnIterator(iter *Iterator)
  9. }
  10. // StreamPool a thread safe pool of streams with same configuration
  11. type StreamPool interface {
  12. BorrowStream(writer io.Writer) *Stream
  13. ReturnStream(stream *Stream)
  14. }
  15. func (cfg *frozenConfig) BorrowStream(writer io.Writer) *Stream {
  16. stream := cfg.streamPool.Get().(*Stream)
  17. stream.Reset(writer)
  18. return stream
  19. }
  20. func (cfg *frozenConfig) ReturnStream(stream *Stream) {
  21. stream.Error = nil
  22. stream.Attachment = nil
  23. cfg.streamPool.Put(stream)
  24. }
  25. func (cfg *frozenConfig) BorrowIterator(data []byte) *Iterator {
  26. iter := cfg.iteratorPool.Get().(*Iterator)
  27. iter.ResetBytes(data)
  28. return iter
  29. }
  30. func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
  31. iter.Error = nil
  32. iter.Attachment = nil
  33. cfg.iteratorPool.Put(iter)
  34. }