Skip to content

Factor the slab allocator out of the mesh allocator, and make it generic.#23232

Open
pcwalton wants to merge 1 commit intobevyengine:mainfrom
pcwalton:factor-out-allocator
Open

Factor the slab allocator out of the mesh allocator, and make it generic.#23232
pcwalton wants to merge 1 commit intobevyengine:mainfrom
pcwalton:factor-out-allocator

Conversation

@pcwalton
Copy link
Contributor

@pcwalton pcwalton commented Mar 5, 2026

The mesh allocator has grown fairly sophisticated, with an efficient O(1) allocation strategy, the ability to manage heterogeneous slabs, batched allocation and deallocation, and so on. However, it's currently tied to meshes. In order to scale to millions of mesh instances, I want to allocate bins using the same strategy. So, in preparation for these bin slabs, this commit factors out the generic allocation logic in MeshAllocator into a new generic SlabAllocator type.

The refactored slab allocator is driven by a new trait, SlabItem. Implementing SlabItem requires specifying the size of the elements that are to be stored in the slabs, as well as the type of the key used to retrieve those items. A single slab allocator can be directed to manage heterogeneous slabs by implementing the associated SlabItem::Layout type.

This PR also refactors slab allocation and deallocation to be explicitly transactional. This means that users of the allocator no longer need to have special logic to deallocate empty slabs and reallocate slabs that changed in size. To perform batched allocation, code that uses the allocator calls stage_allocation to retrieve an AllocationStage object, calls AllocationStage::allocate repeatedly to perform all necessary allocations, and finally calls commit; slab reallocation happens efficiently under the hood. Likewise, to perform batched deallocation, the client code calls stage_deallocation to create a DeallocationStage object, calls DeallocationStage::free to free all necessary objects, and then calls commit to free slabs that are have become empty.

Along with the forthcoming PR to add SparseBufferVec, this PR is a prerequisite to bin slabs. Moreover, it makes the mesh allocation code considerably cleaner and easier to understand.

generic.

The mesh allocator has grown fairly sophisticated, with an efficient
O(1) allocation strategy, the ability to manage heterogeneous slabs,
batched allocation and deallocation, and so on. However, it's currently
tied to meshes. In order to scale to millions of mesh instances, I want
to allocate bins using the same strategy. So, in preparation for these
bin slabs, this commit factors out the generic allocation logic in
`MeshAllocator` into a new generic `SlabAllocator` type.

The refactored slab allocator is driven by a new trait, `SlabItem`.
Implementing `SlabItem` requires specifying the size of the elements
that are to be stored in the slabs, as well as the type of the key used
to retrieve those items. A single slab allocator can be directed to
manage heterogeneous slabs by implementing the associated
`SlabItem::Layout` type.

This PR also refactors slab allocation and deallocation to be explicitly
transactional. This means that users of the allocator no longer need to
have special logic to deallocate empty slabs and reallocate slabs that
changed in size. To perform batched allocation, code that uses the
allocator calls `stage_allocation` to retrieve an `AllocationStage`
object, calls `AllocationStage::allocate` repeatedly to perform all
necessary allocations, and finally calls `commit`; slab reallocation
happens efficiently under the hood. Likewise, to perform batched
deallocation, the client code calls `stage_deallocation` to create a
`DeallocationStage` object, calls `DeallocationStage::free` to free all
necessary objects, and then calls `commit` to free slabs that are have
become empty.

Along with the forthcoming PR to add `SparseBufferVec`, this PR is a
prerequisite to bin slabs. Moreover, it makes the mesh allocation code
considerably cleaner and easier to understand.
@pcwalton pcwalton requested review from atlv24 and tychedelia March 5, 2026 17:53
@pcwalton pcwalton added the A-Rendering Drawing game state to the screen label Mar 5, 2026
@github-project-automation github-project-automation bot moved this to Needs SME Triage in Rendering Mar 5, 2026
@pcwalton pcwalton added C-Code-Quality A section of code that is hard to understand or change S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Code-Quality A section of code that is hard to understand or change S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

1 participant