zeropool
last updated: Oct 20, 2023
https://github.com/colega/zeropool
zeropool
is a zero-allocation type-safesync.Pool
// The function provided to zeropool.New() makes a Pool of a correct type using generics.
pool := zeropool.New(func() []byte { return nil })
// This is a []byte, no need to make type-assertion, no need to de-reference.
buf := pool.Get()
// This does not allocate.
pool.Put(buf)
Go provides
sync.Pool
pool implementation that allows storingany
values (interface{}
values). It is great but has two major drawbacks:
- It's not type-safe, a type-assertion is needed on the elements provided by
Get()
.- Since it stores
interface{}
values, it means that your value will escape1 to the heap unless you store a pointer (it would escape, but maybe just once).