Slab
Slab allocation memory pools for Go.
Install / Use
/learn @funny/SlabREADME
Introduction
Slab allocation memory pools for Go.
Usage
Use lock-free memory pool:
pool := slab.NewAtomPool(
64, // The smallest chunk size is 64B.
64 * 1024, // The largest chunk size is 64KB.
2, // Power of 2 growth in chunk size.
1024 * 1024, // Each slab will be 1MB in size.
)
buf1 := pool.Alloc(64)
... use the buf ...
pool.Free(buf)
Use chan based memory pool:
pool := slab.NewChanPool(
64, // The smallest chunk size is 64B.
64 * 1024, // The largest chunk size is 64KB.
2, // Power of 2 growth in chunk size.
1024 * 1024, // Each slab will be 1MB in size.
)
buf1 := pool.Alloc(64)
... use the buf ...
pool.Free(buf)
Use sync.Pool based memory pool:
pool := slab.NewSyncPool(
64, // The smallest chunk size is 64B.
64 * 1024, // The largest chunk size is 64KB.
2, // Power of 2 growth in chunk size.
)
buf := pool.Alloc(64)
... use the buf ...
pool.Free(buf)
Performance
Result of GOMAXPROCS=16 go test -v -bench=. -benchmem:
Benchmark_AtomPool_AllocAndFree_128-8 20000000 104 ns/op 0 B/op 0 allocs/op
Benchmark_AtomPool_AllocAndFree_256-8 20000000 106 ns/op 0 B/op 0 allocs/op
Benchmark_AtomPool_AllocAndFree_512-8 20000000 109 ns/op 0 B/op 0 allocs/op
Benchmark_ChanPool_AllocAndFree_128-8 5000000 246 ns/op 0 B/op 0 allocs/op
Benchmark_ChanPool_AllocAndFree_256-8 5000000 221 ns/op 0 B/op 0 allocs/op
Benchmark_ChanPool_AllocAndFree_512-8 5000000 304 ns/op 0 B/op 0 allocs/op
Benchmark_LockPool_AllocAndFree_128-8 5000000 275 ns/op 0 B/op 0 allocs/op
Benchmark_LockPool_AllocAndFree_256-8 5000000 265 ns/op 0 B/op 0 allocs/op
Benchmark_LockPool_AllocAndFree_512-8 5000000 280 ns/op 0 B/op 0 allocs/op
Benchmark_SyncPool_AllocAndFree_128-8 100000000 22.9 ns/op 32 B/op 1 allocs/op
Benchmark_SyncPool_AllocAndFree_256-8 100000000 23.4 ns/op 32 B/op 1 allocs/op
Benchmark_SyncPool_AllocAndFree_512-8 50000000 23.3 ns/op 32 B/op 1 allocs/op
Benchmark_SyncPool_CacheMiss_128-8 10000000 196 ns/op 160 B/op 2 allocs/op
Benchmark_SyncPool_CacheMiss_256-8 5000000 228 ns/op 288 B/op 2 allocs/op
Benchmark_SyncPool_CacheMiss_512-8 10000000 251 ns/op 544 B/op 2 allocs/op
Benchmark_Make_128-8 100000000 17.9 ns/op 128 B/op 1 allocs/op
Benchmark_Make_256-8 50000000 31.7 ns/op 256 B/op 1 allocs/op
Benchmark_Make_512-8 20000000 63.6 ns/op 512 B/op 1 allocs/op
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
