github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_pool.go (about)

     1  package jzon
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  var (
     8  	defaultIteratorPool = newIteratorPool()
     9  )
    10  
    11  type iteratorPool struct {
    12  	pool sync.Pool
    13  }
    14  
    15  func newIteratorPool() *iteratorPool {
    16  	return &iteratorPool{
    17  		pool: sync.Pool{
    18  			New: func() interface{} {
    19  				return &Iterator{
    20  					tmpBuffer: make([]byte, 64),
    21  					fixbuf:    make([]byte, 64),
    22  				}
    23  			},
    24  		},
    25  	}
    26  }
    27  
    28  func (p *iteratorPool) borrowIterator() *Iterator {
    29  	it := p.pool.Get().(*Iterator)
    30  	return it
    31  }
    32  
    33  func (p *iteratorPool) returnIterator(it *Iterator) {
    34  	it.reset()
    35  	p.pool.Put(it)
    36  }