Factor the slab allocator out of the mesh allocator, and make it generic.#23232
Open
pcwalton wants to merge 1 commit intobevyengine:mainfrom
Open
Factor the slab allocator out of the mesh allocator, and make it generic.#23232pcwalton wants to merge 1 commit intobevyengine:mainfrom
pcwalton wants to merge 1 commit intobevyengine:mainfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
MeshAllocatorinto a new genericSlabAllocatortype.The refactored slab allocator is driven by a new trait,
SlabItem. ImplementingSlabItemrequires 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 associatedSlabItem::Layouttype.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_allocationto retrieve anAllocationStageobject, callsAllocationStage::allocaterepeatedly to perform all necessary allocations, and finally callscommit; slab reallocation happens efficiently under the hood. Likewise, to perform batched deallocation, the client code callsstage_deallocationto create aDeallocationStageobject, callsDeallocationStage::freeto free all necessary objects, and then callscommitto 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.