Struct 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§

Source§

impl<A> Cache<A>

Source

pub fn new() -> Self

Creates a new cache with no data.

Source

pub fn store(&mut self, val: A)

Stores data into the cache.

Source

pub fn take(&mut self) -> Option<A>

Takes the data from the cache.

Source

pub fn take_or<F>(&mut self, create: F) -> A
where F: FnOnce() -> A,

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

Trait Implementations§

Source§

impl<A: Debug> Debug for Cache<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A> Default for Cache<A>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.