#atomic #garbage #non-blocking #lock-free

object-pool

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

15 unstable releases (3 breaking)

✓ Uses Rust 2018 edition

new 0.4.3 Oct 8, 2019
0.4.2 Oct 8, 2019
0.3.1 Feb 6, 2019
0.2.3 Feb 3, 2019
0.1.6 Feb 2, 2019

#26 in Memory management

Download history 299/week @ 2019-06-25 279/week @ 2019-07-02 130/week @ 2019-07-09 126/week @ 2019-07-16 96/week @ 2019-07-23 147/week @ 2019-07-30 154/week @ 2019-08-06 189/week @ 2019-08-13 218/week @ 2019-08-20 152/week @ 2019-08-27 261/week @ 2019-09-03 183/week @ 2019-09-10 264/week @ 2019-09-17 187/week @ 2019-09-24 140/week @ 2019-10-01

749 downloads per month

MIT/Apache

4MB
119 lines

Object Pool

License Cargo Documentation

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.3"
extern crate object_pool;

Basic usage

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);
//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 examples

Performance

The benchmarks compare alloc() vs pool.pull() vs pool.detach() vs lifeguard vs WIP SyncPool.

Check out the results

For those who don't like graphs, here's the raw output

Dependencies