fastalloc 1.5.0

High-performance memory pooling library with type-safe handles, predictable latency, and zero fragmentation. Perfect for game engines, real-time systems, and high-churn workloads.
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
//! Growing memory pool implementation.

use crate::allocator::{Allocator, FreeListAllocator};
use crate::config::PoolConfig;
use crate::error::{Error, Result};
use crate::handle::{OwnedHandle, PoolInterface};
use crate::traits::Poolable;
use alloc::vec;
use alloc::vec::Vec;
use core::cell::RefCell;
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use core::ptr;

#[cfg(feature = "stats")]
use crate::stats::PoolStatistics;

/// A memory pool that can grow dynamically based on demand.
///
/// This pool starts with an initial capacity and can grow according to
/// a configurable growth strategy when it runs out of space.
///
/// # Examples
///
/// ```rust
/// use fastalloc::{GrowingPool, PoolConfig, GrowthStrategy};
///
/// let config = PoolConfig::builder()
///     .capacity(100)
///     .max_capacity(Some(1000))
///     .growth_strategy(GrowthStrategy::Exponential { factor: 2.0 })
///     .build()
///     .unwrap();
///
/// let pool = GrowingPool::with_config(config).unwrap();
///
/// // Allocate objects - pool will grow as needed
/// let mut handles = Vec::new();
/// for i in 0..500 {
///     handles.push(pool.allocate(i).unwrap());
/// }
/// ```
///
/// # Performance
///
/// - Allocation: < 50ns per object (may spike during growth)
/// - Deallocation: < 15ns per object
/// - Growth causes temporary allocation spike
/// - Slight fragmentation possible with some growth strategies
pub struct GrowingPool<T> {
    /// Storage chunks
    storage: RefCell<Vec<Vec<MaybeUninit<T>>>>,
    /// Allocator for managing free slots
    allocator: RefCell<FreeListAllocator>,
    /// Current total capacity
    capacity: RefCell<usize>,
    /// Cumulative chunk sizes for fast O(log n) chunk lookup
    chunk_boundaries: RefCell<Vec<usize>>,
    /// Pool configuration
    config: PoolConfig<T>,
    /// Statistics collector
    #[cfg(feature = "stats")]
    stats: RefCell<crate::stats::StatisticsCollector>,
    /// Marker for lifetime and Send/Sync bounds
    _marker: PhantomData<T>,
}

impl<T: Poolable> GrowingPool<T> {
    /// Creates a new growing pool with the specified configuration.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fastalloc::{GrowingPool, PoolConfig, GrowthStrategy};
    ///
    /// let config: PoolConfig<i32> = PoolConfig::builder()
    ///     .capacity(100)
    ///     .growth_strategy(GrowthStrategy::Linear { amount: 50 })
    ///     .build()
    ///     .unwrap();
    ///
    /// let pool = GrowingPool::with_config(config).unwrap();
    /// ```
    pub fn with_config(config: PoolConfig<T>) -> Result<Self> {
        let capacity = config.capacity();

        // Allocate initial storage chunk
        let mut storage_chunk = Vec::with_capacity(capacity);
        storage_chunk.resize_with(capacity, MaybeUninit::uninit);

        let storage = vec![storage_chunk];

        let pool = Self {
            storage: RefCell::new(storage),
            allocator: RefCell::new(FreeListAllocator::new(capacity)),
            capacity: RefCell::new(capacity),
            chunk_boundaries: RefCell::new(vec![capacity]),
            config,
            #[cfg(feature = "stats")]
            stats: RefCell::new(crate::stats::StatisticsCollector::new(capacity)),
            _marker: PhantomData,
        };

        Ok(pool)
    }

    /// Grows the pool by allocating an additional chunk of memory.
    fn grow(&self) -> Result<()> {
        let growth_amount = self
            .config
            .growth_strategy()
            .compute_growth(*self.capacity.borrow());

        if growth_amount == 0 {
            return Err(Error::PoolExhausted {
                capacity: *self.capacity.borrow(),
                allocated: *self.capacity.borrow() - self.allocator.borrow().available(),
            });
        }

        let current_capacity = *self.capacity.borrow();
        let new_capacity = current_capacity + growth_amount;

        // Check max capacity constraint
        if let Some(max) = self.config.max_capacity() {
            if new_capacity > max {
                return Err(Error::MaxCapacityExceeded {
                    current: current_capacity,
                    requested: new_capacity,
                    max,
                });
            }
        }

        // Allocate new storage chunk
        let mut new_chunk = Vec::with_capacity(growth_amount);
        new_chunk.resize_with(growth_amount, MaybeUninit::uninit);

        self.storage.borrow_mut().push(new_chunk);
        self.allocator.borrow_mut().extend(growth_amount);
        *self.capacity.borrow_mut() = new_capacity;
        self.chunk_boundaries.borrow_mut().push(new_capacity);

        #[cfg(feature = "stats")]
        self.stats.borrow_mut().record_growth(new_capacity);

        Ok(())
    }

    /// Allocates an object from the pool with the given initial value.
    ///
    /// If the pool is full, it will attempt to grow according to its growth strategy.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fastalloc::{GrowingPool, PoolConfig, GrowthStrategy};
    ///
    /// let config = PoolConfig::builder()
    ///     .capacity(2)
    ///     .growth_strategy(GrowthStrategy::Linear { amount: 2 })
    ///     .build()
    ///     .unwrap();
    ///
    /// let pool = GrowingPool::with_config(config).unwrap();
    ///
    /// let h1 = pool.allocate(1).unwrap();
    /// let h2 = pool.allocate(2).unwrap();
    /// // Pool will grow automatically
    /// let h3 = pool.allocate(3).unwrap();
    /// ```
    pub fn allocate(&self, mut value: T) -> Result<OwnedHandle<'_, T>> {
        // Try to allocate a slot
        let index = {
            let mut allocator = self.allocator.borrow_mut();
            if let Some(idx) = allocator.allocate() {
                idx
            } else {
                // Drop the borrow before growing
                drop(allocator);

                // Pool is full, try to grow
                self.grow()?;

                // Try again after growth
                self.allocator
                    .borrow_mut()
                    .allocate()
                    .ok_or_else(|| Error::PoolExhausted {
                        capacity: *self.capacity.borrow(),
                        allocated: *self.capacity.borrow(),
                    })?
            }
        };

        #[cfg(feature = "stats")]
        self.stats.borrow_mut().record_allocation();

        // Call on_acquire hook
        value.on_acquire();

        // Find which chunk and offset, then write the value
        {
            let mut storage = self.storage.borrow_mut();
            let mut remaining = index;
            let mut found = false;

            for chunk in storage.iter_mut() {
                if remaining < chunk.len() {
                    chunk[remaining].write(value);
                    found = true;
                    break;
                }
                remaining -= chunk.len();
            }

            if !found {
                panic!("Index out of bounds: {}", index);
            }
        }

        Ok(OwnedHandle::new(self, index))
    }

    /// Internal allocation method that returns just the index.
    ///
    /// This is used by thread-safe wrappers to allocate without creating a handle.
    pub(crate) fn allocate_internal(&mut self, mut value: T) -> Result<usize> {
        // Try to allocate a slot
        let index = {
            let mut allocator = self.allocator.borrow_mut();
            if let Some(idx) = allocator.allocate() {
                idx
            } else {
                // Drop the borrow before growing
                drop(allocator);

                // Pool is full, try to grow
                self.grow()?;

                // Try again after growth
                self.allocator
                    .borrow_mut()
                    .allocate()
                    .ok_or_else(|| Error::PoolExhausted {
                        capacity: *self.capacity.borrow(),
                        allocated: *self.capacity.borrow(),
                    })?
            }
        };

        #[cfg(feature = "stats")]
        self.stats.borrow_mut().record_allocation();

        // Call on_acquire hook
        value.on_acquire();

        // Find which chunk and offset, then write the value
        {
            let mut storage = self.storage.borrow_mut();
            let mut remaining = index;
            let mut found = false;

            for chunk in storage.iter_mut() {
                if remaining < chunk.len() {
                    chunk[remaining].write(value);
                    found = true;
                    break;
                }
                remaining -= chunk.len();
            }

            if !found {
                panic!("Index out of bounds: {}", index);
            }
        }

        Ok(index)
    }

    /// Converts a flat index to chunk index and offset within that chunk.
    /// Returns (chunk_index, offset_within_chunk)
    /// Uses cached chunk boundaries for fast O(log n) binary search lookup.
    #[inline]
    fn compute_chunk_location(&self, index: usize) -> (usize, usize) {
        let boundaries = self.chunk_boundaries.borrow();

        // Binary search to find the chunk
        let chunk_idx = match boundaries.binary_search(&(index + 1)) {
            Ok(idx) => idx,
            Err(idx) => idx,
        };

        // Compute offset within chunk
        let offset = if chunk_idx == 0 {
            index
        } else {
            index - boundaries[chunk_idx - 1]
        };

        (chunk_idx, offset)
    }

    /// Returns the total capacity of the pool.
    #[inline]
    pub fn capacity(&self) -> usize {
        *self.capacity.borrow()
    }

    /// Returns the number of available (free) slots in the pool.
    #[inline]
    pub fn available(&self) -> usize {
        self.allocator.borrow().available()
    }

    /// Returns the number of currently allocated objects.
    #[inline]
    pub fn allocated(&self) -> usize {
        self.capacity() - self.available()
    }

    /// Returns whether the pool is full (no available slots and cannot grow).
    #[inline]
    pub fn is_full(&self) -> bool {
        self.allocator.borrow().is_full() && !self.can_grow()
    }

    /// Returns whether the pool is empty (all slots available).
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.allocator.borrow().is_empty()
    }

    /// Returns whether the pool can grow further.
    #[inline]
    pub fn can_grow(&self) -> bool {
        if !self.config.growth_strategy().allows_growth() {
            return false;
        }

        if let Some(max) = self.config.max_capacity() {
            self.capacity() < max
        } else {
            true
        }
    }

    /// Gets a reference to an object at the given index.
    ///
    /// # Safety
    ///
    /// This is internal and should only be called with valid allocated indices.
    #[inline]
    pub(crate) fn get(&self, index: usize) -> &T {
        let (chunk_idx, offset) = self.compute_chunk_location(index);
        let storage = self.storage.borrow();
        // Safety: index is valid and initialized by allocate()
        // We extend the lifetime beyond the borrow - safe because pool owns the data
        unsafe {
            let ptr = storage.as_ptr();
            let chunk = &*ptr.add(chunk_idx);
            &*chunk.as_ptr().add(offset).cast::<T>()
        }
    }

    /// Gets a mutable reference to an object at the given index.
    ///
    /// # Safety
    ///
    /// This is internal and should only be called with valid allocated indices.
    #[inline]
    #[allow(clippy::mut_from_ref)]
    pub(crate) fn get_mut(&self, index: usize) -> &mut T {
        let (chunk_idx, offset) = self.compute_chunk_location(index);
        let storage = self.storage.borrow_mut();
        // Safety: index is valid and initialized by allocate()
        // We extend the lifetime beyond the borrow - safe because pool owns the data
        unsafe {
            let ptr = storage.as_ptr() as *mut Vec<MaybeUninit<T>>;
            let chunk = &mut *ptr.add(chunk_idx);
            &mut *chunk.as_mut_ptr().add(offset).cast::<T>()
        }
    }

    /// Returns an object to the pool.
    pub(crate) fn return_to_pool(&self, index: usize) {
        let (chunk_idx, offset) = self.compute_chunk_location(index);

        // Get the value and call on_release
        let mut storage = self.storage.borrow_mut();

        unsafe {
            let value_ptr = storage[chunk_idx][offset].as_mut_ptr();
            (*value_ptr).on_release();
            ptr::drop_in_place(value_ptr);
        }

        // Mark the slot as free
        self.allocator.borrow_mut().free(index);

        #[cfg(feature = "stats")]
        self.stats.borrow_mut().record_deallocation();
    }

    /// Get current pool statistics.
    #[cfg(feature = "stats")]
    #[cfg_attr(docsrs, doc(cfg(feature = "stats")))]
    pub fn statistics(&self) -> PoolStatistics {
        let mut stats = self.stats.borrow().snapshot();
        stats.current_usage = self.allocated();
        stats.capacity = self.capacity();
        stats
    }

    /// Reset statistics counters.
    #[cfg(feature = "stats")]
    #[cfg_attr(docsrs, doc(cfg(feature = "stats")))]
    pub fn reset_statistics(&self) {
        self.stats.borrow_mut().reset();
    }
}

impl<T: Poolable> PoolInterface<T> for GrowingPool<T> {
    #[inline]
    fn get(&self, index: usize) -> &T {
        self.get(index)
    }

    #[inline]
    fn get_mut(&self, index: usize) -> &mut T {
        self.get_mut(index)
    }

    #[inline]
    fn return_to_pool(&self, index: usize) {
        self.return_to_pool(index)
    }
}

unsafe impl<T: Send> Send for GrowingPool<T> {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::GrowthStrategy;

    #[test]
    fn new_pool() {
        let config = PoolConfig::builder()
            .capacity(100)
            .growth_strategy(GrowthStrategy::Linear { amount: 50 })
            .build()
            .unwrap();

        let pool = GrowingPool::<i32>::with_config(config).unwrap();
        assert_eq!(pool.capacity(), 100);
        assert_eq!(pool.available(), 100);
    }

    #[test]
    fn pool_grows_on_demand() {
        let config = PoolConfig::builder()
            .capacity(2)
            .growth_strategy(GrowthStrategy::Linear { amount: 2 })
            .build()
            .unwrap();

        let pool = GrowingPool::with_config(config).unwrap();

        let _h1 = pool.allocate(1).unwrap();
        let _h2 = pool.allocate(2).unwrap();
        assert_eq!(pool.capacity(), 2);

        // This should trigger growth
        let _h3 = pool.allocate(3).unwrap();
        assert_eq!(pool.capacity(), 4);
    }

    #[test]
    fn respects_max_capacity() {
        let config = PoolConfig::builder()
            .capacity(2)
            .max_capacity(Some(4))
            .growth_strategy(GrowthStrategy::Linear { amount: 2 })
            .build()
            .unwrap();

        let pool = GrowingPool::with_config(config).unwrap();

        let _h1 = pool.allocate(1).unwrap();
        let _h2 = pool.allocate(2).unwrap();
        let _h3 = pool.allocate(3).unwrap(); // Grows to 4
        let _h4 = pool.allocate(4).unwrap();

        assert_eq!(pool.capacity(), 4);

        // Should fail - cannot grow beyond max
        let result = pool.allocate(5);
        assert!(matches!(result, Err(Error::MaxCapacityExceeded { .. })));
    }
}