Skip to content

Commit 1ceeb0a

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 1ceeb0a

6 files changed

Lines changed: 1289 additions & 143 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'fetchium': patch
3+
---
4+
5+
Entity snapshots no longer walk the proxy to read fields, and an entity whose data hasn't changed is no longer re-read at all. Previously `Object.keys(proxy)` triggered a descriptor trap per key that computed the value in order to report it, so every field was read twice on every dependency change. Each entity now carries a version, and a snapshot taken at the same version re-reads only the fields whose values live elsewhere: child entities, live collections, and a query's own getters.
6+
7+
Array items are paired with the snapshot they produced themselves rather than the one at the same index, so inserting into or re-sorting a list no longer gives every existing row a new snapshot identity. Output is otherwise unchanged. Dev builds re-read the skipped fields to verify that, so the speedup is a production-build property.

‎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)