gitee.com/quant1x/pkg@v0.2.8/fastjson/pool.go (about)

     1  package fastjson
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  // ParserPool may be used for pooling Parsers for similarly typed JSONs.
     8  type ParserPool struct {
     9  	pool sync.Pool
    10  }
    11  
    12  // Get returns a Parser from pp.
    13  //
    14  // The Parser must be Put to pp after use.
    15  func (pp *ParserPool) Get() *Parser {
    16  	v := pp.pool.Get()
    17  	if v == nil {
    18  		return &Parser{}
    19  	}
    20  	return v.(*Parser)
    21  }
    22  
    23  // Put returns p to pp.
    24  //
    25  // p and objects recursively returned from p cannot be used after p
    26  // is put into pp.
    27  func (pp *ParserPool) Put(p *Parser) {
    28  	pp.pool.Put(p)
    29  }
    30  
    31  // ArenaPool may be used for pooling Arenas for similarly typed JSONs.
    32  type ArenaPool struct {
    33  	pool sync.Pool
    34  }
    35  
    36  // Get returns an Arena from ap.
    37  //
    38  // The Arena must be Put to ap after use.
    39  func (ap *ArenaPool) Get() *Arena {
    40  	v := ap.pool.Get()
    41  	if v == nil {
    42  		return &Arena{}
    43  	}
    44  	return v.(*Arena)
    45  }
    46  
    47  // Put returns a to ap.
    48  //
    49  // a and objects created by a cannot be used after a is put into ap.
    50  func (ap *ArenaPool) Put(a *Arena) {
    51  	ap.pool.Put(a)
    52  }