ZeroPool
A high-performance, security-focused buffer pool for Rust
Why ZeroPool?
ZeroPool is a thread-safe buffer pool that combines high performance with strong security guarantees. Unlike traditional buffer pools that trade security for speed, ZeroPool provides both:
- Secure by default: All memory is automatically zeroed to prevent information leakage
- Safe Rust: No unsafe memory operations, only safe abstractions
- High performance: Thread-local caching and smart allocation strategies minimize overhead
- Auto-configured: Adapts to your CPU topology for optimal multi-threaded performance
Perfect for applications that handle sensitive data (credentials, encryption keys, PII) while maintaining high throughput.
Quick Start
use BufferPool;
let pool = new;
// Get a buffer (automatically zeroed and returned to pool when dropped)
let mut buffer = pool.get; // 1MB
// Use it for I/O or data processing
file.read?;
// Buffer automatically zeroed and returned to pool here
Key Features
Security First ๐
- Memory zeroing: All buffers are explicitly zeroed on allocation and return
- No information leakage: Previous data never exposed to new allocations
- Safe Rust: Zero unsafe memory operations (only safe Send/Sync trait impls)
- Optional memory pinning: Prevent sensitive data from swapping to disk
High Performance โก
- Thread-local caching: Lock-free fast path for 60-110ns allocation latency
- Smart sharding: Minimal contention with power-of-2 shard count
- Auto-configured: CPU-aware defaults (4-128 shards, 2-8 TLS cache size)
- Configurable eviction: Choose between LIFO or CLOCK-Pro algorithms
Simple API ๐ฏ
- Just
get()anddrop(): Buffers automatically return to the pool - Builder pattern: Easy customization when needed
- Type-safe: Leverages Rust's ownership for automatic resource management
Architecture
Thread 1 Thread 2 Thread N
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ
โ TLS (4) โ โ TLS (4) โ โ TLS (4) โ โ Lock-free (60-110ns)
โโโโโโฌโโโโโ โโโโโโฌโโโโโ โโโโโโฌโโโโโ
โโโโโโโโโโโโฌโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโ
โ Sharded Pool โ โ Thread affinity
โ [0][1]...[N] โ โ Minimal contention
โโโโโโโโโโโโโโโโโโ
Fast path: Thread-local cache (lock-free, ~60-110ns) Slow path: Thread-affinity shard selection (better cache locality) Optimization: Power-of-2 shards enable bitwise AND instead of modulo
Performance
Cache Behavior Benchmarks
| Pattern | Metric | Result |
|---|---|---|
| Ping-pong (LIFO) | Time per operation | 3.56 ยตs |
| Ping-pong (ClockPro) | Time per operation | 3.68 ยตs |
| Hot/cold buffers | Time per operation | 1.05 ยตs |
| Multi-size workload | Time per operation | 6.2 ยตs |
| TLS cache (2 bufs) | Allocation latency | 60.5 ns |
| TLS cache (4 bufs) | Allocation latency | 108 ns |
| TLS cache (8 bufs) | Allocation latency | 288 ns |
| Eviction pressure | Time per operation | 400 ns |
Multi-threaded Scaling
| Threads | Time per 1000 ops | Notes |
|---|---|---|
| 1 | 44.7 ยตs | Single-threaded baseline |
| 4 | 141 ยตs | Good scaling with TLS cache |
| 8 | 282 ยตs | Near-linear scaling |
| 16 | 605 ยตs | Still scales well at high concurrency |
Performance Characteristics
- Constant latency: 60-110ns for TLS cache hits regardless of buffer size
- Secure by default: ~20-25% overhead vs unsafe implementations (acceptable trade-off)
- Lock-free fast path: Thread-local cache eliminates contention
- Scales linearly: Near-linear scaling up to 16+ threads
Run yourself:
Test System
- CPU: Intel i9-10900K @ 3.7GHz (10 cores, 20 threads, 5.3GHz turbo)
- RAM: 32GB DDR4
- OS: Linux 6.17.0
Configuration
use BufferPool;
let pool = builder
.tls_cache_size // Buffers per thread
.min_buffer_size // Keep buffers โฅ 512KB
.max_buffers_per_shard // Max pooled buffers
.num_shards // Override auto-detection
.build;
Defaults (auto-configured based on CPU count):
- Shards: 4-128 (power-of-2, ~1 shard per 2 cores)
- TLS cache: 2-8 buffers per thread
- Min buffer size: 1MB
- Max per shard: 16-64 buffers
Memory Pinning
Lock buffer memory in RAM to prevent swapping sensitive data:
use BufferPool;
let pool = builder
.pinned_memory
.build;
Useful for high-performance computing, security-sensitive data, or real-time systems. May require elevated privileges on some systems. Falls back gracefully if pinning fails.
Eviction Policy
Choose between simple LIFO or intelligent CLOCK-Pro buffer eviction:
use ;
let pool = builder
.eviction_policy // Better cache locality (default)
.build;
let pool_lifo = builder
.eviction_policy // Simple, lowest overhead
.build;
CLOCK-Pro (default): Uses access counters to favor recently-used buffers, preventing cache thrashing in mixed-size workloads. ~8 bytes overhead per buffer.
LIFO: Simple last-in-first-out eviction. Minimal memory overhead, best for uniform buffer sizes.
How It Works
Thread-local caching (lock-free)
- Lock-free access to recently used buffers
- No atomic operations on fast path (60-110ns latency)
- Zero cache-line bouncing
Thread-local shard affinity
- Each thread consistently uses the same shard (cache locality)
shard = hash(thread_id) & (num_shards - 1)(no modulo)- Minimal lock contention + better CPU cache utilization
- Auto-scales with CPU count
First-fit allocation
- O(1) instead of O(n) best-fit
- Perfect for predictable I/O buffer sizes
Secure memory zeroing
- All buffers are zeroed on return using
fill(0) - All buffers are zeroed on allocation using
resize(size, 0) - Prevents information leakage between buffer users
- Safe for security-sensitive workloads
Thread Safety
BufferPool is Clone and thread-safe:
let pool = new;
for _ in 0..4
Safety and Security
ZeroPool prioritizes both safety and security:
Memory Zeroing
- All buffers are explicitly zeroed when returned to the pool using
fill(0) - All buffers are zeroed when allocated from the pool using
resize(size, 0) - Prevents information leakage between different buffer users
- Safe for processing sensitive data (credentials, encryption keys, PII)
Safe Rust
- Uses only safe Rust operations for memory management
- No unsafe
set_len()calls or uninitialized memory - Send and Sync automatically derived (no unsafe trait implementations)
- All safety guaranteed by Rust's type system
Security Best Practices
- Defense-in-depth: Zeroing at both allocation and deallocation
- Optional memory pinning: Prevent swapping sensitive data to disk
- Auditable: Simple, safe code that's easy to review
- Production-ready: Suitable for security-critical applications
Use Cases
Security-Sensitive Applications
- Cryptographic operations: Handle keys and sensitive material safely
- Authentication systems: Process credentials without leakage risk
- PII processing: GDPR/HIPAA compliant data handling
- Secure communications: Network buffers for encrypted protocols
High-Performance I/O
- Async file loading: io_uring, tokio, async-std
- Network servers: HTTP, gRPC, WebSocket servers
- Data processing: ETL pipelines, log processing
- LLM inference: Fast checkpoint loading
Real-World Example
Before ZeroPool, loading GPT-2 checkpoints took 200ms with 70% spent on buffer allocation. With ZeroPool: 53ms (3.8x faster) while maintaining security guarantees.
System Scaling
ZeroPool automatically adapts to your system:
| System | Cores | TLS Cache | Shards | Buffers/Shard | Total Capacity |
|---|---|---|---|---|---|
| Embedded | 4 | 4 | 4 | 16 | 64 (~64MB) |
| Laptop | 8 | 6 | 8 | 16 | 128 (~128MB) |
| Workstation | 16 | 6 | 8 | 32 | 256 (~256MB) |
| Small Server | 32 | 8 | 16 | 64 | 1024 (~1GB) |
| Large Server | 64 | 8 | 32 | 64 | 2048 (~2GB) |
| Supercompute | 128 | 8 | 64 | 64 | 4096 (~4GB) |
Comparison with Alternatives
| Feature | ZeroPool | bytes::BytesMut | Lifeguard | Sharded-Slab |
|---|---|---|---|---|
| Memory zeroing | โ Always | โ No | โ No | โ No |
| Safe Rust | โ 100% | โ ๏ธ Some unsafe | โ ๏ธ Some unsafe | โ ๏ธ Heavy unsafe |
| Thread-safe | โ Yes | โ No | โ ๏ธ Limited | โ Yes |
| Lock-free path | โ TLS cache | โ No | โ No | โ ๏ธ Partial |
| Auto-configured | โ CPU-aware | โ Manual | โ Manual | โ Manual |
| Security focus | โ Primary | โ No | โ No | โ No |
ZeroPool is the only buffer pool designed for security-first applications while maintaining competitive performance.
License
Dual licensed under Apache-2.0 or MIT.
Contributing
PRs welcome! Please include benchmarks for performance changes and ensure all tests pass:
Changelog
See CHANGELOG.md for version history.
Credits
Built with โค๏ธ for the Rust community. Inspired by the need for secure, high-performance buffer management in production systems.