Skip to content

docs: design proposal — incremental cross-references (for Commander decision) - #575

Merged
GoKubar merged 2 commits into
mainfrom
agent/daniel-parent/incremental-xref-design
Sep 15, 2026
Merged

GoKubar merged 2 commits into
mainfrom
agent/daniel-parent/incremental-xref-design

Conversation

@d-q222

@d-q222 d-q222 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

What

This is a proposal only. It needs the Commander's ruling before any implementation starts. It changes no product code.

It adds docs/design/incremental-cross-references.md, a design document of about three pages that covers:

Recommendation:

  1. Measure the 302-page document through flashtex-render first.
  2. Then do a small flag audit, starting with cites and \bibitem.
  3. Then adopt Option A inside FT-070, falling back to the clean path when it oscillates.
  4. Keep Option C in reserve, and leave Option B to FT-070's parse work.

Why

This is the "biggest remaining win" item in #535's Not done list. The work is an architecture decision (Tier-1), so it needs a ruling before implementation.

Overlap check

Test results

docs-only change: no build or test targets affected
wc -w docs/design/incremental-cross-references.md: 2625

Not done

🤖 Generated with Claude Code

https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz

…ecision)

What changed:
- docs/design/incremental-cross-references.md: problem and measurements from
  #535 (302-page document, 0 of 5 912 blocks reused, 265 ms warm edit), the
  current dependency model in crates/compiler and in render-pipeline, three
  options (dependency-tracked resolution with a dirty-page fixpoint;
  incremental parsing with per-block checkpoints; aux-style stale references
  with background reconciliation), FT-070 interaction, a recommendation and
  open questions. Proposal only; no product code.

What was run:
- Read-only code study of crates/compiler/src/{incremental,layout,parser,xref,bib,protocol}.rs
  and crates/render-pipeline/src/{lib,adapter,incremental}.rs; gh pr view 535, 206, 232, 273, 283, 294, 299.

Next step:
- Commander ruling on the open questions in section 6.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
@d-q222

d-q222 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

daniel-parent independent review (subagent)

Verdict: CHANGES NEEDED (one factual correction; the rest of the doc checks out precisely)

Method: read the doc against a worktree pinned to the exact commit it cites (origin/main 36fe7ec3), then grepped/read crates/compiler/src/{parser,incremental,layout}.rs and crates/render-pipeline/src/{adapter,incremental,lib}.rs for every checkable factual claim.

Verified accurate:

  • 18 document_global_state = true sites in parser.rs — exact count matches, and spot-checking the surrounding code at each confirms they correspond to the named constructs (label, ref/pageref/cref family, \tableofcontents, cite/\bibitem, footnote, \chapter, series/resume, a numberwithin-like counter site, subequations).
  • compile_project_with's flag branch: incremental.rs around line 219, if parsed.document_global_state { ...layout::layout_converged_with_options(...); stats.full_recompile = true; stats.blocks_recomputed = parsed.blocks.len(); return ... } — exactly "skips block reuse entirely."
  • layout_converged_with_options (layout.rs:2153): starts from state = (BTreeMap::new(), Vec::new()), an empty label/TOC table; loops up to REFERENCE_ITERATION_LIMIT = 5 (layout.rs:67) with oscillation detection (history.contains(&next)) and a fallback warning; visit_references runs after the loop regardless. Matches the doc's description precisely.
  • Render-pipeline claims: adapter::Labels::from_parsed (adapter.rs:1088), lower_inline (adapter.rs:2156, converts Reference/CleverReference to resolved Inline::Text), RenderCache (incremental.rs:74), MAX_LABEL_PASSES: u32 = 3 (lib.rs:68), needs_pages (adapter.rs:1106) all exist exactly as named; lib.rs:220's let max_passes = if adapter::Labels::needs_pages(&parsed) || has_lists { MAX_LABEL_PASSES } else { 1 }; matches "runs up to MAX_LABEL_PASSES passes... whenever needs_pages is true or the document has contents lists."
  • ShellModel.locateDefaultProducer() (apps/mac/Sources/FlashTeXMac/ShellModel.swift:863): locateRenderPipeline() ?? locateCompiler() — matches "prefers flashtex-render and falls back to flashtex-compiler."

Inaccurate claim (§2.2):

"The RenderCache key is a hash of the block's content... and not its geometry. So only blocks whose resolved text changed miss the cache."

This overstates the actual granularity. In adapter.rs's items_cached (~line 6242–6294), every block's cache key unconditionally mixes in labels_fp:

let labels_fp = { ... for (k, v) in &labels.values { k.hash(&mut h); v.hash(&mut h); }
                   for (k, v) in &labels.pages  { k.hash(&mut h); v.hash(&mut h); } ... };

(adapter.rs:1219–1229), a hash of the entire document's resolved label-value and label-page maps, computed once per render call and then hashed into every block's key (adapter.rs:6294, unconditionally, before the per-inline match). So when any single label's resolved value or page changes anywhere in the document, labels_fp changes and every cached block — including blocks with no references at all — gets a new key and misses, not just the blocks whose own resolved text changed. It's whole-document invalidation gated on the label table changing at all, not the selective per-reference invalidation the doc describes.

This matters for the doc's own argument: §4 leans on "the render pipeline already has A's numbers-from-parse and content-keyed reuse" as evidence the render-pipeline model is basically sound and cheap to extend, and the Option A latency estimate is anchored to "in line with the 500 KB figure." Whether render-pipeline's existing reuse is as selective as claimed affects how much weight that comparison should carry — worth a line in §2.2 and a caveat wherever §3/§5 use it as a baseline, rather than blocking the recommendation itself (the doc's own step 0, "measure #535's 302-page document through flashtex-render," would surface this in practice regardless).

Nothing else to flag; the doc is unusually precise about the code it cites.

…lock

What changed:
- §2.2 claimed that "only blocks whose resolved text changed miss the
  cache", which is wrong. `adapter.rs` `items_cached` mixes `labels_fp`, a
  hash of the whole `labels.values` and `labels.pages` maps (~1219-1229),
  into every block's `adapted` key. So any label value or page change
  re-adapts every block. The `blocks` and `assembled` maps key on the adapted
  items plus the style fingerprint and flags, so unchanged blocks still hit
  there. `RenderCache::stats()` counts `blocks` only.
- Option A gains an explicit step 2: key each block only on the labels it
  reads, never on a whole-table fingerprint. Later steps are renumbered. The
  expected latency now states that 30-40 ms depends on that step; with a
  whole-table key a renumbering edit costs a full layout.
- §4: the render pipeline lacks both the per-block label key and page
  seeding, so its reuse is weaker evidence for A.
- §5: step 0 measures `adapted` hits and misses for three edit kinds (no
  label change, renumbering, page move). Phase 1 includes the per-block key
  and adds a renumbering acceptance check. "Why A" and open question 3 are
  updated to match.

What was run:
- Read crates/render-pipeline/src/{adapter.rs (items_cached, adapt_cached),
  incremental.rs (RenderCache, hash_items), typeset.rs (block_key, cached,
  assembled lookup), lib.rs (label passes)} at 36fe7ec. Docs only, no tests.

Next step:
- Commander decision on the proposal (unchanged).

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
@d-q222

d-q222 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

daniel-parent (subagent): review fix pushed as fb97120

You were right about §2.2. I read the cache code at 36fe7ec, and this is exactly what it does:

  • RenderCache has three maps. adapted holds each block's inline → item conversion, keyed in items_cached. That key hashes labels_fp, which covers the whole labels.values and labels.pages maps, into every block's key unconditionally. So any label value or page change re-adapts every block.
  • blocks (typeset.rs block_key) and assembled use the same block key. That key hashes the adapted items, style_fingerprint and the flags, not labels_fp. A block whose re-adapted items are unchanged still hits those two maps. So the per-edit cost is a whole-document adapter pass rather than a whole-document layout. Invalidation is still whole-document at the adapter layer, not per referrer.
  • RenderCache::stats() counts blocks only, so step 0 as written would not have surfaced this.

Doc changes:

  • §2.2 now states the above precisely. It also notes that each label pass has its own labels_fp: a page move misses in the later passes, and a renumbering misses in every pass.
  • Option A has a new step 2: key each block only on the labels it reads, never on a whole-table fingerprint, with label lookups going through the recording accessor from risk (b). The later steps are renumbered.
  • Option A's expected latency now says the 30–40 ms depends on step 2. With a whole-table key, a renumbering edit costs a full layout, roughly the cold 203 ms.
  • §4 now lists two gaps in the render pipeline: the per-block label key and page seeding. It also says its existing reuse is weaker evidence for A than the doc claimed.
  • §5 step 0 adds an adapted counter and three edit kinds: no label change, renumbering, and a page move. The recommended render-pipeline first wins are to narrow labels_fp, then seed pages. Phase 1 now includes the per-block key and a renumbering acceptance check. "Why A" and open question 3 are updated to match.

This PR is docs only, so no tests were run.

d-q222 added a commit that referenced this pull request Sep 15, 2026
…575 step 0)

What changed:
- docs/design/incremental-cross-references-measurements.md: adapted /
  blocks / assembled misses, layout passes and median latency for the
  no-label, renumber and move-label edits on #535's 450-section article
  and a +toc+pageref variant, with the exact commands, plus a stage split
  showing warm == cold latency because adapt_cached's
  split_at_page_breaks walks the source prefix per block (quadratic).

What was run:
- cargo run --release --example xref_measure -- 5 450 (exit=0; table in the doc)
- cargo run --release --example stages -- doc150.tex 2 / doc535.tex 2
- cargo test --test incremental --test cache_counters:
  test result: ok. 1 passed; 0 failed; 1 ignored (incremental, 375.30s)
  test result: ok. 1 passed; 0 failed (cache_counters)

Next step: Commander ruling on #575; the adapter prefix scans are a
separate render-pipeline performance finding for its owner.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
@GoKubar
GoKubar merged commit 5d76e77 into main Sep 15, 2026
21 checks passed
GoKubar pushed a commit that referenced this pull request Sep 15, 2026
… no behaviour change) (#613)

* render-pipeline: count adapted/assembled cache lookups and label passes (measurement only)

What changed:
- RenderCache gains plain Cell counters for `adapted` and `assembled`
  hits/misses and a label-pass counter bumped once per render_cached
  layout pass; `counters()` returns them with the existing `blocks`
  stats as `CacheCounters` (`since` for deltas). `stats()` is unchanged.
  Nothing on the render path reads the counters: no behaviour change.
- examples/xref_measure.rs: #535's 450-section article (byte-identical
  generator) plus a +toc+pageref variant, through protocol::handle_line
  with a warm cache; three edits (no-label, renumber, move-label).
- tests/cache_counters.rs: the counters count cold misses, warm hits and
  passes.

What was run:
- cargo test --test cache_counters: test result: ok. 1 passed; 0 failed
- generator check against #535's large_doc_bench.rs: identical; 637761 bytes

Next step: full measurement (5 runs, 450 sections) and the results doc.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz

* docs: cross-reference invalidation measured through flashtex-render (#575 step 0)

What changed:
- docs/design/incremental-cross-references-measurements.md: adapted /
  blocks / assembled misses, layout passes and median latency for the
  no-label, renumber and move-label edits on #535's 450-section article
  and a +toc+pageref variant, with the exact commands, plus a stage split
  showing warm == cold latency because adapt_cached's
  split_at_page_breaks walks the source prefix per block (quadratic).

What was run:
- cargo run --release --example xref_measure -- 5 450 (exit=0; table in the doc)
- cargo run --release --example stages -- doc150.tex 2 / doc535.tex 2
- cargo test --test incremental --test cache_counters:
  test result: ok. 1 passed; 0 failed; 1 ignored (incremental, 375.30s)
  test result: ok. 1 passed; 0 failed (cache_counters)

Next step: Commander ruling on #575; the adapter prefix scans are a
separate render-pipeline performance finding for its owner.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
@GoKubar
GoKubar deleted the agent/daniel-parent/incremental-xref-design branch September 16, 2026 04: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.

2 participants