Repository navigation
Commit c4b87fc
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
- .changeset
- packages/fetchium
- src
- __benchmarks__
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
| 159 | + | |
159 | 160 | | |
160 | 161 | | |
161 | 162 | | |
| |||
0 commit comments