framealloc 0.11.1

Intent-aware, thread-smart memory allocation for Rust game engines
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# framealloc Patterns Guide


This guide covers common patterns and best practices for using framealloc effectively (2-20 hours experience).

## Table of Contents


1. [Frame Allocation Patterns]#frame-allocation-patterns
2. [Pool Allocation Patterns]#pool-allocation-patterns
3. [Threading Patterns]#threading-patterns
4. [Organization Patterns]#organization-patterns
5. [Lifecycle Patterns]#lifecycle-patterns
6. [Error Handling Patterns]#error-handling-patterns

## Frame Allocation Patterns


### The Scratch Buffer Pattern


Most common pattern - temporary data per frame:

```rust
struct FrameScratch {
    positions: Vec<Vector3>,
    velocities: Vec<Vector3>,
    forces: Vec<Vector3>,
    indices: Vec<u32>,
}

impl FrameScratch {
    fn new(alloc: &SmartAlloc) -> Self {
        Self {
            positions: alloc.frame_vec(),
            velocities: alloc.frame_vec(),
            forces: alloc.frame_vec(),
            indices: alloc.frame_vec(),
        }
    }
}

// Usage
alloc.begin_frame();
let scratch = FrameScratch::new(&alloc);
// Fill with data...
process_physics(&scratch);
alloc.end_frame(); // Everything freed automatically
```

### The Builder Pattern


Build complex structures with frame allocations:

```rust
struct MeshBuilder<'a> {
    vertices: &'a mut Vec<Vertex>,
    indices: &'a mut Vec<u32>,
    normals: &'a mut Vec<Vector3>,
}

impl<'a> MeshBuilder<'a> {
    fn new(alloc: &'a SmartAlloc) -> Self {
        Self {
            vertices: alloc.frame_vec(),
            indices: alloc.frame_vec(),
            normals: alloc.frame_vec(),
        }
    }
    
    fn add_vertex(&mut self, pos: Vector3) -> &mut Self {
        self.vertices.push(Vertex::new(pos));
        self
    }
    
    fn build(self) -> Mesh {
        Mesh::new(self.vertices, self.indices)
    }
}

// Usage
alloc.begin_frame();
let mesh = MeshBuilder::new(&alloc)
    .add_vertex(Vector3::new(0, 0, 0))
    .add_vertex(Vector3::new(1, 0, 0))
    .add_vertex(Vector3::new(0, 1, 0))
    .build();
alloc.end_frame();
```

### The Temporary View Pattern


Create temporary views without copying:

```rust
struct SubSlice<'a, T> {
    data: &'a [T],
    offset: usize,
    len: usize,
}

impl<'a, T> SubSlice<'a, T> {
    fn from_slice(slice: &'a [T], range: Range<usize>) -> Self {
        Self {
            data: &slice[range],
            offset: range.start,
            len: range.end - range.start,
        }
    }
}

// Usage with frame allocation
alloc.begin_frame();
let all_vertices = alloc.frame_slice::<Vertex>(1000);
let visible_subset = SubSlice::from_slice(all_vertices, 100..200);
process_visible(&visible_subset);
alloc.end_frame();
```

## Pool Allocation Patterns


### The Component Pool Pattern


Game entities with pooled components:

```rust
#[derive(Component)]

struct Transform {
    position: Vector3,
    rotation: Quaternion,
}

#[derive(Component)]

struct Velocity {
    vector: Vector3,
}

struct EntityWorld {
    transforms: PoolHandle<Transform>,
    velocities: PoolHandle<Velocity>,
    // ... other component pools
}

impl EntityWorld {
    fn new(alloc: &SmartAlloc) -> Self {
        Self {
            transforms: alloc.pool_handle(),
            velocities: alloc.pool_handle(),
        }
    }
    
    fn spawn_entity(&mut self, pos: Vector3) -> EntityId {
        let id = EntityId::new();
        self.transforms.insert(id, Transform::new(pos));
        id
    }
}
```

### The Cache Pattern


Pool as a cache for frequently reused objects:

```rust
struct ObjectCache<T> {
    available: Vec<T>,
    in_use: HashSet<usize>,
}

impl<T: Default> ObjectCache<T> {
    fn get(&mut self, alloc: &SmartAlloc) -> PoolHandle<T> {
        if let Some(obj) = self.available.pop() {
            alloc.pool_box(obj)
        } else {
            alloc.pool_box(T::default())
        }
    }
    
    fn return_object(&mut self, obj: T) {
        self.available.push(obj);
    }
}
```

### The Resource Pool Pattern


Limited resources with pool tracking:

```rust
struct ResourcePool<T> {
    max_size: usize,
    current_size: usize,
    pool: PoolHandle<T>,
}

impl<T> ResourcePool<T> {
    fn acquire(&mut self, alloc: &SmartAlloc) -> Option<PoolHandle<T>> {
        if self.current_size < self.max_size {
            self.current_size += 1;
            Some(alloc.pool_box(T::new()))
        } else {
            None
        }
    }
    
    fn release(&mut self, _handle: PoolHandle<T>) {
        self.current_size -= 1;
    }
}
```

## Threading Patterns


### The Job System Pattern


Frame allocations in worker threads:

```rust
struct JobSystem {
    alloc: SmartAlloc,
    workers: Vec<JoinHandle<()>>,
}

impl JobSystem {
    fn dispatch_jobs(&mut self, jobs: Vec<Job>) {
        for job in jobs {
            let alloc_clone = self.alloc.clone();
            let handle = thread::spawn(move || {
                alloc_clone.begin_frame();
                job.execute(&alloc_clone);
                alloc_clone.end_frame();
            });
            self.workers.push(handle);
        }
        
        // Wait for all jobs
        for handle in self.workers.drain(..) {
            handle.join().unwrap();
        }
    }
}
```

### The Producer-Consumer Pattern


Cross-thread data transfer:

```rust
fn producer_thread(alloc: SmartAlloc, sender: Sender<TransferHandle<Data>>) {
    loop {
        alloc.begin_frame();
        
        // Produce data
        let data = alloc.frame_box(Data::generate());
        
        // Transfer to consumer
        let handle = alloc.frame_box_for_transfer(data);
        sender.send(handle).unwrap();
        
        alloc.end_frame();
    }
}

fn consumer_thread(receiver: Receiver<TransferHandle<Data>>) {
    while let Ok(handle) = receiver.recv() {
        let data = handle.receive();
        process_data(&data);
        // Data automatically freed when dropped
    }
}
```

### The Thread-Local Pattern


Each thread maintains its own allocator state:

```rust
thread_local! {
    static THREAD_ALLOC: RefCell<SmartAlloc> = RefCell::new(SmartAlloc::new(Default::default()));
}

fn process_on_thread() {
    THREAD_ALLOC.with(|alloc| {
        let mut alloc = alloc.borrow_mut();
        alloc.begin_frame();
        
        let local_data = alloc.frame_vec::<WorkItem>();
        // Process work...
        
        alloc.end_frame();
    });
}
```

## Organization Patterns


### The Tag Pattern


Organize allocations by subsystem:

```rust
struct SystemAllocators {
    physics: TaggedAllocator,
    rendering: TaggedAllocator,
    audio: TaggedAllocator,
    ai: TaggedAllocator,
}

impl SystemAllocators {
    fn new(alloc: &SmartAlloc) -> Self {
        Self {
            physics: TaggedAllocator::new(alloc, "physics"),
            rendering: TaggedAllocator::new(alloc, "rendering"),
            audio: TaggedAllocator::new(alloc, "audio"),
            ai: TaggedAllocator::new(alloc, "ai"),
        }
    }
}

// Usage
let systems = SystemAllocators::new(&alloc);
let contacts = systems.physics.frame_vec::<Contact>();
let draw_calls = systems.rendering.frame_vec::<DrawCall>();
```

### The Budget Pattern


Per-system memory budgets:

```rust
struct BudgetManager {
    budgets: HashMap<String, usize>,
    current: HashMap<String, usize>,
}

impl BudgetManager {
    fn check_budget(&mut self, tag: &str, size: usize) -> bool {
        let used = self.current.entry(tag.to_string()).or_insert(0);
        let budget = *self.budgets.get(tag).unwrap_or(0);
        
        if *used + size <= budget {
            *used += size;
            true
        } else {
            false
        }
    }
    
    fn reset_frame(&mut self) {
        for (_, used) in self.current.iter_mut() {
            *used = 0;
        }
    }
}
```

### The Scoped Pattern


RAII for tagged allocations:

```rust
struct ScopedTag<'a> {
    alloc: &'a SmartAlloc,
    tag: String,
}

impl<'a> ScopedTag<'a> {
    fn new(alloc: &'a SmartAlloc, tag: &str) -> Self {
        Self {
            alloc,
            tag: tag.to_string(),
        }
    }
    
    fn alloc_frame<T>(&self) -> FrameBox<T> {
        self.alloc.with_tag(&self.tag, |a| a.frame_box())
    }
}

impl<'a> Drop for ScopedTag<'a> {
    fn drop(&mut self) {
        // Cleanup if needed
    }
}
```

## Lifecycle Patterns


### The Frame Retention Pattern


Keep specific frame data beyond frame boundary:

```rust
struct FrameRetainer<T> {
    data: Option<FrameRetained<T>>,
    policy: RetentionPolicy,
}

impl<T> FrameRetainer<T> {
    fn retain(&mut self, alloc: &SmartAlloc, data: T) {
        self.data = Some(alloc.frame_retained(data, self.policy));
    }
    
    fn get(&self) -> Option<&T> {
        self.data.as_ref().map(|d| d.get())
    }
}

// Usage
let mut navmesh_cache = FrameRetainer::new(RetentionPolicy::PromoteToPool);
if should_rebuild_navmesh {
    navmesh_cache.retain(&alloc, build_navmesh());
}
```

### The Promotion Pattern


Promote frequently used frame data to pool:

```rust
struct PromotionTracker<T> {
    frame_count: usize,
    threshold: usize,
    data: Option<T>,
}

impl<T> PromotionTracker<T> {
    fn update(&mut self, alloc: &SmartAlloc, data: T) {
        self.frame_count += 1;
        
        if self.frame_count >= self.threshold {
            // Promote to pool
            self.data = Some(alloc.pool_box(data).into_inner());
            self.frame_count = 0;
        }
    }
}
```

### The Cleanup Pattern


Explicit cleanup at specific points:

```rust
struct CleanupGuard<'a> {
    alloc: &'a SmartAlloc,
    cleanup_points: Vec<fn(&SmartAlloc)>,
}

impl<'a> CleanupGuard<'a> {
    fn new(alloc: &'a SmartAlloc) -> Self {
        Self {
            alloc,
            cleanup_points: Vec::new(),
        }
    }
    
    fn add_cleanup(&mut self, cleanup: fn(&SmartAlloc)) {
        self.cleanup_points.push(cleanup);
    }
}

impl<'a> Drop for CleanupGuard<'a> {
    fn drop(&mut self) {
        for cleanup in &self.cleanup_points {
            cleanup(self.alloc);
        }
    }
}
```

## Error Handling Patterns


### The Fallible Allocation Pattern


Handle allocation failures gracefully:

```rust
fn try_allocate_frame<T>(alloc: &SmartAlloc) -> Option<FrameBox<T>> {
    alloc.try_frame_alloc()
}

fn safe_frame_alloc<T>(alloc: &SmartAlloc, fallback: T) -> FrameBox<T> {
    alloc.try_frame_alloc().unwrap_or_else(|| {
        // Use fallback allocation strategy
        alloc.pool_box(fallback).into_frame_box()
    })
}
```

### The Recovery Pattern


Recover from allocation errors:

```rust
struct AllocationRecovery {
    fallback_strategy: Box<dyn Fn() -> RecoveryAction>,
}

enum RecoveryAction {
    Retry,
    UseFallback,
    Abort,
}

impl AllocationRecovery {
    fn handle_error<T>(&self, alloc: &SmartAlloc) -> Result<FrameBox<T>, AllocError> {
        match alloc.try_frame_alloc() {
            Ok(data) => Ok(data),
            Err(_) => match (self.fallback_strategy)() {
                RecoveryAction::Retry => self.handle_error(alloc),
                RecoveryAction::UseFallback => {
                    // Implement fallback
                    Err(AllocError::FallbackUsed)
                }
                RecoveryAction::Abort => Err(AllocError::OutOfMemory),
            },
        }
    }
}
```

## Best Practices Summary


1. **Use frame allocation for temporary data** - Reset automatically
2. **Pool for persistent small objects** - Automatic lifecycle
3. **Heap for large persistent data** - Still tracked
4. **Tag allocations by system** - Better organization
5. **Set budgets** - Prevent memory bloat
6. **Use TransferHandle for cross-thread** - Safe transfers
7. **Promote hot data** - Frame → Pool → Heap
8. **Handle allocation failures** - Graceful degradation

## Next Steps


- Read [Advanced Guide]advanced.md for deep internals
- Check [Performance Guide]performance.md for optimization
- Explore [Cookbook]cookbook.md for copy-paste recipes
- See domain-specific guides for your use case