Overview
framealloc is a deterministic, frame-based memory allocation library for Rust game engines and real-time applications. It provides predictable performance through explicit lifetimes and scales seamlessly from single-threaded to multi-threaded workloads.
Not a general-purpose allocator replacement. Purpose-built for game engines, renderers, simulations, and real-time systems.
Key Capabilities
| Capability | Description |
|---|---|
| Frame Arenas | Lock-free bump allocation, reset per frame |
| Object Pools | O(1) reuse for small, frequent allocations |
| Thread Coordination | Explicit transfers, barriers, per-thread budgets |
| Static Analysis | cargo fa catches memory mistakes at build time |
| Runtime Diagnostics | Behavior filter detects pattern violations |
Features
Core Allocation
use ;
let alloc = new;
loop
Frame Retention & Promotion
// Keep frame data beyond frame boundary
let navmesh = alloc.;
// Get promoted allocations at frame end
let promotions = alloc.end_frame_with_promotions;
Thread Coordination (v0.6.0)
// Explicit cross-thread transfers
let handle = alloc.frame_box_for_transfer;
worker_channel.send;
// Receiver: let data = handle.receive();
// Frame barriers for deterministic sync
let barrier = new;
barrier.signal_frame_complete;
barrier.wait_all;
// Per-thread budgets
alloc.set_thread_frame_budget;
IDE Integration (v0.7.0)
// Enable snapshot emission for fa-insight
let snapshot_config = default
.with_directory
.with_max_snapshots;
let emitter = new;
// In your frame loop
alloc.end_frame;
let snapshot = build_snapshot;
emitter.maybe_emit; // Checks for request file
fa-insight — VS Code extension for framealloc-aware development:
- Inline diagnostics from
cargo fa - Memory inspector sidebar panel
- Real-time snapshot visualization
- Tag hierarchy and budget tracking
- CodeLens — Memory usage shown above functions (v0.2.0)
- Trend Graphs — Sparklines showing memory over time (v0.2.0)
- Budget Alerts — Toast warnings at 80%+ usage (v0.2.0)
Install: Search "FA Insight" in VS Code Marketplace or code --install-extension fa-insight-0.2.0.vsix.
Tokio Integration (v0.8.0)
Safe async/await patterns with the hybrid model:
use ;
// Main thread: frame allocations OK
alloc.begin_frame;
let scratch = alloc.;
// Async tasks: use TaskAlloc (pool-backed, auto-cleanup)
let alloc_clone = alloc.clone;
spawn;
alloc.end_frame; // Frame reset, async tasks unaffected
Key principle: Frame allocations stay on main thread, async tasks use pool/heap.
Enable with:
= { = "0.8", = ["tokio"] }
See docs/Tokio-Frame.md for the full async safety guide.
Performance Optimizations (v0.9.0)
Batch Allocations
For hot loops with multiple allocations, use batch APIs for maximum performance:
// Instead of:
for _ in 0..1000
// Use batch allocation (139x faster):
let items = alloc.;
for i in 0..1000
Minimal Mode
Disable statistics for maximum performance in production:
= { = "0.9", = ["minimal"] }
Cache Prefetch
Enable cache prefetch hints for better alloc+write patterns (x86_64 only):
= { = "0.9", = ["prefetch"] }
Specialized Batch Sizes
For known small counts, use specialized methods:
let pair = alloc.; // 2 items
let quad = alloc.; // 4 items
let octet = alloc.; // 8 items
Runtime Behavior Filter
// Opt-in runtime detection of allocation pattern issues
alloc.enable_behavior_filter;
// After running...
let report = alloc.behavior_report;
for issue in &report.issues
Static Analysis
cargo-fa is a companion tool that detects memory intent violations before runtime.
Installation
Usage
# Check specific categories
# Run all checks
# CI integration
Filtering
Subcommands
Diagnostic Categories
| Range | Category | Examples |
|---|---|---|
| FA2xx | Threading | Cross-thread access, barrier mismatch, budget missing |
| FA3xx | Budgets | Unbounded allocation loops |
| FA6xx | Lifetime | Frame escape, hot loops, missing boundaries |
| FA7xx | Async | Allocation across await, closure capture |
| FA8xx | Architecture | Tag mismatch, unknown tags |
Quick Start
Basic Usage
use ;
Bevy Integration
use *;
use SmartAllocPlugin;
Cargo Features
| Feature | Description |
|---|---|
bevy |
Bevy ECS plugin integration |
parking_lot |
Faster mutex implementation |
debug |
Memory poisoning, allocation backtraces |
tracy |
Tracy profiler integration |
nightly |
std::alloc::Allocator trait support |
diagnostics |
Enhanced runtime diagnostics |
memory_filter |
Behavior filter for pattern detection |
Performance
Allocation priority minimizes latency:
- Frame arena — Bump pointer increment, no synchronization
- Thread-local pools — Free list pop, no contention
- Global pool refill — Mutex-protected, batched
- System heap — Fallback for oversized allocations
In typical game workloads, 90%+ of allocations hit the frame arena path.
Documentation
| Resource | Description |
|---|---|
| API Docs | Generated API documentation |
| TECHNICAL.md | Architecture and implementation details |
| CHANGELOG.md | Version history and migration guides |
License
Licensed under either of:
at your option.