Onsen provides a hot Pool for objects. In most cases allocation from this Pool is faster and offers better locality than the standard allocator. For small to medium sized objects the performance improvement is around 20% or better. For large objects the gains become smaller as caching effects even out.
An onsen pool allocated blocks with exponentially growing sizes. Allocations are served from these blocks. Freed entries are kept in a double linked cyclic freelist. This freelist is kept in weakly ordered and the entry point always point close to where the last action happend to keep the caches hot.
Onsen comes with its own Box and Rc/Weak implementations that wrap the underlying Pool in a safe way. A 'Sc' reference counted box without weak reference support is available as well and provides an advantage for small objects where the weak count would add some weight.
Allocating from a Pool returns Slot handles. These are lightweight abstractions to memory addresses, they do not keep a relation to the Pool they are allocated from. The rationale for this design is to make them usable in a VM that uses NaN tagging.
Slots are guarded by typestate policies which prevent some wrong use at compile time.
Because of this Slots need to be handled with care and certain contracts need to be enforced. The library provides some help to ensure correctness. Few things can not be asserted and are guarded by unsafe functions. Higher level API's (Such as Box, Rc and Sc above) can easily enforce these in a safe way.
pool.leak()
which drops a pool while leaking its memory blocks. This can be
used when one will never try to free memory obtained from that Pool.Onsen provides a singlethreaded Pool
which uses a RefCell
and a multithreaded TPool
which
use a Mutex. Additional features are gated with feature flags.
st_tbox is the default. This enables the most complete API with best performance.
Onsen pools are optimized for cache locality and by that to some extend for singlethreaded use. It is best to have one Pool per type per thread.
The TPool adds a Mutex to be used in multithreaded cases but its performance is rather poor. Still to reduce dependencies this is the default when using TBoxes.
Onsen uses criterion for benchmarking, since onsen is made for singlethreaded application its best to be tested when locked on a single CPU core and lock the core to some frequency well below the max to give more consistent results. At higher priority so it wont be disturbed as much from other programs. On Linux you may do something like:
shell,ignore
sudo renice -15 $$
sudo cpupower -c 1 frequenc-sety -f 2.8GHz
taskset 2 cargo bench
Will produce target/criterion/report/index.html
.