Skip to content

Commit c4b87fc

Browse files
perf(fetchium): snapshot entities without walking their proxies
`useQuery` deep-snapshots its result through `useReactive` on every dependency change, and the entity snapshot handler walked the proxy to do it. `Object.keys` triggers a `getOwnPropertyDescriptor` trap per key, that trap calls `handler.get` to produce the value it reports, and nested plain data came back as the read-only wrapper proxies the `get` trap returns, which the snapshot then walked through more traps. Every field was read twice per snapshot and each read allocated a descriptor. Each proxy now registers an `EntitySnapshotSource`: consume the notifier and activate the entity relay once, list the enumerable own keys, read a field's raw value. The handler walks nested plain data itself, unwrapping `WRAPPED_VALUE` holders and handing nested entity proxies back to Signalium so they still consume their own notifiers. The value half of the `get` trap moved into `readField` so the trap and the snapshot share one reader. On top of that, `EntityInstance` carries a `version` bumped in `notify()`. A snapshot records the source id, version and key list it came from, so re-snapshotting an entity still at that version re-reads only its dynamic fields: child entities, live collections, and a query's attached getters. `isStaticFieldDef` decides that per shape, recognising only the def shapes `getEntityDef` validates against and treating everything else as dynamic, so an unfamiliar def costs a read rather than serving a stale value. Array items are paired with the snapshot they produced themselves rather than with whatever sat at the same index. Signalium diffs arrays by position, so a front insert handed every entity its neighbour's snapshot and every memoized row re-rendered. The pairing index is built from the consumer's own previous array, so two consumers keep independent trees, and only when a mispairing is detected, so appends don't pay for it. Guarding the result, since none of this is observable from behavior: - Dev builds re-read the fields the fast path skipped and throw if one moved. That catches both a merge that changes `data` without notifying and a field wrongly judged static — the two ways a snapshot could go permanently stale — at the point it happens rather than in a render. It costs dev builds the read the fast path saved. - `__debug_snapshotFullWalks` / `__debug_snapshotFieldReads` let tests assert the fast path is taken. A regression that quietly routes every snapshot through the full walk leaves every behavioral test green. - `src/__benchmarks__/snapshot.bench.ts` and `npm run bench` measure the paths this touches, built with `IS_DEV: false` so the dev guard doesn't skew them. The snapshot origin holds the source's id rather than the proxy: a snapshot outlives the entity it came from, and a strong reference would have made the entity graph reachable from a detached snapshot tree. Ids are per proxy, which `PROXY_ID` is not — an entity has one proxy per shape and they share a key. Output is otherwise unchanged: same key set, same structural sharing, same reactive dependencies, no public API change. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 78334ff commit c4b87fc

6 files changed

Lines changed: 1295 additions & 143 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'fetchium': patch
3+
---
4+
5+
Snapshot entities without walking their proxies, and skip entities whose data has not changed.
6+
7+
`useQuery` deep-snapshots its result through `useReactive` on every dependency change, and the entity snapshot handler did that by walking the proxy: `Object.keys` triggers a `getOwnPropertyDescriptor` trap per key, that trap computes the value in order to report it, and nested plain data came back as the read-only wrapper proxies the `get` trap returns, which the snapshot then walked through more traps. Every field was read twice per snapshot and each read allocated a descriptor. Each proxy now registers an internal snapshot source, so the handler reads raw values once and walks nested plain data itself.
8+
9+
`EntityInstance` also carries a `version`, bumped on every `notify()` — the only way its own `data` changes. A snapshot records the entity and version it came from, so re-snapshotting an entity still at that version re-reads only the fields whose values live elsewhere in the graph: child entities, live collections, and the getters a query attaches to its root entity. Which fields those are is decided per shape and only for the shapes recognised as inert, so an unfamiliar type def costs an extra read rather than serving a stale value. Development builds re-read the skipped fields and throw if one moved, trading dev-build throughput for catching a stale snapshot where it happens rather than in a render.
10+
11+
Array items are now paired with the snapshot they produced themselves rather than with whatever sat at the same index. Signalium diffs arrays by position, so inserting at the front of a sorted list handed every entity its neighbour's snapshot: every row got a new identity and every memoized row re-rendered, on a list of any size. Arrays that Signalium walks itself are still positional, and a foreign entity's snapshot arriving as `prev` is now discarded rather than diffed against.
12+
13+
Snapshot output is otherwise unchanged — same keys, same structural sharing, same reactive dependencies — with no public API change. `npm run bench` covers the paths this touches.

‎packages/fetchium/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
"test:unit": "vitest run --project unit",
157157
"test:transform": "vitest run --project transform",
158158
"test:react": "vitest run --project react",
159+
"bench": "vitest bench --project bench",
159160
"check-types": "tsc --noEmit",
160161
"copy-docs": "node scripts/copy-docs.mjs",
161162
"build": "npm run copy-docs && npm run build:clean && npm run build:types && npm run build:dev && npm run build:prod && node scripts/generate-legacy-entries.cjs",

0 commit comments

Comments
 (0)