object-pool 0.2.0

A thread-safe object pool with automatic return and attach/detach semantics
Documentation

Object Pool

License Cargo Documentation Rust 1.34+

This is nighly only as of 1.32 stable

A thread-safe object pool with automatic return and attach/detach semantics.

The goal of an object pool is to reuse expensive to allocate objects or frequently allocated objects Common use case is when using buffer to read IO.

You would create a pool of size n, containing Vec<u8> that can be used to call something like file.read_to_end(buff).

Usage

[dependencies]
object-pool = "0.1"
extern crate object_pool;

Basic usage for pulling from the pool

let pool: Pool<Vec<u8>> = Pool::new(32, || Vec::with_capacity(4096));
let mut reusable_buff = pool.pull().unwrap();
reusable_buff.clear();
some_file.read_to_end(reusable_buff.deref_mut());
//reusable_buff falls out of scope and is returned to the pool

For access across multiple threads simply wrap the pool in an Arc

let pool: Arc<Pool<T>> = Pool::new(cap, || T::new());

Check out the docs for more info

Performance

Here are some performance results from the benches

test tests::bench_alloc_128k       ... bench:          18 ns/iter (+/- 0)
test tests::bench_alloc_1g         ... bench:       4,819 ns/iter (+/- 1,665)
test tests::bench_pull_128k        ... bench:          13 ns/iter (+/- 0)
test tests::bench_pull_1g          ... bench:          13 ns/iter (+/- 0)
test tests::bench_pull_detach_128k ... bench:          14 ns/iter (+/- 0)
test tests::bench_pull_detach_1g   ... bench:          14 ns/iter (+/- 0)

these are not scientific in the slightest, free feel to open an issue about how I can improve these