另客网go项目公用的代码库

pool.go 956B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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.out = nil
  22. stream.Error = nil
  23. stream.Attachment = nil
  24. cfg.streamPool.Put(stream)
  25. }
  26. func (cfg *frozenConfig) BorrowIterator(data []byte) *Iterator {
  27. iter := cfg.iteratorPool.Get().(*Iterator)
  28. iter.ResetBytes(data)
  29. return iter
  30. }
  31. func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
  32. iter.Error = nil
  33. iter.Attachment = nil
  34. cfg.iteratorPool.Put(iter)
  35. }