Expand description
A thread-safe object pool collection with automatic return.
Some implementations are lockfree :
Other use std::Mutex :
And NoneObjectPool
basic allocation without pool.
§Example
The general pool creation looks like this for
use lockfree_object_pool::LinearObjectPool;
let pool = LinearObjectPool::<u32>::new(
|| Default::default(),
|v| {*v = 0; });
// And use the object pool
let mut item = pool.pull();
*item = 5;
At the end of the scope item return in object pool.
§Multithreading
All implementation support allocation/desallocation from on or more thread. You only need to wrap the pool in a std::sync::Arc
:
use lockfree_object_pool::LinearObjectPool;
use std::sync::Arc;
let pool = Arc::new(LinearObjectPool::<u32>::new(
|| Default::default(),
|v| {*v = 0; }));
§Performance
Global report.
§Allocation
ObjectPool | Duration in Monothreading (us) | Duration Multithreading (us) |
---|---|---|
NoneObjectPool | 1.2848 | 0.62509 |
MutexObjectPool | 1.3107 | 1.5178 |
SpinLockObjectPool | 1.3106 | 1.3684 |
LinearObjectPool | 0.23732 | 0.38913 |
[crate 'sharded-slab' ] | 1.6264 | 0.82607 |
[crate 'object-pool' ] | 0.77533 | 0.26224 |
Report monothreading and multithreading
§Forward Message between Thread
ObjectPool | 1 Reader - 1 Writter (ns) | 5 Reader - 1 Writter (ns) | 1 Reader - 5 Writter (ns) | 5 Reader - 5 Writter (ns) |
---|---|---|---|---|
NoneObjectPool | 529.75 | 290.47 | 926.05 | 722.35 |
MutexObjectPool | 429.29 | 207.17 | 909.88 | 409.99 |
SpinLockObjectPool | 34.277 | 182.62 | 1089.7 | 483.81 |
LinearObjectPool | 43.876 | 163.18 | 365.56 | 326.92 |
[crate 'sharded-slab' ] | 525.82 | 775.79 | 966.87 | 1289.2 |
Not supported by [crate 'object-pool'
]
§Desallocation
ObjectPool | Duration in Monothreading (ns) | Duration Multithreading (ns) |
---|---|---|
NoneObjectPool | 111.81 | 93.585 |
MutexObjectPool | 26.108 | 101.86 |
SpinLockObjectPool | 22.441 | 50.107 |
LinearObjectPool | 7.5379 | 41.707 |
[crate 'sharded-slab' ] | 7.0394 | 10.283 |
[crate 'object-pool' ] | 20.517 | 44.798 |
Report monothreading and multithreading
Structs§
- ObjectPool use a lockfree vector to secure multithread access to pull.
- Wrapper over T used by
LinearObjectPool
. - Wrapper over T used by
LinearObjectPool
. - ObjectPool use a
std::sync::Mutex
over vector to secure multithread access to pull. - Wrapper over T used by
MutexObjectPool
. - Wrapper over T used by
MutexObjectPool
. - Basic allocation without pull. Used to compare default rust allocation with different kind of object pool.
- Wrapper over T used by
NoneObjectPool
. - ObjectPool use a spin lock over vector to secure multithread access to pull.
- Wrapper over T used by
SpinLockObjectPool
. - Wrapper over T used by
SpinLockObjectPool
.