Remove archetype_component_access from QueryState#12474
Merged
alice-i-cecile merged 10 commits intobevyengine:mainfrom Mar 17, 2024
Merged
Remove archetype_component_access from QueryState#12474alice-i-cecile merged 10 commits intobevyengine:mainfrom
alice-i-cecile merged 10 commits intobevyengine:mainfrom
Conversation
joseph-gio
approved these changes
Mar 15, 2024
Member
joseph-gio
left a comment
There was a problem hiding this comment.
LGTM. I always appreciate these simple-in-hindsight optimizations with wide-reaching effects
I'd be interested to see if there's a measurable impact on our benchmarks, or if the savings are mainly for memory and cache usage
Member
Author
I don't really expect them to show up in the microbenchmarks since they all hit the ArchetypeGeneration check, come up empty, and return immediately. Might improve it a little bit by reducing instruction cache pressure a bit by having less codegen though. |
alice-i-cecile
approved these changes
Mar 17, 2024
github-merge-queue bot
pushed a commit
that referenced
this pull request
Mar 30, 2024
# Objective Other than the exposed functions for reading matched tables and archetypes, a `QueryState` does not actually need both internal Vecs for storing matched archetypes and tables. In practice, it will only use one of the two depending on if it uses dense or archetypal iteration. Same vein as #12474. The goal is to reduce the memory overhead of using queries, which Bevy itself, ecosystem plugins, and end users are already fairly liberally using. ## Solution Add `StorageId`, which is a union over `TableId` and `ArchetypeId`, and store only one of the two at runtime. Read the slice as if it was one ID depending on whether the query is dense or not. This follows in the same vein as #5085; however, this one directly impacts heap memory usage at runtime, while #5085 primarily targeted transient pointers that might not actually exist at runtime. --- ## Changelog Changed: `QueryState::matched_tables` now returns an iterator instead of a reference to a slice. Changed: `QueryState::matched_archetypes` now returns an iterator instead of a reference to a slice. ## Migration Guide `QueryState::matched_tables` and `QueryState::matched_archetypes` does not return a reference to a slice, but an iterator instead. You may need to use iterator combinators or collect them into a Vec to use it as a slice. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
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
QueryState::archetype_component_accessis only really ever used to extendSystemMeta's. It can be removed to save some memory for everyQueryin an app.Solution
new_archetypepass in a&mut Access<ArchetypeComponentId>instead and pull it fromSystemMetadirectly.QueryState::newfromQueryState::new_with_accessand a commonQueryState::new_uninitialized.new_archetypeinto an internal and public version. Call the internal version inupdate_archetypes.This should make it faster to construct new QueryStates, and by proxy lenses and joins as well.
matched_tablesalso similarly is only used to deduplicate inserting intomatched_table_ids. If we can find another efficient way to do so, it might also be worth removing.The generated assembly reflects this well, with all of the access related updates in
QueryStatebeing removed.Changelog
Removed:
QueryState::archetype_component_access.Changed:
QueryState::new_archetypenow takes a&mut Access<ArchetypeComponentId>argument, which will be updated with the new accesses.Changed:
QueryState::update_archetype_component_accessnow takes a&mut Access<ArchetypeComponentId>argument, which will be updated with the new accesses.Migration Guide
QueryState::archetype_component_accesshas been removed. This can be worked around by accessing the surroundingSystemState's instead. If you needed this explicitly forQueryState, please file an issue.