-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Opprotunistically use dense iteration when an archetype and its table are 1:1 #12381
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
Comments
Isn't it possible for them to have the same entity count by coincidence? |
An archetype can only have one table mapped to it. If they have the same entity count, the archetype and table should map to the same entities. If we add a new sparse set component to any of those entities, we still write to the same table, but fragment the archetype instead, which would disable this opportunistic optimization. |
Why is iterating through archetypes slower? |
yeah , assume we have two distinct archetypes, let's call them A and B: Both archetypes share the same table_id. When entities in Archetype B are despawned, could potentially disrupting the order of entities in Archetype A. |
# Objective - currently, bevy employs sparse iteration if any of the target components in the query are stored in a sparse set. it may lead to increased cache misses in some cases, potentially impacting performance. - partial fixes #12381 ## Solution - use dense iteration when an archetype and its table have the same entity count. - to avoid introducing complicate unsafe noise, this pr only implement for `for_each ` style iteration. - added a benchmark to test performance for hybrid iteration. ## Performance  nearly 2x win in specific scenarios, and no performance degradation in other test cases. --------- Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: Christian Hughes <[email protected]>
What problem does this solve or what need does it fill?
We currently force sparse iteration if any of the target components in the data or filter part of a query is a sparse set component. This is suboptimal given the random access pattern forced by archetypal iteration.
If an archetype and its table have the same entity count, we should be able to assume that they target the same set of entities, which should make it safe to use dense iteration.
What solution would you like?
Extend all iteration methods to opportunistically use dense iteration if the fetched archetype and table have the same length.
What alternative(s) have you considered?
Leave it as is, this may notably complicate the safety story for query iteration.
Additional context
Related: #2144.
The text was updated successfully, but these errors were encountered: