github.com/grailbio/base@v0.0.11/gtl/freepool.go.tpl (about) 1 package PACKAGE 2 3 // A freepool for a single thread. The interface is the same as sync.Pool, but 4 // it avoids locks and interface conversion overhead. 5 // 6 // Example: 7 // generate.py -package=foo -prefix=int -DELEM=foo -DMAXSIZE=128 8 // 9 // 10 // Parameters: 11 // ELEM: the object to be kept in the freepool 12 // MAXSIZE: the maxium number of objects to keep in the freepool 13 14 type ZZPool struct { 15 New func() ELEM 16 p []ELEM 17 } 18 19 func (p *ZZPool) Get() ELEM { 20 if len(p.p) == 0 { 21 return p.New() 22 } 23 tmp := p.p[len(p.p)-1] 24 p.p = p.p[:len(p.p)-1] 25 return tmp 26 } 27 28 func (p *ZZPool) Put(tmp ELEM) { 29 if len(p.p) < MAXSIZE { 30 p.p = append(p.p, tmp) 31 } 32 }