Skip to content

Project only the changed pages; skip cache lookups for unchanged files (#145 M4) - #334

Merged
QuintinBotes merged 5 commits into
mainfrom
perf/project-changed-pages-145
Nov 30, 2026
Merged

QuintinBotes merged 5 commits into
mainfrom
perf/project-changed-pages-145

Conversation

@QuintinBotes

Copy link
Copy Markdown
Owner

Advances #145 (milestone M4).

Stacked on #333 (perf/lazy-extraction-145), which is still open; this PR targets that branch so the diff is M4 alone.

A re-map now projects only the pages a change reached, and does not look up unchanged files in the extraction cache. This is about half of M4. The 2× target is not reached: every linker still reads every file's facts through allFileFacts(). That is the other half, and it is not in this PR (see "What remains O(repo)").

What changed

  • Projection by pages. Projection takes its input as pages: each file's facts, and each linker's share for the file it linked (JavaScript, Python, generic). The linkers hand projection that share as out.pages.
    • A page is projected again only when one of these holds: its file changed, was extracted or failed; a linker replaced the file's facts; or its linker linked the file again.
    • For such a page, a content hash decides whether its fact keys are worked out again. The old and new keys of a changed page are then diffed against fact_rows, as runs were.
    • Facts outside pages (history, discovery, linker output that belongs to no single file) keep the content-defined runs.
  • Nodes and edges are merged again from their contributors only. A node or edge is rebuilt from: its own path's pages; the pages fact_pages lists for it (one row per page holding a fact about a node it is not named after); the dirty pages; and the facts outside pages. The merge keeps the order of a full pass. An edge into a node from a page that was not read is found by target in fact_rows.
  • Kept state. Per page: its prefixes (projection:page). Per map: one brotli row with each page's signature, count and hash, and each linker's publication stamp (projection:pages).
    • A linker publication the projection did not see marks that linker's pages for comparison.
    • So does a linker that failed, because it left link-only attributes in place.
  • Cache lookups are skipped for unchanged files. cache:refs keeps, for each file, where its results are (page and slot, or empty) and which adapters read it. A file whose census entry is unchanged is neither matched against adapter globs nor looked up.
    • The row is valid only while the generation and projection stamp are the ones the last map wrote. A map by an older release rewrites the stamp, so the next map looks everything up again.
    • A stale page reference is read again by its key, through the existing lazyExtractions retry.
  • Bug fix from M3. An edited file's paged cache row is now decoded before its replacement can release the page. M3 passed the slot number as the file's previous facts, so on every edit that changed a declaration the generic linker relinked every module (4,400 of 4,400 at 10k). It now relinks 12–18.
  • Counters. cache.looked_up is the number of validity lookups. projection.pages: { dirty, read } is the number of pages projected again and pages decoded.

Cold maps are unchanged. Cold maps of tests/fixtures/{js,python,generic,contracts,database,iac,k8s,delivery,ownership,golden}, runtime/ and adapters/ store identical facts, nodes and edges before and after this change (compared row by row against #333's head). The new merge order differs from the old one only in ways that cannot change any merged node or edge.

Compatibility. Migration 17 fact-pages is additive (breaks: false, always). The run format is renamed (pages48:1), so an older release's runs are never misread: the first map after an upgrade, or after an older release mapped, matches fact by fact once. node scripts/upgrade-test.mjs --count 2 (v0.3.3, v0.4.0): 2/2 passed.

Measurements

Bench fixture, the bench's 10-file edit. Times are results[0], in ms; main is origin/main 9869568, which does not include #333.

10k, alternating:

run load (1/5/15) edit wall unchanged edit/unchanged census extraction all_facts link projection store bytes
main 1 6.41 7.49 8.39 1,132 165 6.9× 94 381 – 318 218 109,954,798
branch 1 6.19 7.35 8.32 953 158 6.0× 93 100 177 355 120 112,813,505
main 2 7.11 7.48 8.33 1,135 179 6.3× 101 347 – 341 224 109,917,930
branch 2 6.90 7.37 8.26 930 172 5.4× 104 109 172 334 110 112,920,018

50k, one run each (load below 8 at the start of each):

run load edit wall unchanged edit/unchanged census extraction all_facts link discovery coverage projection store bytes
main 5.35 6.92 8.06 5,598 477 11.7× 424 1,672 – 1,947 208 192 964 559,914,043
branch 7.75 7.03 7.93 4,345 456 9.5× 423 303 1,049 1,749 147 32 464 574,081,062

The edit is 18% faster at 10k and 22% faster at 50k. The store is 2.5% larger (page prefixes, the two kept rows, fact_pages).

What remains O(repo) (50k, branch run)

step ms why
allFileFacts() 1,049 Every linker reads every file's facts: JS, Python and generic build their indexes over all files and run linkFrameworks over all facts; quality, database, k8s, ownership, contracts, di, delivery, wiring, literals, iac and scip each scan every file.
link 1,749 The same linkers' global passes over those facts: indexes, member reach, unpacking every kept result, the generic reuse scan's indexes, ownership's OWNED_BY for every module.
projection 464 Of this, roughly: the kept rows (projection:pages, cache:refs) read and rewritten whole (O(files)); the runs over facts outside pages (history's per-module facts and linker output no single file owns); checkoutState's git calls.
extraction 303 Census-wide loops (adapter membership, the kept-refs check, empty-cache tokens), plus the real extraction of the 10 files.
census, history, discovery 423, 137, 147 Census is the baseline. History reads the window again for the new commit. Discovery runs its cached readers.

Reaching 2× needs every linker to work from kept per-file output and the change set, so that allFileFacts() runs only on a cold map. At 50k that would remove most of the 2.8 s in the first two rows. That work, and the remap-cost assertion facts_read.all.files == 0, are not in this PR. The 10-file edit still decodes every unchanged file through allFileFacts().

Tests

  • projection-pages.test.mjs (new). Randomized: 12 seeds × 15 edit steps over pages that share nodes, compared row by row with projecting the same input into an empty store. Covers:

    • a table declared by several pages, a package containing modules, constants that come and go, repeated facts, an id held with two signatures;
    • changes stated by signature or by dirty, and linker stamps that do or do not match.

    Also run once with 150 seeds, locally.

    • It also checks that an unchanged re-map reads no page and an edit reads few.
  • incremental-projection.test.mjs (new). Randomized map-level test: JS, Python and Java with imports, calls, a shared SQL table, package.json and requirements.txt, with files edited, added and deleted. Each step's facts, nodes, edges and derived facts are compared with a cold map. 2 seeds × 8 steps; also run locally with 25 seeds.

  • remap-cost.test.mjs.

    • For a 10-file edit, it asserts the unchanged files are not looked up, projection matches by runs, at most 30 pages are dirty and fewer than 60 are read, and the counts equal a cold map's.
    • New: an edited file's paged row is read before it is replaced, so the generic linker relinks fewer than 5 of 20 modules. It fails without the fix.
  • empty-extractions.test.mjs also drops cache:refs when it simulates a store with legacy rows, because the kept references describe the cache as the last map left it.

  • incremental-*.test.mjs and the JS and Python randomized differential tests pass unchanged.

  • npm test: 4,021 tests, 4,011 pass, 0 fail, 10 skipped.

🤖 Generated with Claude Code

Quintin Botes and others added 4 commits November 30, 2026 13:03
The Python linker emits its output per module and records what each
module's link read in link_refs: the files whose symbols or bindings it
looked up, and the layout keys its resolution read (mod:<dotted>,
top:<package>, stem:<path>). A re-map links again the changed modules,
the modules that read a layout key of a Python file that came or went,
the modules that read either, and modules with unused-member evidence;
every other module reuses its kept output. Different source roots, an
empty link_refs or a semantic index link every module.

directories() and packFact/unpackFact move to link-state.mjs and are
shared with the JavaScript linker; a packed `path:line` source ref is
stored as the line number. The randomized test harness moves to
tests/helpers/incremental-link.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…aries (#145 M3)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#145 M4)

Projection takes its input as pages: each file's facts, and each linker's
share for the file it linked (JavaScript, Python, generic). A page is
projected again only when its file changed, was extracted or failed, a
linker replaced its facts, or its linker linked it again; its content hash
then decides whether its fact keys are worked out. Facts outside pages keep
the content-defined runs. A node or edge is merged again from its own
path's pages, the pages fact_pages lists for it (migration 17, additive),
the dirty pages and the facts outside pages, in the merge order of a full
pass. Cold maps store exactly what they stored before.

A file whose census entry is unchanged since the last map is not looked up
in the extraction cache: cache:refs keeps where each file's results are
(page and slot, or empty) and which adapters read it, valid while the
generation and projection stamp are the last map's.

An edited file's paged cache row is now read before its replacement can
release the page: the generic linker reused nothing after M3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Base automatically changed from perf/lazy-extraction-145 to main November 30, 2026 14:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@QuintinBotes
QuintinBotes merged commit 0b2da8f into main Nov 30, 2026
12 of 13 checks passed
@QuintinBotes
QuintinBotes deleted the perf/project-changed-pages-145 branch November 30, 2026 15:16
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