ZeroPool
A high-performance, lock-free buffer pool for Rust.
Usage
use BufferPool;
let pool = new;
let mut buf = pool.get; // 1MB — returned as RAII guard
buf = 42; // Deref<Target = [u8]>
// automatically returned to pool on drop
How it works
Buffers are routed into 8 power-of-two size classes (4KB–64MB). Each thread keeps a local cache per class; when empty, a batch of buffers is moved from the shared lock-free queue (crossbeam::ArrayQueue). Unique pool IDs prevent cross-pool contamination in TLS.
Hot path (~24ns): TLS cache pop — no locks, no atomics on the data path.
Configuration
Defaults are auto-tuned to CPU count. Override as needed:
let pool = new
.tls_cache_size // per-class per-thread cache depth
.max_buffers_per_class // shared pool capacity per class
.min_buffer_size // discard returned buffers smaller than this
.batch_size // TLS <-> shared transfer size
.pinned_memory; // mlock buffers to prevent swapping
Statistics
let s = pool.stats;
println!;
// gets: 1 | puts: 1 | hit_rate: 0.0%
// tls_hits: 0 (0.0%) | shared_hits: 0 | allocations: 1 | discards: 0 | oversize: 0
pool.reset_stats;
Counters use Relaxed atomics — no measurable overhead on the hot path.
Thread safety
BufferPool is Clone + Send + Sync (Arc<PoolState> internally). Each clone shares the same pool; each thread gets its own TLS cache automatically.
Benchmarks
Single-threaded TLS-hit latency (2-core VM):
| Size | Latency |
|---|---|
| 1KB–64KB | ~24 ns |
| 1MB | ~25 ns |
License
Apache-2.0 OR MIT