Struct owned_alloc::Cache

source ·
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

Creates a new cache with no data.

Stores data into the cache.

Takes the data from the cache.

Takes the data from the cache. If there was no data, the passed closure is called to produce the returned data.

Trait Implementations

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.