Crate zeropool

Crate zeropool 

Source
Expand description

§ZeroPool - High-Performance Buffer Pool for Rust

ZeroPool provides a thread-safe buffer pool optimized for high-throughput I/O workloads. It achieves exceptional performance through:

  • System-aware defaults: Automatically adapts to CPU count and hardware topology
  • Thread-local caching: Lock-free fast path with adaptive cache size (2-8 buffers)
  • Sharded global pool: Reduces contention with CPU-scaled sharding (4-128 shards)
  • Zero-copy operations: Avoids unnecessary memory allocations and copies
  • Smart buffer reuse: First-fit allocation with configurable size limits

§Performance

In benchmarks with 500MB buffers, ZeroPool achieves:

  • 70% faster than no pooling (176ms → 52ms)
  • 3.36x speedup through buffer reuse
  • Lock-free fast path for single-threaded workloads
  • Scales automatically from embedded systems to 128+ core servers

§Quick Start

use zeropool::BufferPool;

// Create a pool with smart defaults
let pool = BufferPool::new();

// Get a buffer
let mut buffer = pool.get(1024 * 1024); // 1MB buffer

// Use the buffer for I/O operations
// ... read/write operations ...
// Buffer automatically returned to pool when it goes out of scope

§Custom Configuration

Use the builder pattern for custom configuration:

use zeropool::BufferPool;

let pool = BufferPool::builder()
    .min_buffer_size(512 * 1024)      // Keep buffers >= 512KB
    .tls_cache_size(8)                // 8 buffers per thread
    .max_buffers_per_shard(32)        // Up to 32 buffers per shard
    .num_shards(16)                   // Override CPU-based default
    .build();

§System-Aware Scaling

ZeroPool automatically adapts to your system:

SystemCoresTLS CacheShardsBuffers/ShardTotal Capacity
Embedded4441664 (~64MB)
Laptop86816128 (~128MB)
Workstation166832256 (~256MB)
Small Server32816641024 (~1GB)
Large Server64832642048 (~2GB)
Supercompute128864644096 (~4GB)

§Safety and Security

ZeroPool prioritizes safety and security with the following guarantees:

  • Memory zeroing: Buffers are not zeroed by default for maximum performance. Users should manually zero buffers if information leakage prevention is required for their use case.

  • Safe memory operations: Uses safe Rust methods like resize() and fill() to manage buffer contents, avoiding unsafe code wherever possible. The only remaining unsafe code is for safe trait implementations (Send/Sync).

  • Safe shard indexing: Shard index is masked with shard_mask (power of 2 - 1), guaranteeing bounds. Runtime assertions verify this invariant before access.

  • Optional memory pinning: When pinned_memory is enabled, buffers are locked in RAM using mlock to prevent swapping. This is best-effort and fails gracefully if insufficient permissions. Useful for security-sensitive or latency-critical workloads.

§Ownership and Pool Return

When a PooledBuffer is dropped normally, the buffer is returned to the pool for reuse. However, if you need to extract the underlying Vec<u8> and prevent pool return, use PooledBuffer::into_inner() or PooledBuffer::into_vec().

use zeropool::BufferPool;

let pool = BufferPool::new();

// Normal usage - returns to pool on drop
{
    let buffer = pool.get(1024);
    // buffer is automatically returned when it goes out of scope
}

// Extract ownership - does NOT return to pool
let buffer = pool.get(1024);
let vec: Vec<u8> = buffer.into_inner();
// vec is now owned, buffer was consumed

Structs§

BufferPool
A high-performance, thread-safe buffer pool optimized for I/O workloads
Builder
Builder for configuring a BufferPool
PooledBuffer
RAII wrapper for a pooled buffer that automatically returns to the pool on drop.

Enums§

EvictionPolicy
Buffer eviction policy