pub struct Cache<A> { /* private fields */ }
Expand description
A general purpouse cache suitable for saving discarted memory allocations in a tight loop.
§Dummy Example
extern crate owned_alloc;
use owned_alloc::{Cache, RawVec, UninitAlloc};
fn do_some_stuff(iter: usize, n: usize) -> usize {
let mut cache = Cache::new();
let mut res = 0usize;
for i in 1 ..= iter {
let alloc =
cache.take_or(|| UninitAlloc::from(RawVec::with_capacity(n)));
let inited = unsafe {
alloc.init_in_place(|slice| {
for j in 0 .. slice.len() {
(&mut slice[j] as *mut usize)
.write(i.wrapping_mul(j + 1))
}
})
};
for &item in &*inited {
res = res.wrapping_add(item);
}
cache.store(inited.drop_in_place());
}
res
}
assert_eq!(do_some_stuff(2, 3), 1 + 2 + 3 + 2 + 4 + 6);
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<A> Freeze for Cache<A>where
A: Freeze,
impl<A> RefUnwindSafe for Cache<A>where
A: RefUnwindSafe,
impl<A> Send for Cache<A>where
A: Send,
impl<A> Sync for Cache<A>where
A: Sync,
impl<A> Unpin for Cache<A>where
A: Unpin,
impl<A> UnwindSafe for Cache<A>where
A: UnwindSafe,
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
Mutably borrows from an owned value. Read more