December 1, 2026 - 6 min read
How Collate diffs 400 files in 76 ms
Most diff viewers feel fast until you switch branches on a big change, edit a file while a coding agent writes three others, or open a generated 400,000-line file. Collate is built so scrolling, typing, and staging never wait on git or diffing - even when hundreds of files changed.
1. Keep git and diffing off the UI thread
In src/watcher.rs, a dedicated watcher thread runs git status, reads changed blobs into a snapshot, computes every file diff in parallel, and sends a finished Prepared value to the UI thread. The UI thread swaps in the prepared diffs and rebuilds virtual rows without ever spawning git subprocesses or running diff algorithms on the frame loop.
To avoid polling churn while still reacting instantaneously when you or an editor saves a file, Collate pairs FSEvents (on macOS) and inotify (on Linux) with a strict filter for internal git metadata:
pub fn relevant(path: &std::path::Path, git: &[PathBuf]) -> bool {
let Some(rel) = git.iter().find_map(|d| path.strip_prefix(d).ok()) else { return true };
matches!(
rel.components().next().map(|c| c.as_os_str().to_string_lossy()).as_deref(),
Some("index" | "HEAD" | "refs" | "packed-refs" | "MERGE_HEAD"
| "CHERRY_PICK_HEAD" | "REVERT_HEAD" | "rebase-merge" | "rebase-apply")
)
}Only working-tree edits and the git paths that actually alter a diff (index, HEAD, refs, packed-refs, and active merge/rebase state) wake up the watcher. Object writes, reflogs, lock files, and Collate's own backups are ignored.
2. Linear-space Myers O(ND) with bounded worst cases
Collate's diff engine in src/diff/myers.rs implements Myers' O(ND) difference algorithm using divide-and-conquer on the middle snake, so memory stays proportional to the inputs rather than quadratic in edit distance. Two practical details make a noticeable difference on real codebases:
- Fast line interning with
rustc-hash: Before running Myers, lines are interned into integer IDs so line comparisons reduce to cheap equality checks. Profiling showed standard SipHash accounted for roughly a fifth of total diffing time, so Collate switched torustc-hash(already in the dependency tree from Rune). - Bounded wholesale rewrites (
MAX_D = 1024): Past 1,024 edits inside one subproblem,middle_snakestops searching and splits at the furthest diagonal reached, keeping pathologically rewritten files bounded in time without degrading output quality. - One algorithm for lines and words: Because
diff<T: Eq>is generic over any comparable sequence, the same linear-space Myers solver runs over interned lines for file hunks and over tokens for inline word-level highlights.
3. Parallel work-stealing and DiffCache
Inside watcher::prepare, Collate caches built FileDiff values in a DiffCache keyed by (content_hash, context_lines, ignore_whitespace). When one file changes in a 416-file review, the other 415 entries match the cache key immediately and are skipped - leaving only roughly 6 ms of work.
For cold opens or switches where multiple files need diffing at once, worker threads pull missing indices via an atomic counter (AtomicUsize) instead of static chunking. Big files often cluster in a directory, so dynamic work stealing keeps one thread from getting stuck behind five giant files while other cores sit idle. After moved-block detection runs across the file list, raw file text buffers are dropped before crossing to the UI thread.
4. Virtual, memoised rows on the GPU
Collate renders on the Rune UI framework (Metal-backed on macOS, native GPU rendering on Linux). Diff rows are virtualized and memoised in src/diff/state.rs: each row keeps a stable key tied to its line and only rebuilds when its display state or theme version changes.
Measured numbers
The repository ships a reproducible benchmark generator in scripts/bench-set.py that clones fixed tags of Linux and Kubernetes and applies deterministic edits. Medians of 7 runs on an Apple M5 Pro:
| Workload | Scope | Open | Refresh | Scroll frame (p99) |
|---|---|---|---|---|
| Large review | 416 files, 95,280 changed lines | 76 ms | 12 ms | 9.2 ms |
| Linux v6.12 | 323 files, 39,407 changed lines | 76 ms | 35 ms | 10.3 ms |
| Kubernetes v1.31.0 | 298 files, 26,926 changed lines | 55 ms | 29 ms | 9.5 ms |
| One huge file | 400,000-line edit + 400,000-line new file | 122 ms | 54 ms | 8.4 ms |
Memory stays compact too: 390 changed files fit in 5.5 MB, and on the Linux and Kubernetes trees most of the refresh latency comes from running git status across tens of thousands of tracked paths.