github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/gtl/tests/freepool.go (about)

     1  // Code generated by "../generate.py --prefix=byte --PREFIX=byte -DMAXSIZE=128 -DELEM=[]byte --package=tests --output=freepool.go ../freepool.go.tpl". DO NOT EDIT.
     2  
     3  package tests
     4  
     5  // A freepool for a single thread. The interface is the same as sync.Pool, but
     6  // it avoids locks and interface conversion overhead.
     7  //
     8  // Example:
     9  //  generate.py -package=foo -prefix=int -D[]byte=foo -D128=128
    10  //
    11  //
    12  // Parameters:
    13  //  []byte: the object to be kept in the freepool
    14  //  128: the maxium number of objects to keep in the freepool
    15  
    16  type bytePool struct {
    17  	New func() []byte
    18  	p   [][]byte
    19  }
    20  
    21  func (p *bytePool) Get() []byte {
    22  	if len(p.p) == 0 {
    23  		return p.New()
    24  	}
    25  	tmp := p.p[len(p.p)-1]
    26  	p.p = p.p[:len(p.p)-1]
    27  	return tmp
    28  }
    29  
    30  func (p *bytePool) Put(tmp []byte) {
    31  	if len(p.p) < 128 {
    32  		p.p = append(p.p, tmp)
    33  	}
    34  }