Change ResourceEntities from SparseSet to SparseArray to speed up resource lookups#23616
Open
chescock wants to merge 2 commits intobevyengine:mainfrom
Open
Change ResourceEntities from SparseSet to SparseArray to speed up resource lookups#23616chescock wants to merge 2 commits intobevyengine:mainfrom
ResourceEntities from SparseSet to SparseArray to speed up resource lookups#23616chescock wants to merge 2 commits intobevyengine:mainfrom
Conversation
…up resource lookups.
alice-i-cecile
approved these changes
Apr 1, 2026
Member
alice-i-cecile
left a comment
There was a problem hiding this comment.
This makes sense to me, and given that perf characteristics are otherwise kind of a wash we might as well save a bit of memory.
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.
Objective
Reduce memory usage for resources, and maybe improve performance of resource lookups.
Related to #23039, but not a solution.
Solution
Change
ResourceEntitiesfrom aSparseSetto aSparseArray.SparseArrayispub (crate), so simply exposing it throughDerefwould cause privacy errors. Instead, remove theDerefimpl and add wrapper methods foriter,get, andremove. Change the return types from&EntitytoEntitynow that they aren't generic.As background: A
SparseArrayis a simpleVec<Option<V>>, while aSparseSetis aSparseArraythat maps keys to dense indexes, combined with dense arrays of keys and values. That requires a second array operation to find the actual value, but can be much better for memory usage when the values are large, since missing items only take up space for a single index instead of an entire value.But the values in
ResourceEntitiesareEntity, which are already small! ASparseArraywill always be smaller on 64-bit systems, since anEntityis the same size as ausize, and we don't need to store the additionaldenseandindicesarrays. So switching toSparseArraywill save a lookup and save memory.One drawback is that we can no longer use the dense lists to iterate all resources, so methods like
iter_resourcesnow need to scan all component ids. I don't expect this to be a problem in practice, though.iter_resourcesis rarely used, and O(components) isn't all that much worse than O(resources). If it turns out to be an issue, it's also possible to recover this data by querying theIsResourcecomponent.Testing
Inconclusive.
I attempted to run benchmarks, both
bevymarkas in the linked issue andcargo bench -p benches --bench ecs, but the results were too noisy on my machine to reach any conclusions. And now that I look more closely, we don't have many benches that even use resources!