Repository navigation
🐛 core: fix query hash collisions from the shared sort buffer - #281
Merged
krispya merged 3 commits intoSep 8, 2026
Merged
Conversation
Member
|
Thanks this a helpful bug you found! I think there might be a simpler solution 🤔. I'm going to ponder it for a day. |
createQueryHash in packages/core/src/query/utils/create-query-hash.ts writes into a module-level Float64Array named sortBuf and starts every call at index 0. When a relation filter is passed as inline parameters, the targetQuery branch recurses into createQueryHash to hash the target query. That nested call restarts at index 0 and overwrites the entries the outer call has already written. The result is that world.query(Position, ChildOf(IsPlayer)) and world.query(Name, ChildOf(IsPlayer)) both hash to "3,49000001", because the leading trait id is replaced by IsPlayer's id in both. That hash is the key for ctx.queriesHashMap in packages/core/src/world/world.ts, so the second query reuses the first query's cached instance and silently returns the wrong entities. A nested call now claims the slice after its caller's entries through a sortBufBase offset, and each call joins sortBuf.subarray(base, cursor) rather than subarray(0, cursor). Nothing is allocated, and a query with no inline relation filter never touches the offset.
krispya
force-pushed
the
fix-nested-query-hash-buffer
branch
from
September 8, 2026 08:22
80b84f7 to
c02bd83
Compare
Member
|
I did some slight tweaks and moved this to canary which will soon become the new main. Thank you! |
krispya
added a commit
that referenced
this pull request
Sep 16, 2026
* 🐛 core: fix query hash collisions from the shared sort buffer createQueryHash in packages/core/src/query/utils/create-query-hash.ts writes into a module-level Float64Array named sortBuf and starts every call at index 0. When a relation filter is passed as inline parameters, the targetQuery branch recurses into createQueryHash to hash the target query. That nested call restarts at index 0 and overwrites the entries the outer call has already written. The result is that world.query(Position, ChildOf(IsPlayer)) and world.query(Name, ChildOf(IsPlayer)) both hash to "3,49000001", because the leading trait id is replaced by IsPlayer's id in both. That hash is the key for ctx.queriesHashMap in packages/core/src/world/world.ts, so the second query reuses the first query's cached instance and silently returns the wrong entities. A nested call now claims the slice after its caller's entries through a sortBufBase offset, and each call joins sortBuf.subarray(base, cursor) rather than subarray(0, cursor). Nothing is allocated, and a query with no inline relation filter never touches the offset. * ♻️ core: simplify recursive query hash offsets * 🧪 benches: cover createQuery and inline query performance --------- Co-authored-by: Kris Baumgartner <kjbaumgartner@gmail.com>
krispya
added a commit
that referenced
this pull request
Sep 16, 2026
* 🐛 core: fix query hash collisions from the shared sort buffer createQueryHash in packages/core/src/query/utils/create-query-hash.ts writes into a module-level Float64Array named sortBuf and starts every call at index 0. When a relation filter is passed as inline parameters, the targetQuery branch recurses into createQueryHash to hash the target query. That nested call restarts at index 0 and overwrites the entries the outer call has already written. The result is that world.query(Position, ChildOf(IsPlayer)) and world.query(Name, ChildOf(IsPlayer)) both hash to "3,49000001", because the leading trait id is replaced by IsPlayer's id in both. That hash is the key for ctx.queriesHashMap in packages/core/src/world/world.ts, so the second query reuses the first query's cached instance and silently returns the wrong entities. A nested call now claims the slice after its caller's entries through a sortBufBase offset, and each call joins sortBuf.subarray(base, cursor) rather than subarray(0, cursor). Nothing is allocated, and a query with no inline relation filter never touches the offset. * ♻️ core: simplify recursive query hash offsets * 🧪 benches: cover createQuery and inline query performance --------- Co-authored-by: Kris Baumgartner <kjbaumgartner@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.
Two queries that differ only in the traits listed before a relation filter end up with the same hash, so the second one reuses the first one's cached query instance and hands back the wrong entities.
The problem
Both of those hash to
3,49000001, where3isIsPlayer's trait id. Nothing throws, the second query just quietly returns the first one's results.The cause
createQueryHashinpackages/core/src/query/utils/create-query-hash.tsfills a module levelFloat64ArraycalledsortBufand starts every call at index 0. When a relation filter is given as inline parameters, line 43 recurses intocreateQueryHashto hash the target query. That nested call restarts at index 0, so it writesIsPlayer's id over thePositionorNameid its caller had already put there, and sorts that slice on the way out. The caller then carries on from its own cursor and joins a buffer whose leading entries belong to the target query rather than to itself.The hash is the cache key for
ctx.queriesHashMapinpackages/core/src/world/world.tsand foruniverse.cachedQueriesinpackages/core/src/query/query.ts, so a collision returns a query instance that was built from different parameters.Passing a cached query instead, as in
ChildOf(createQuery(IsPlayer)), was never affected, because that branch reads.hashoff the query rather than recursing. That is also why the existing relation filter tests pass: they only ever pass the filter as the sole parameter, or pass a cached query.The fix
A nested call now claims the slice after its caller's entries through a
sortBufBaseoffset, and every call joinssortBuf.subarray(base, cursor)rather thansubarray(0, cursor). Nothing extra is allocated, and a query with no inline relation filter never reads or writes the offset, so the hot path is unchanged.Verification
Node 24.11.0, pnpm 11.9.0.
pnpm -F @koota/core test runwith the new test and without the source change fails onexpected [ 2 ] to deeply equal [ 3 ]. With the source change the core suite is 136 passed, 1 expected fail, 8 files, and the wholepnpm testrun is green across@koota/collections(23),@koota/core(136) and@koota/react(36).I also ran the
pnpm test:buildpath, building the bundle and running the generated tests againstdist: 172 passed, 2 expected fail, 13 files. Reverting onlycreate-query-hash.tsand rebuilding makes the same generated test fail, so the fix holds through the inline function transform too.