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 ...

// Return buffer to pool for reuse
pool.put(buffer);

§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

ZeroPool uses targeted unsafe optimizations for maximum performance while maintaining memory safety guarantees:

  • set_len() instead of resize(): When reusing pooled buffers, we use unsafe set_len() to avoid redundant zero-initialization. This is safe because:

    1. Capacity is always verified before setting length
    2. Buffers are pooled (previously allocated and cleared)
    3. Users receive initialized memory (may contain previous data, but no UB)
  • Unchecked shard indexing: Shard index is masked with shard_mask (power of 2 - 1), guaranteeing bounds. Using get_unchecked eliminates redundant bounds checks.

  • 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.

Structs§

BufferPool
A high-performance, thread-safe buffer pool optimized for I/O workloads
Builder
Builder for configuring a BufferPool