pub struct QueryArena { /* private fields */ }Expand description
Query-lifetime arena allocator
Provides fast bump-pointer allocation for temporary buffers during query execution. All allocations are freed together when the arena is dropped (end of query).
§Thread Safety
QueryArena is NOT thread-safe (no Sync/Send). Each thread should have its own
arena for parallel query execution. Use thread_local! or pass per-thread.
§Examples
// Create arena at query start
let arena = QueryArena::new();
// Allocate filter bitmap (fast O(1) bump allocation)
let bitmap = arena.alloc_bitmap(10000);
for i in 0..10000 {
bitmap[i] = evaluate_predicate(i);
}
// Allocate temp buffer for expression results
let results = arena.alloc_values(10000);
for i in 0..10000 {
results[i] = evaluate_expression(i);
}
// Arena automatically freed at end of query (drop)Implementations§
Source§impl QueryArena
impl QueryArena
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new query arena with default capacity (1 MB)
The arena pre-allocates a large chunk to serve many small allocations without frequent system calls.
Sourcepub fn with_capacity(bytes: usize) -> Self
pub fn with_capacity(bytes: usize) -> Self
Create a new query arena with specified initial capacity
Use this if you have a good estimate of memory needs:
- Small queries (< 10K rows): 128 KB
- Medium queries (10K-100K rows): 1 MB
- Large queries (> 100K rows): 10 MB
Arena will grow automatically if needed.
Sourcepub fn alloc_bitmap(&self, len: usize) -> &mut [bool]
pub fn alloc_bitmap(&self, len: usize) -> &mut [bool]
Allocate a boolean bitmap with specified length
Common use: Filter bitmaps marking rows that pass predicates
§Example
let bitmap = arena.alloc_bitmap(1000);
for i in 0..1000 {
bitmap[i] = row_passes_filter(i);
}Sourcepub fn alloc_bitmap_filled(&self, len: usize, value: bool) -> &mut [bool]
pub fn alloc_bitmap_filled(&self, len: usize, value: bool) -> &mut [bool]
Allocate a bitmap and initialize with a specific value
More efficient than alloc_bitmap + fill for large bitmaps.
§Example
// All rows pass by default, then mark failures
let bitmap = arena.alloc_bitmap_filled(1000, true);
for i in failing_rows {
bitmap[i] = false;
}Sourcepub fn alloc_slice<T: Default + Copy>(&self, len: usize) -> &mut [T]
pub fn alloc_slice<T: Default + Copy>(&self, len: usize) -> &mut [T]
Allocate an array of values
Use for temporary expression results, intermediate calculations, etc.
§Example
let temps: &mut [f64] = arena.alloc_slice(1000);
for i in 0..1000 {
temps[i] = calculate(i);
}Sourcepub fn alloc_slice_filled<T: Copy>(&self, len: usize, value: T) -> &mut [T]
pub fn alloc_slice_filled<T: Copy>(&self, len: usize, value: T) -> &mut [T]
Allocate an array and initialize with a specific value
Sourcepub fn alloc<T>(&self, value: T) -> &mut T
pub fn alloc<T>(&self, value: T) -> &mut T
Allocate a single value
Use sparingly - arena allocation is optimized for bulk allocations. For single values, normal heap allocation may be comparable.
Sourcepub fn alloc_vec<T>(&self) -> Vec<'_, T>
pub fn alloc_vec<T>(&self) -> Vec<'_, T>
Allocate a Vec-like collection with specified capacity
The returned Vec will allocate from the arena’s chunk, not the heap.
§Example
let mut results = arena.alloc_vec();
for row in rows {
if predicate(row) {
results.push(row);
}
}Sourcepub fn alloc_vec_with_capacity<T>(&self, capacity: usize) -> Vec<'_, T>
pub fn alloc_vec_with_capacity<T>(&self, capacity: usize) -> Vec<'_, T>
Allocate a Vec with specified capacity
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the arena, freeing all allocations at once
This is much faster than freeing individual allocations. Use between queries when reusing the same arena.
§Safety
All references to arena-allocated data become invalid after reset. The borrow checker prevents this at compile time for normal usage.
§Example
let arena = QueryArena::new();
for query in queries {
let bitmap = arena.alloc_bitmap(1000);
// ... execute query ...
arena.reset(); // Free all at once for next query
}Sourcepub fn allocated_bytes(&self) -> usize
pub fn allocated_bytes(&self) -> usize
Get the number of bytes allocated from the arena
Useful for profiling and capacity tuning.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for QueryArena
impl !RefUnwindSafe for QueryArena
impl Send for QueryArena
impl !Sync for QueryArena
impl Unpin for QueryArena
impl !UnwindSafe for QueryArena
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more