Skip to main content

Crate zeropool

Crate zeropool 

Source
Expand description

§ZeroPool — A User-Space Byte Allocator for Rust

ZeroPool is a high-performance, thread-safe byte allocator that recycles buffers through size-class bucketing and thread-local caching. Safe allocations are zero-initialized; use ZeroPool::alloc_uninit for full-overwrite workloads that need the fastest path.

  • Size-class bucketing: Power-of-two classes (4KB→64MB) for O(1) class selection
  • Lock-free shared pool: crossbeam::ArrayQueue per class — no mutexes
  • Thread-local caching: Per-class LIFO caches with magazine-style batch transfer
  • Pool isolation: Unique pool IDs prevent TLS cache cross-contamination
  • Pluggable allocator: Custom buffer creation via the Allocator trait

§Quick Start

use zeropool::ZeroPool;

let pool = ZeroPool::new();

let mut buf = pool.alloc(1024 * 1024); // 1MB — returned as RAII guard
buf[0] = 42;                            // Deref<Target = [u8]>
// automatically deallocated back to pool on drop

For full-overwrite workloads, skip zeroing with ZeroPool::alloc_uninit:

use zeropool::ZeroPool;

let pool = ZeroPool::new();
let buf = pool.alloc_uninit(5).write_from_slice(b"hello");
assert_eq!(&*buf, b"hello");

§Custom Configuration

use zeropool::ZeroPool;

let pool = ZeroPool::new()
    .min_buffer_size(4096)           // discard buffers < 4KB on dealloc
    .tls_cache_size(8)               // 8 buffers per class per thread
    .max_buffers_per_class(64)       // 64 buffers per class in shared pool
    .batch_size(4);                  // transfer 4 at a time (magazine)

§Pluggable Allocator

use zeropool::{Allocator, ZeroPool};

struct PrefaultAllocator;
impl Allocator for PrefaultAllocator {
    fn allocate(&self, capacity: usize) -> Vec<u8> {
        vec![0; capacity]
    }
}

let pool = ZeroPool::new().allocator(PrefaultAllocator);

§Ownership

When a Buf is dropped, it deallocates back to the pool. Use Buf::into_inner() to extract the Vec<u8> without returning it.

use zeropool::ZeroPool;

let pool = ZeroPool::new();

// Normal: deallocates back to pool on drop
{
    let buf = pool.alloc(1024);
}

// Extract ownership — does NOT return to pool
let buf = pool.alloc(1024);
let vec: Vec<u8> = buf.into_inner();

Macros§

pool_vec
Allocate a buffer from a pool.

Structs§

Buf
An allocated byte buffer that deallocates back to the pool on drop.
BufUninit
An allocated byte buffer whose contents are not guaranteed to be initialized.
ClassInfo
Per-class snapshot within Stats.
HeapAllocator
Standard heap allocation via vec![0; capacity].
Stats
Point-in-time snapshot of allocator statistics.
ZeroPool
A user-space byte allocator with size-class bucketing and thread-local caching.

Traits§

Allocator
Controls how the pool creates raw byte buffers.