Crate fastalloc

Crate fastalloc 

Source
Expand description

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

§Overview

fastalloc provides memory pools that allow you to reuse allocations efficiently, reducing allocation overhead and improving cache locality. It’s designed for use cases where objects are frequently created and destroyed, such as:

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

§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
  • Multiple allocation strategies: Stack (LIFO), free-list, and bitmap allocators
  • Optional statistics: Track allocation patterns and pool usage (with stats feature)
  • no_std support: Works in embedded and bare-metal environments

§Quick Start

use fastalloc::{FixedPool, PoolConfig};

// Create a pool of 1000 integers
let pool = FixedPool::<i32>::new(1000).unwrap();

// Allocate from the pool
let mut handle = pool.allocate(42).unwrap();

// Use the value
assert_eq!(*handle, 42);
*handle = 100;
assert_eq!(*handle, 100);

// Automatically returned to pool when handle is dropped
drop(handle);

§Builder Configuration

use fastalloc::{PoolConfig, GrowthStrategy};

let config = PoolConfig::builder()
    .capacity(1000)
    .max_capacity(Some(10000))
    .growth_strategy(GrowthStrategy::Exponential { factor: 2.0 })
    .alignment(64) // Cache-line aligned
    .pre_initialize(true)
    .build()
    .unwrap();

§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

§Safety

This crate minimizes the use of unsafe code and leverages Rust’s ownership system to prevent common memory safety issues like use-after-free and double-free. Debug builds include additional runtime checks for:

  • Double-free detection
  • Leak detection
  • Pool exhaustion warnings

Re-exports§

pub use config::GrowthStrategy;
pub use config::InitializationStrategy;
pub use config::PoolConfig;
pub use error::Error;
pub use error::Result;
pub use handle::OwnedHandle;
pub use handle::SharedHandle;
pub use handle::WeakHandle;
pub use pool::FixedPool;
pub use pool::GrowingPool;
pub use traits::Poolable;
pub use pool::ThreadLocalPool;std
pub use pool::ThreadSafePool;std
pub use pool::LockFreePool;std and lock-free
pub use stats::PoolStatistics;stats
pub use stats::StatisticsCollector;stats

Modules§

config
Configuration types for memory pools.
error
Error types for the fastalloc crate.
handle
Smart handles for pool-allocated objects.
pool
Memory pool implementations.
prelude
Convenient re-exports of commonly used types
statsstats
Statistics collection and reporting for memory pools.
traits
Traits for working with memory pools.