MTB::Entity: An address-stable, interior-mutable Slab allocator
中文版请见: README-zh.md
⚠️ Notes
- This allocator is experimental; its API may change significantly.
- It has not been thoroughly tested and may contain memory-safety issues. Use with care.
- It currently supports single-threaded use only, with no plans for multithreading support.
- The recommended alternative is the
slabcrate — it’s superior in both performance and safety in general. If you don’t specifically need interior mutability, preferslab.
Introduction
The motivation for this allocator came while building Remusys. The slab crate’s Slab requires a mutable reference to allocate elements, which complicated some of my optimizations. This chunked, address-stable allocator lets you allocate new elements while reading existing ones:
use ;
/// Not every type can be stored in this allocator. You need to implement the
/// `IEntityAllocatable` trait for your type.
Basic type hierarchy
EntityAlloc— The allocator itself; manages all chunks and the allocation/freeing of elements.IEntityAllocID<E>— A trait for converting betweenPtrIDandIndexedID.PtrID<E>— An ID pointing to an element inside the allocator, internally a raw pointer. Fast but unsafe.IndexedID<E>— An ID pointing to an element, represented by a pair of chunk index and in-chunk index. Safe but much slower.IDProxy<'alloc, E>— A proxy used for two-phase initialization of a target element.
Allocation policies
The allocator supports multiple allocation strategies to fit different scenarios. Not every type can be stored in the allocator; your type must implement IEntityAllocatable and specify a policy. Available policies:
EntityAllocPolicy128<E>— 128 elements per chunk (medium/small chunks, single-level bitmap).EntityAllocPolicy256<E>— 256 elements per chunk (medium/small chunks, single-level bitmap).EntityAllocPolicy512<E>— 512 elements per chunk (medium/small chunks, single-level bitmap).EntityAllocPolicy1024<E>— 1024 elements per chunk (large chunks, two-level bitmap).EntityAllocPolicy2048<E>— 2048 elements per chunk (large chunks, two-level bitmap).EntityAllocPolicy4096<E>— 4096 elements per chunk (large chunks, two-level bitmap).
Here’s the boilerplate for making a type allocatable, as in the example above:
use ;
Containers
Some container types are built on top of EntityAlloc for convenience:
EntityList<E>— A doubly-linked list of entities. Internally mutable; modifying the list does not requireEntityAllocto be mutable.EntityRingList<E>— A doubly-linked ring list of entities. Internally mutable; modifying the list does not requireEntityAllocto be mutable.