Make the pointers in Fetch impls unions#5085
Closed
james7132 wants to merge 30 commits intobevyengine:mainfrom
Closed
Make the pointers in Fetch impls unions#5085james7132 wants to merge 30 commits intobevyengine:mainfrom
james7132 wants to merge 30 commits intobevyengine:mainfrom
Conversation
Contributor
|
This was discussed on discord at https://discord.com/channels/691052431525675048/749335865876021248/989568732449865809 |
Contributor
|
Some suggested changes james7132#4 |
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
Every
Fetchstruct initializes pointers for both storages, even though only one is ever really used. This both makes the struct bigger, and adds a miniscule amount of overhead when initializing aFetchas it needs to zero out the unused Fetch impl.Solution
Extend #4800. Add a compile-time discriminated
unioncalledStorageSwitchwhich is a debug-build checked union of different ways a Fetch could represent a pointer to the respective storage.As sparse sets always have a reference populated when making a fetch. They're no longer wrapped in an Option, and do not need any unwrapping.
This can be done (more) safely if GATs was stablized, but it seems like rust-lang/rust#96709 may be stalled for the foreseeable future.
As this is based on #4800, this won't come out of draft until some form of that PR is merged.
Co-Authored-By: Boxy supbscripter@gmail.com