Skip to content

Fix archetype and query matching on hash collisions, and TrimExcess id reuse - #317

Merged
genaray merged 3 commits into
genaray:masterfrom
lgarczyn:fix-archetype-hash-collision
Oct 17, 2026
Merged

genaray merged 3 commits into
genaray:masterfrom
lgarczyn:fix-archetype-hash-collision

Conversation

@lgarczyn

@lgarczyn lgarczyn commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Hash collisions

QueryCache treat equal hashes as equal component sets, but we got at least one confirmed hash collision. For context, we have up to 2000 archetypes on a normal game.

Add, AddRange, Create and Remove move the entity to the wrong archetype, everything explodes. Dropped components, garbage new component. The add/remove pointers on archetypes keep the wrong archetype forever.

  • World.Query() can return another description's query.

Fix:

  • Linked list of Archetypes with the same hash
  • Signature.Equals and QueryDescription.Equals compare components after the hash check

TrimExcess reuses live ids (Fixes #315)

TrimExcess dropped recycled ids, which meant new entities could get existing id+version combo

Tests

9 new tests that fail on master and pass here.

Benchmark runs about the same

@genaray

genaray commented Oct 14, 2026

Copy link
Copy Markdown
Owner

Thanks! Before we merge this, is there probably a Hash algo that we could replace it with so it does Not generate collisions?

@lgarczyn

Copy link
Copy Markdown
Contributor Author

Thanks! Before we merge this, is there probably a Hash algo that we could replace it with so it does Not generate collisions?

System.HashCode is probably close to the birthday bound, with thousands of archetypes, thousands of users each with hundreds of sessions, you're gonna get collisions. From napkin math with 2000 archetypes it's about 1 collision per 2000 sessions.

Going to 64 bits would definitely fix it as well, but harder to test and prove, and then you have to do your hashing

@lgarczyn

Copy link
Copy Markdown
Contributor Author

We just had a second confirmed collision ^^ two resource containers suddenly became crewmembers

@genaray

genaray commented Oct 17, 2026

Copy link
Copy Markdown
Owner

We just had a second confirmed collision ^^ two resource containers suddenly became crewmembers

Alright, lets merge it than :) thanks!

@genaray
genaray merged commit cbe716f into genaray:master Oct 17, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: World.TrimExcess drops recycled ids, new entities then reuse the id of a live entity

2 participants