How pascalkuthe/imara-diff works
Purpose and Entry Points
imara-diff is a Rust library for high-performance generic sequence diffs, emphasizing bounded behavior on pathological inputs. Its public API and algorithm selection live in src/lib.rs; consumers supply token sources and a change sink rather than using a CLI or service.
Input Normalization and Reuse
src/sources.rs adapts arbitrary collections into the token-oriented inputs consumed by diff engines. src/intern.rs interns/equates tokens efficiently, enabling compact comparisons and reuse of preprocessing when one source is compared against multiple targets. Pointer-compressed internal representations impose the documented ~2^31-token limit.
Algorithm Dispatch and Shared Output Contract
The library routes comparison through public algorithm APIs in src/lib.rs into implementations that emit edit operations through a shared sink/callback boundary. This separates edit-script computation from consumers such as UI highlighters, fine-grained secondary diffs, and textual renderers.
Myers Diff Pipeline
src/myers.rs implements the linear-space Myers variant. src/myers/preprocess.rs trims and prepares common regions before the core search; src/myers/middle_snake.rs finds divide-and-conquer split points. The resulting recursive/linear-space search produces ranges of equal, inserted, and deleted tokens while avoiding unbounded practical runtime through preprocessing and heuristics.
Histogram Diff Pipeline
src/histogram.rs provides the preferred histogram/patience-derived algorithm for many workloads. src/histogram/lcs.rs identifies useful low-frequency anchors, while src/histogram/list_pool.rs manages its compact working structures. Anchor selection partitions the input into smaller regions; unmatched regions become edits or are recursively diffed.
Heuristic Postprocessing
src/postprocess.rs adjusts initially valid edit boundaries toward more readable and Git/GNU-diff-like results. src/slider_heuristic.rs shifts change boundaries across equivalent surrounding tokens. This is a deliberate semantic boundary: algorithm cores establish a valid script, then postprocessing chooses presentation-friendly alignment.
Rendering and Delivery
src/unified_diff.rs turns emitted changes into unified-diff text, keeping formatting separate from sequence algorithms. Library correctness and compatibility are protected by integration fixtures in tests/integration/main.rs; robustness is additionally exercised by algorithm and renderer fuzz targets under fuzz/fuzz_targets/. The runtime is a Rust crate (MSRV 1.61), with no required network service or deployment infrastructure.
Main entry points: histogram.rs Histogram diff · myers.rs Myers diff · lib.rs Change sink · intern.rs Token interning
Components 14
Input preparation
- Token sources · Adapts arbitrary collections into diff token inputs.
src/sources.rs - Token interning · Compacts and equates tokens for efficient comparison and reuse.
src/intern.rs
Diff algorithms
- Myers diff · Computes a valid edit script with divide-and-conquer search.
src/myers.rs - Myers preprocessing · Trims and prepares common regions before search.
src/myers/preprocess.rs - Middle snake search · Finds divide-and-conquer split points.
src/myers/middle_snake.rs - Histogram diff · Preferred histogram/patience-derived diff algorithm.
src/histogram.rs - Low-frequency anchors · Finds useful anchors that partition comparison regions.
src/histogram/lcs.rs - Compact list pool · Manages histogram working structures.
src/histogram/list_pool.rs
Edit delivery
- Change sink · Receives equal, inserted, and deleted token ranges.
src/lib.rs - Edit postprocessing · Adjusts valid edits toward readable diff boundaries.
src/postprocess.rs - Slider heuristic · Shifts boundaries across equivalent surrounding tokens.
src/slider_heuristic.rs - Unified diff renderer · Formats emitted changes as unified-diff text.
src/unified_diff.rs
Other components
- Public API & dispatch · Selects diff algorithms and exposes the shared sink contract.
src/lib.rs - Library consumers · UI highlighters, secondary diffs, and renderers consume change events.
Connections 16
- Token sources → Token interning: normalizes tokens
- Token interning → Public API & dispatch: prepared inputs
- Public API & dispatch → Myers diff: selects
- Public API & dispatch → Histogram diff: selects
- Myers diff → Myers preprocessing: prepares regions
- Myers preprocessing → Middle snake search: searches splits
- Middle snake search → Myers diff: drives recursive regions
- Histogram diff → Low-frequency anchors: selects anchors
- Histogram diff → Compact list pool: uses
- Myers diff → Change sink: emits edit ranges
- Histogram diff → Change sink: emits edit ranges
- Change sink → Edit postprocessing: valid edit script
- Edit postprocessing → Slider heuristic: refines boundaries
- Slider heuristic → Unified diff renderer: aligned changes
- Change sink → Library consumers: callback events
- Unified diff renderer → Library consumers: formatted text