/gpool

Generic Pool in Go

Primary LanguageGoMIT LicenseMIT

gpool

Generic wrapper for sync.Pool in Go

Usage

int64 type

p := Pool[int64]{New: func() int64 { return 42 }}
x := p.Get()
defer p.Put(x)
fmt.Printf("x = (%T) %d", x, x)
// Output: x = (int64) 42

string type

p := Pool[string]{New: func() string { return "foo" }}
x := p.Get()
defer p.Put(x)
fmt.Printf("x = (%T) %s", x, x)
// Output: x = (string) foo

Benchmarks

% go test -bench=. -benchmem ./...
goos: darwin
goarch: arm64
pkg: github.com/sv-tools/gpool
BenchmarkSyncPool-8     699275571                1.614 ns/op           0 B/op          0 allocs/op
BenchmarkPool-8         647708158                1.732 ns/op           0 B/op          0 allocs/op