Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the pointers in Fetch impls unions #5085

Closed
wants to merge 30 commits into from

Conversation

james7132
Copy link
Member

Objective

Every Fetch struct 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 a Fetch as it needs to zero out the unused Fetch impl.

Solution

Extend #4800. Add a compile-time discriminated union called StorageSwitch which 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

@james7132 james7132 requested a review from BoxyUwU June 23, 2022 21:36
@james7132 james7132 added A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change labels Jun 23, 2022
@nicopap
Copy link
Contributor

nicopap commented Jun 24, 2022

@nicopap
Copy link
Contributor

nicopap commented Jun 24, 2022

Some suggested changes james7132#4

@Weibye Weibye added the S-Adopt-Me The original PR author has no intent to complete this work. Pick me up! label Aug 10, 2022
@james7132 james7132 removed the S-Adopt-Me The original PR author has no intent to complete this work. Pick me up! label Sep 14, 2022
@james7132 james7132 closed this Oct 28, 2022
@james7132 james7132 deleted the fetch-union branch December 12, 2022 06:44
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants