perf(cache,parser): a cache hit builds the relationship node Maps on first use (stacked on #9925)

Closed
LO
19d ago
+1059-182

Stacked on #9925, which is stacked on #9782 and #9785. This PR's own change is the top commit, 0103e634ec.

Untraced expectation: about 20 ms on a Holter reopen (less on smaller models). It gets no slot of its own: it is timed together with #9925 as one arm, and the two together are expected to remove about 95–130 ms from Holter's reopens.

Why

readEdges built two Maps per relationship half, offsets and counts, one entry per node, before any consumer asked. Then it validated every node's range by iterating them. That was most of the relationships section's decode: 32 ms traced on a Holter snapshot reopen (main 52c82a099d + #9782 + #9785), about 20–25 ms untraced (call-heavy Map loops inflate most under the profiler).

In Node, on 184,000 nodes:

partms
building the two Maps65.5
validating through the Maps28.8
the same range and order checks on the typed arrays0.7

Holter's graph has 82,334 forward and 61,474 inverse nodes.

Change

  • Node table kept as typed arrays. The table is written ascending by entity id (RelationshipGraphBuilder sorts its keys), and the reader checks that order. getEdges, getTargets and hasAnyEdges binary-search it.
  • Maps on first read. offsets and counts are getters that build the same Maps, with the same entries in the same order, the first time a consumer reads them. Every later read returns the same object.
  • Old or hand-built tables keep today's behaviour. A node table that is not strictly ascending (unsorted, or with a repeated id) builds the eager Maps at once, so a repeated id still resolves to its last row and only that row's range is checked.
  • Range validation moved to the typed arrays. It throws the same error message as the Map-based check.
  • One parser call site. RelationshipEdges.nodeIds() is a new optional member that lists the node ids in the Maps' order. The spatial hierarchy's canonical-parent pass, which runs during the cache hit's cache.spatial, walks inverse.nodeIds() instead of inverse.offsets.keys(). Otherwise it would force the inverse Map back during the load. A graph without nodeIds (a fresh parse) walks the Map as before.

Proof

  • Equivalence on the seven cached fixtures (one-off Node check: parser graph → cache write → lazy read): FZK, advanced_model, ISSUE_129, schependomlaan, skolebygg, Holter and BWK.
    • Every node id on both halves: getEdges and hasAnyEdges equal the eager graph's.
    • nodeIds() equals offsets.keys() in order, and the Maps' entries are equal.
    • Absent ids answer the same.
    • 0 mismatches in 392,796 lookups.
  • Tests (relationships.test.ts, 15 pass):
    • the lazy Maps and every lookup equal the eagerly built graph, for present and absent ids and each relationship type;
    • an early reader of offsets right after the decode gets the complete Maps, the same object every time;
    • an unsorted or repeated-id table reads as before (eager, last row wins);
    • an out-of-range row is rejected with the same message, ascending or not;
    • the existing corrupt-range, shadowed-id and trailer tests are unchanged and pass.
  • Suites:
    • packages/cache: 267 passed, 1 skipped;
    • packages/parser: 2,259 passed, 19 skipped;
    • viewer cache-hit and spatial-group files: 33 pass;
    • typecheck clean for data, parser and cache.

šŸ¤– Generated with Claude Code