Concurrent Pool
A concurrent object pool based on Crossbeam Queue
Features
- Configurable capacity and preallocation.
- Thread-safe: Multiple threads can pull and recycle items concurrently.
- Automatic return of dropped items to the pool for reuse.
- Automatic reclamation of unused item when the continuous occurrence
of
surplus-pullreaches a certain threshold ifauto_reclaimis enabled.
surplus-pull: After pulling data from the memory pool, available allocated
entities in the memory pool are exceed a certain threshold. We call this pull
is a surplus-pull.
Examples
Local memory pool
use Pool;
let pool: = with_capacity;
assert_eq!;
let item = pool.pull.unwrap;
assert_eq!;
assert_eq!;
let item_clone = item.clone;
drop;
assert_eq!;
drop;
assert_eq!;
Multiple threads shared memory pool
use ;
use ;
let mut builder = new;
let pool: = new;
let = channel;
let clone_pool = pool.clone;
let tx1 = tx.clone;
let sender1 = spawn;
let clone_pool = pool.clone;
let sender2 = spawn;
let receiver = spawn;
sender1.join.unwrap;
sender2.join.unwrap;
receiver.join.unwrap;
Performance
These benchmarks are based on sharded-slab repository.
The first shows the results of a benchmark where an increasing number of
items are inserted and then removed into a slab concurrently by five
threads. It compares the performance of the concurrent pool implementation
with a sharded-slab and a RwLock<slab::Slab>:
The second graph shows the results of a benchmark where an increasing
number of items are inserted and then removed by a single thread. It
compares the performance of the concurrent pool with a sharded-slab,
an RwLock<slab::Slab> and a mut slab::Slab.
The benchmarks show that concurrent pool performs worse than slab in single-threaded scenarios, but significantly outperforms sharded slab. In multi-threaded scenarios, it is clearly better than slab, and only slightly worse than sharded slab.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.