Skip to content

diffr: defer language query compilation until first use - #1017

Closed
milanb17 wants to merge 1 commit into
codex/reapply-diffr-after-releasefrom
codex/diffr-lazy-initialization
Closed

milanb17 wants to merge 1 commit into
codex/reapply-diffr-after-releasefrom
codex/diffr-lazy-initialization

Conversation

@milanb17

@milanb17 milanb17 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Config::compile() builds Tree-sitter annotation and highlighting queries for every configured language, even when a host only needs one. Add an opt-in Config::prepare() that assembles sources immediately and compiles each language, including its embedded languages, on first use. Cache successful results and errors per configuration.

Config::compile()  → prepare sources → compile every configured language (existing CLI behavior)
Config::prepare()  → prepare sources
  first diff      → compile its language + embedded languages
  later diff      → reuse compiled queries

Language lookup now returns Result; diff entry points propagate configuration errors while retaining typed QueryConflict errors inside anyhow::Error. CLI debug callers use their already-validated configuration.

This PR contains only the core change and a native timing example. It is based on codex/reapply-diffr-after-release, independently of #1011. The browser's one-line switch from compile() to prepare() was used for measurements but is intentionally left in the website branch. No deployment is included.

Evidence

Measured locally with Node 24.18.1 and release WASM on the browser branch containing #1011. Timings exclude download, WASM compilation/instantiation, and rendering; these are individual local runs, not CI performance thresholds.

Measurement Eager Lazy
Cold Differ constructor 1,876 ms 47 ms
First ask-composer.tsx diff from PR #998 79 ms 150 ms
Constructor + first diff 1,955 ms 198 ms
Median warm four-file PR #998 workload, five rounds 217 ms 222 ms

All 18 existing browser corpus responses are JSON-identical before and after this change. The browser engine's five native integration tests passed with the opt-in. The new core regression exercises an invalid Rust query: eager compilation rejects it immediately; prepared configuration can diff Python; repeated Rust diffs return the named query error rather than panic.

Native reproduction from diffr/, using separate processes to keep each initialization cold:

cargo run --locked --release -p diffr-core --example initialization -- eager
cargo run --locked --release -p diffr-core --example initialization -- lazy

The example reports initialization, first TSX diff, their combined time, and warm diff time. It measures the core pipeline, without browser plugins or UI. On this machine with Rust 1.99 release builds, eager initialization plus first diff took 1,614 ms versus 53 ms for lazy preparation (3.4 ms preparation + 49.7 ms first diff). Warm diff time was 112–113 µs for both. These native numbers use the small inline TSX example, not the PR #998 workload above.

Validation: cargo test --locked (596 passed across the workspace, including the 294 language-context cases and WASM plugin integration), cargo fmt --all -- --check, and git diff --check.

Merge Danger

Door: two-way; no persisted data changes.

Blast Radius: shared core API. Params::language becomes fallible and diff entry points return anyhow::Result, requiring downstream direct callers to handle errors. Existing Config::compile() callers retain eager validation. Hosts opting into prepare() discover invalid query syntax/tags only when that language is first used, and pay that language's compilation cost on its first diff. Query sources remain in memory for deferred compilation. Browser adoption and any change to the CLI's validation policy are separate.

AI-assisted implementation and validation with Codex.

Keep eager configuration validation available while allowing hosts to compile and cache only languages they use. Propagate deferred query errors and include a cold initialization comparison example. AI-assisted with Codex.

Agent-Session: 01a1197a-7e1b-7742-a9bd-04ed4353bed1
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