Module veryfast::pool
[−]
[src]
A heap allocator that gives ownership of the value like a Box
, but allocates in batches.
Pool
allocates objects on the heap in batches.
All objects must be of the same type like in Vec
.
It allows fast multithreaded parallel allocation of objects.
If the Pool
runs out of memory, it will allocate more but will not move the old values
from their place.
When objects are dropped, their memory is returned to the pool to be reused for
future allocations.
Only when all the objects and the Pool
are dropped will the memory be released.
It gives the option to allocate each object on a separate CPU cache line, increasing performance of multithreaded access to adjacent elements.
The Object
class has exclusive ownership of the value contained within. When dropped, the
owned object will be dropped as well. The memory, however, will be returned to the Pool
it
was allocated from to be available for other allocations.
Examples
use veryfast::pool::{Pool, Object}; let pool = Pool::new(); let var1 = pool.push(15i32); let mut var2 = pool.push(7); *var2 = *var1; assert_eq!(*var1, *var2); let mut vec = Vec::new(); for i in 0..10 { vec.push(pool.push(i)); } for i in &vec { print!("{} ", **i); }
An example using a scoped thread pool:
extern crate scoped_threadpool; fn slow(val: &mut i32) { *val += 1; } let mut thread_pool = scoped_threadpool::Pool::new(4); let memory_pool = veryfast::pool::Pool::with_params(true); let mut vec = Vec::new(); for i in 0..10 { vec.push(memory_pool.push(i)); } thread_pool.scoped(|scoped| { for e in &mut vec { scoped.execute(move || { slow(&mut **e); }); } }); for i in 0..10 { assert_eq!(*vec[i], vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10][i]); }
Structs
Object |
A pointer type that owns its content. |
Pool |
A fast heap-allocator. Allocates objects in a batch, but transfers the ownership to the |