Skip to content

Bring derived facts forward, hash less, drop unused facts indexes (#145) - #163

Open
QuintinBotes wants to merge 4 commits into
mainfrom
goal/145-incremental-projection
Open

QuintinBotes wants to merge 4 commits into
mainfrom
goal/145-incremental-projection

Conversation

@QuintinBotes

Copy link
Copy Markdown
Owner

Summary

Follow-up to #144 for #145. Derived facts are now brought forward from the rows projection changed instead of being recomputed from the whole graph, hashing fact ids costs about half the CPU, and three facts indexes nothing reads are dropped. A 10-file re-map at 50k went from 12.0 s to 8.8 s, and a one-file edit on MewsSystems/mews from 30 to 44 s to 23 to 27 s. Neither #145 target is met: the changed / no-change ratio is 17.6 (bound 2), and the store is 46.8 times the tree (bound 3). What is left runs over the whole repository for reasons the design here cannot remove soundly. They are listed below.

Part of #145

Design

Derived facts (runtime/graph/derived.mjs updateDerived). project now returns the node rows it wrote or deleted (before and after) and the edges it wrote or deleted. Components and their strict parts depend only on IMPORTS edges, and the build check only on packages, package containment and DEPENDS_ON. A change to any of these recomputes everything, now reading only the four edge types derived facts use (Graph.fromStore(store, { edgeTypes })). Otherwise the components stand, and only these are worked out again from the store:

  • the node-level rows (public_surface, test_code, ownership) of changed modules and of modules whose OWNED_BY edges changed
  • the components and declared-only edges that hold a node whose type, path, test flag or language changed

Rows keep the generation they were written at, and the _done row of a generation names it (DERIVED_VERSION 3). An update therefore rewrites only the rows that changed. A kind whose key set changes is written again in key order, because readers take rows by rowid. The summary's derived reports updated, recomputed or kept.

Hashing and reading.

  • canonicalJSON returns a value already in canonical form as it is, instead of copying it. Cached facts are stored canonical. The output is unchanged; the change is covered by a 400k-value fuzz run against the old implementation and a unit test.
  • Projection's merge pass skips untouched edge facts without building their ids.
  • Empty cache rows are not parsed.
  • A linker's saved state is read only by linkers that keep one.

Store (migration 4, drop-unused-fact-indexes, non-breaking). facts_subject, facts_path and facts_generation are read by no query. Lookups go by id, and search goes by object. schema_version stays 3, so 0.4.0 can still write the store. It creates the indexes again, and the next open here drops them.

Rejected or not done.

  • Loading only changed files' facts. Every linker builds its indexes from every file's facts, and discovery and coverage read them too.
  • Projecting only changed files' facts. This is unsound with the linkers as they are. The route-group and server-interface steps (JavaScript, Python and generic linkers), contracts, and the member-attribute drop all change other files' facts after loading. What a file contributes is therefore not a function of its own cache row.
  • Incremental JavaScript and Python linkers. At 50k, a 10-file re-map spends about 0.5 s and 0.35 s of CPU in them. The generic pattern itself spends about 0.5 s of its 1.1 s reading kept results back for 24,900 files, so the saving would be small. It is not done.
  • A sha1 of JSON.stringify as a fact fingerprint. It needs another column and saves less than the canonical fast path.
  • Compressing the per-file cache. It would trade store size for re-map time.

Targets

Target Status Evidence
1. Read only changed facts Not met (constant factor only) Empty rows skipped, linker state lazy. Reading stays 2.0 s at 50k and 3.5 to 3.9 s on mews (see Rejected)
2. Delta projection Partly: still hashes every fact, about half the CPU per id idbench: 1.5 to 1.9 s vs 3.5 to 3.7 s CPU for 376k fact ids, alternated in one process. Projection 2.9 → 2.7 s at 50k
3. Derived facts Met for edits that keep the import graph; others recompute from four edge types 50k: about 2.9 s → under 0.1 s (47 ms in the gate run). mews: 5.4 to 9.3 s → 2 to 16 ms
4. JS/Python incremental linkers Not done Payoff bounded at about 0.85 s CPU, less replay cost (see Rejected)
5. Store ≤ 3× tree Not met; one sound reduction Migration 4: 50k 1,168 → 1,092 MB (50.0 → 46.8×). mews cold store 3.1 → 2.75 GiB (about 7.4× its 0.37 GiB tree). The 50k files are about 470 bytes and yield about 18 facts each, each kept in the per-file cache, the facts table and the merged nodes and edges
Incremental map = cold map Met See Testing

Measurements

Apple M5 Max, Node 26.13. Other projects' agents kept the load average between 8 and 88. Pairs were run back to back, and only the calm pair (load 8 to 10) is compared.

node scripts/bench.mjs --sizes 50k --gate:

Run Cold No change 10 changed Store No change / cold Changed / no change Store / tree
main, first pair 51.7 s 0.5 s 12.5 s 1,168 MB 0.01 24.2 50.0
branch, first pair (load spike) 122.4 s 1.0 s 10.2 s 1,092 MB 0.008 10.5 46.8
main, second pair 48.8 s 0.5 s 12.0 s 1,168 MB 0.011 23.3 50.0
branch, second pair 49.3 s 0.5 s 8.8 s 1,092 MB 0.01 17.6 46.8

The branch's 10-file re-map in the second pair: census 0.4 s, extraction (reading cached facts) 2.0 s, link 3.2 s, discovery 0.3 s, projection 2.7 s, derived under 0.1 s. main had the same phases plus about 2.9 s of derived facts.

MewsSystems/mews (30.8k files, 210,713 nodes, 1,085,342 edges), unknot map from the CLI. A // touch comment is appended to Enterprise.cs and committed, alternating main and the branch on two copies of the same store (load 12 to 23):

Run main Branch
No-change re-map 0.7 s 0.7 s
One-file edit, three rounds 44 s, 30 s, 30 s 27 s, 25 s, 23 s
of which derived 9.3 s, 5.6 s, 5.4 s 4 ms, 2 ms, 16 ms
of which link 11.8 to 12.4 s 11.0 to 12.1 s
of which projection 5.4 to 13.3 s 5.1 to 5.7 s
Store after a cold map 3.1 GiB (#88) 2.75 GiB

Testing

Equality with a cold map:

  • On mews, a cold branch map of the same tree and history matches the incremental store after the upgrade and four edits. Facts (1,374,200), nodes, edges and derived facts hash identically, and node and edge counts match.
  • On the 50k fixture, a cold map matches the store after five 10-file re-maps (facts, nodes, edges and derived hash identically).

New and extended tests:

  • tests/unit/graph/incremental-link.test.mjs adds import cycles in C#, JavaScript and Python, with one cycle across build units and a CODEOWNERS file. Each step is compared row by row with a cold map. The steps are:

    • a body edit in a cycle member
    • a module that gains, then loses, its first export
    • Python and C# body edits
    • an owners change
    • a new import that closes a cycle
    • a broken cycle

    Each step also checks whether derived facts were updated or recomputed.

  • tests/unit/graph/incremental-derived.test.mjs (new) changes stored node and edge rows by hand and compares the updated rows with a full recompute, in rowid order. The changes:

    • a member of a component the build forbids becomes a test
    • an end of a declared-only edge becomes a test
    • node-level rows come and go
    • an OWNED_BY edge alone changes

    It also checks that an import, package, containment or DEPENDS_ON change is not brought forward, and that the four edge types are enough.

  • tests/unit/policy/core-primitives.test.mjs: canonical JSON of partly canonical values.

  • tests/unit/state/migrations.test.mjs: migration 4 drops the indexes, including after an older release has recreated them, and keeps schema 3.

Mutation checks. Each mutation broke one of the new tests:

  • not recomputing changed components: caught by incremental-derived
  • not recomputing node-level rows: caught by both
  • not falling back on an IMPORTS change: caught by both
  • not recomputing declared-only rows: caught by incremental-derived
  • writing a kind back unsorted: caught by incremental-derived
  • ignoring OWNED_BY-only changes: missed at first, so the OWNED_BY case was added; then caught by incremental-derived
  • leaving the test flag out of the structural check: caught by incremental-derived
  • not dropping one index: caught by migrations

Commands:

  • npm test: 3,680 tests, 3,664 passed, 9 skipped, 7 failed. All 7 failures are in tests/unit/daemon/remote.test.mjs ("CA signature digest algorithm too weak", local LibreSSL) and fail the same way on main.
  • node scripts/upgrade-test.mjs --count 3: 3/3 (v0.3.2, v0.3.3, v0.4.0).

Migration notes

Migration 4 runs on first open. It is quick and keeps schema 3. Derived facts are recomputed once by the first map after the upgrade. An existing store keeps the pages the dropped indexes freed and reuses them for later writes. A new store is smaller.

🤖 Generated with Claude Code

Quintin Botes and others added 4 commits November 26, 2026 23:10
A changed re-map recomputed every derived fact from the whole graph
(Graph.fromStore plus every component). Projection now returns the node
and edge rows it wrote or deleted, and updateDerived works from those:
components and their strict parts depend only on IMPORTS edges, and the
build check on package containment, packages and DEPENDS_ON, so a change
to any of these still recomputes everything. Otherwise only the
node-level rows of changed modules, and the components and declared-only
edges holding a module whose type, path, test flag or language changed,
are worked out again, from the store. Rows keep their generation and the
_done row names it (DERIVED_VERSION 3), so nothing else is rewritten; a
kind whose key set changes is written again in key order.

The full recompute reads only the edge types derived facts use.

Tests: incremental-derived compares updated rows, in rowid order, with a
full recompute after hand-made node and edge changes; incremental-link
adds cycles in C#, JavaScript and Python and checks each step against a
cold map and which path derived facts took.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- canonicalJSON returns a value already in canonical form as it is
  instead of copying it. Cached facts are stored canonical, so hashing
  every fact id in projection no longer copies every attrs object. The
  output is unchanged (a test covers integer-like keys, holes, undefined
  members and unsorted parts under sorted ones).
- Projection's merge pass skips an edge fact that no change touches
  without building its id.
- An empty cache row ('[]', most rows of contracts, di and security) is
  not parsed.
- A linker's saved state (the previous signatures of every path) is read
  and worked out only when the linker asks for it; most keep none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
facts_subject, facts_path and facts_generation are read by no query
(lookups go by id, and search by object). They were about 11% of the
MewsSystems/mews store (353 MB of 3.1 GB) and 6% of the synthetic 50k
store, and every fact written maintained them. Migration 4 drops them
and the DDL no longer creates them. It does not raise schema_version:
an older release that opens the store creates them again and loses
nothing, and the next open here drops them again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/benchmarks.md: the 50k gate, main and this change back to back
(changed / no change 23.3 to 17.6, store / tree 50.0 to 46.8), the
mews one-file edit (30 to 44 s to 23 to 27 s, store after a cold map
3.1 to 2.75 GiB, same rows as a cold map), and why reading every
file's facts, projecting only changed facts and incremental
JavaScript and Python linking are not done here. CHANGELOG entries
and a migration note.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant