fastalloc
A high-performance memory pooling library for Rust with type-safe handles and zero-cost abstractions.
Version 1.0 - Production-ready stable release with comprehensive testing and battle-tested API.
Features
- 🚀 Multiple pool types: Fixed-size, growing, thread-local, and thread-safe pools
- 🔒 Type-safe handles: RAII-based handles that automatically return objects to the pool
- ⚙️ Flexible configuration: Builder pattern with extensive customization options
- 📊 Optional statistics: Track allocation patterns and pool usage
- 🔧 Multiple allocation strategies: Stack (LIFO), free-list, and bitmap allocators
- 🌐 no_std support: Works in embedded and bare-metal environments
Quick Start
Add this to your Cargo.toml:
[]
= "1.0"
Basic usage:
use FixedPool;
// Create a pool of 1000 integers
let pool = new.unwrap;
// Allocate from the pool
let mut handle = pool.allocate.unwrap;
// Use the value
assert_eq!;
*handle = 100;
assert_eq!;
// Automatically returned to pool when handle is dropped
drop;
Why Use Memory Pools?
Memory pools significantly improve performance in scenarios with frequent allocations:
- Game Development: Entities, particles, physics objects
- Real-Time Systems: Audio processing, robotics
- High-Performance Servers: Connection pooling, request handling
- Data Processing: Temporary objects in hot paths
- Scientific Computing: Matrices, particles, graph nodes
Performance
Typical performance characteristics:
- Fixed pool allocation: < 20ns per object
- Deallocation: < 10ns per object
- Memory overhead: < 5% for pools over 1000 objects
- Thread-safe pool: < 100ns with moderate contention
Examples
Growing Pool with Configuration
use ;
let config = builder
.capacity
.max_capacity
.growth_strategy
.alignment // Cache-line aligned
.build
.unwrap;
let pool = with_config.unwrap;
Thread-Safe Pool
use ThreadSafePool;
use Arc;
use thread;
let pool = new;
let mut handles = vec!;
for i in 0..4
for handle in handles
Custom Initialization
use ;
let config = builder
.capacity
.reset_fn
.build
.unwrap;
Statistics Tracking
Pool Types
FixedPool
Pre-allocated fixed-size pool with O(1) operations and zero fragmentation.
let pool = new.unwrap;
GrowingPool
Dynamic pool that grows based on demand according to a configurable strategy.
let pool = with_config.unwrap;
ThreadLocalPool
Per-thread pool that avoids synchronization overhead.
let pool = new.unwrap;
ThreadSafePool
Lock-based concurrent pool safe for multi-threaded access.
let pool = new.unwrap;
Features
Enable optional features in your Cargo.toml:
[]
= { = "1.0", = ["stats", "serde", "parking_lot"] }
Available features:
std(default): Enable standard library supportstats: Pool statistics collection and reportingserde: Serialization supportparking_lot: Faster mutex implementationcrossbeam: Lock-free data structurestracing: Instrumentation supportlock-free: Experimental lock-free pool (requirescrossbeam)
no_std Support
fastalloc works in no_std environments:
[]
= { = "1.0", = false }
Benchmarks
Run benchmarks with:
See docs/benchmarks/ for detailed performance comparisons.
Documentation
Examples
Run examples with:
Safety
fastalloc minimizes unsafe code and leverages Rust's ownership system to prevent:
- Use-after-free
- Double-free
- Data races
- Memory leaks
Debug builds include additional runtime checks.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Authors
- Eshan Roy eshanized@proton.me
Organization
Tonmoy Infrastructure & Vision
Repository
https://gitlab.com/tivisionoss/crates/fastalloc
Changelog
See CHANGELOG.md for version history.