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

object-pool

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

16 unstable releases (3 breaking)

✓ Uses Rust 2018 edition

0.4.4 Oct 29, 2019
0.4.3 Oct 8, 2019
0.3.1 Feb 6, 2019
0.2.3 Feb 3, 2019
0.1.6 Feb 2, 2019

#23 in Memory management

Download history 251/week @ 2019-11-07 307/week @ 2019-11-14 341/week @ 2019-11-21 649/week @ 2019-11-28 457/week @ 2019-12-05 1034/week @ 2019-12-12 200/week @ 2019-12-19 296/week @ 2019-12-26 223/week @ 2020-01-02 609/week @ 2020-01-09 225/week @ 2020-01-16 465/week @ 2020-01-23 513/week @ 2020-01-30 380/week @ 2020-02-06 414/week @ 2020-02-13

1,555 downloads per month
Used in 3 crates (2 directly)

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.4"
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<'a, Vec<u8>>> = Pool::new(32, || Vec::with_capacity(4096));

Sending pooled resources across threads is possible, but requires the pool's lifetime be static

lazy_static! {
    static ref POOL: Arc<Pool<'static, Vec<u8>>> = Arc::new(Pool::new(32, || Vec::with_capacity(4096)));
}

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