fix(data): canonical observe.select cache key so Set includes don't collide - #166
Merged
Conversation
…ollide
observe.select cached observe functions under JSON.stringify({include, options}),
but a Set (the shape of archetype.components) serializes to "{}", collapsing every
set-based select onto one key. In a composed graph a second set-based select was
served the first's observe, so membership contraction never re-emitted. Replace
with a hand-built canonical key (sorted include/exclude, sorted where, ordered
order) — correct across array/Set and ~2.3x faster than the stringify path.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.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.
Description
observe.selectcached its per-queryObservefunctions underJSON.stringify({ include, options }). Becauseincludemay be aReadonlySet— exactly the shape ofarchetype.components— andJSON.stringify(aSet)is always"{}", every set-based select collapsed onto a single cache key. A second set-based select was then served the first one's observe function.This explains the reported minimal-vs-composed divergence: with a single set-based subscription (a minimal chain) the collision is invisible; in a composed graph an earlier set-based select poisons the shared key, so a later
observe.select(TrackItem.components)silently watches the wrong include set and membership contraction (update(entity, { orderedChildOf: undefined })) never re-emits (before:1, after:1). Every existing test passed an array, so the Set path was unexercised.Fix
Replace the key builder with a hand-built canonical key:
include/exclude— sorted (order- and array-vs-Set-agnostic), killing the collision.where— keys sorted, values JSON-serialized (so equivalent filters dedup too).order— sequence preserved (it's a sort priority).Chosen over a normalized
JSON.stringify(the quick fix) and over a WeakMap identity cache: the hand-built key is ~2.3x faster than stringify in both stable-ref and fresh-literal microbenchmarks and pessimizes neither, on a subscription-setup path where absolute cost is already sub-microsecond. A WeakMap identity cache hits 127 M/s on stable refs but drops to stringify-level on fresh literals — the wrong trade for a cold path.Tests
New
Set-based includeblock inobserve-select-entities.test.ts(red before, green after):Full
@adobe/datasuite (3001 tests) and monorepo typecheck pass.Related PRs
Follows up #144 (membership-contraction re-emit).