Perf/p2 canonical archetype key - #36
Merged
Merged
Conversation
…56 archetypes) scenarios (#772)
Adding several components at once (CreateEntityWithComponentsN, AddComponentsN) resolved the destination archetype by scanning every archetype and comparing sets element by element, and the variadic slice escaped to the heap on every call because a miss retained it as the new archetype's type: one allocation per entity, and a cost growing with the number of archetypes (x7 with 256 of them). An archetype type is now a sorted set, hashed (FNV-1a) into a map from key to archetype. The candidate set is sorted in a stack scratch and only cloned when an archetype is actually created. A key collision falls back to the scan, the first archetype keeps the key. BenchmarkCreateEntityVolt (5800X): 100 101 allocs/op -> ~114 (column growth and world setup only).
Resolving the archetype reached by adding several components at once through the canonical key costs a sort, a hash, a map lookup and a type comparison on every call, while the same transition is asked again and again from the same call sites. A direct-mapped cache on the world remembers, per (source archetype, ids as given), the destination: a hit is one key comparison, with no sorting. The whole key is compared, so a slot collision is a miss, never a wrong archetype, and archetypes are never destroyed, so entries never go stale. BenchmarkCreateRemoveVolt (5800X): 7.7 ms/op -> 5.8 ms/op (v1.9.0 scan: 6.2).
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
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.
Canonical archetype key for multi-component adds
Adding several components at once resolved the destination archetype by scanning every archetype, and the variadic slice escaped to the heap on each call: one allocation per entity, and a cost growing with the archetype count.
An archetype type is now a sorted set hashed into a map; the candidate set is sorted in a stack scratch and cloned only when an archetype is created. A small direct-mapped cache on the world remembers each (source archetype, ids) →
destination transition, so a repeated add is one key comparison.
Benchmarks (5800X, n=6 median): create −17 %, create+remove −9 %, with 256 archetypes −87 %, 8 components −16 %; allocations per entity 1 → 0. Add/remove/iterate unchanged (±2 %).