[][src]Crate remem

Thread-safe object reuse

In many programs the allocator can be a bottleneck, especially when allocating larger structures in a short amount of time. The allocator will have to spend time finding free memory to fit new allocations, and defragmenting memory to free up new space for new ones.

However often we'll be allocating in a loop, and right after we drop an object we'll want a new object of the same size. This is where remem comes in: it allows you to reuse memory in a thread-safe way.

This is useful when writing networked services, performing file reads, or anything else that might allocate a lot. Internally it's implemented using a "Treiber stack" which is a really fast algorithm that makes remem safe to use between threads!


use remem::Pool;
use std::thread;

let p = Pool::new(|| vec![0usize; 1024]);

// Create a new handle onto the pool and send it to a new thread.
let p2 = p.clone();
let t = thread::spawn(move || {
    // Get a new vec from the pool and push two values into it.
    let mut v = p2.get();

// Meanwhile we can still access the original handle from the main
// thread and use it to get new vecs from.
let mut v = p.get();

// Wait for the other thread to complete

// When the vec is dropped, it's returned to the pool and is ready to be
// used again from a next call to `p.get()`.



RAII structure used to reintroduce an item into the pool when dropped.


A pool of reusable memory.