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

     1  package jzon
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  var (
     8  	defaultStreamerPool = newStreamerPool()
     9  )
    10  
    11  type streamerPool struct {
    12  	pool sync.Pool
    13  }
    14  
    15  func newStreamerPool() *streamerPool {
    16  	return &streamerPool{
    17  		pool: sync.Pool{
    18  			New: func() interface{} {
    19  				return &Streamer{
    20  					buffer: make([]byte, 0, 64),
    21  				}
    22  			},
    23  		},
    24  	}
    25  }
    26  
    27  func (p *streamerPool) borrowStreamer() *Streamer {
    28  	s := p.pool.Get().(*Streamer)
    29  	return s
    30  }
    31  
    32  func (p *streamerPool) returnStreamer(s *Streamer) {
    33  	s.reset()
    34  	p.pool.Put(s)
    35  }