# pascalkuthe/imara-diff architecture

> Architecture diagram of the GitHub repository pascalkuthe/imara-diff, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pascalkuthe/imara-diff
- Repository: https://github.com/pascalkuthe/imara-diff

## Overview

#### 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: [`src/histogram.rs`](https://github.com/pascalkuthe/imara-diff/blob/master/src/histogram.rs) (Histogram diff), [`src/myers.rs`](https://github.com/pascalkuthe/imara-diff/blob/master/src/myers.rs) (Myers diff), [`src/lib.rs`](https://github.com/pascalkuthe/imara-diff/blob/master/src/lib.rs) (Change sink), [`src/intern.rs`](https://github.com/pascalkuthe/imara-diff/blob/master/src/intern.rs) (Token interning)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_input["Input preparation"]
  node_sources["Token sources<br/>input adapter<br/>[sources.rs]"]
  node_intern["Token interning<br/>preprocessing<br/>[intern.rs]"]
end

subgraph group_algorithms["Diff algorithms"]
  node_myers["Myers diff<br/>linear-space engine<br/>[myers.rs]"]
  node_myers_preprocess["Myers preprocessing<br/>pipeline stage<br/>[preprocess.rs]"]
  node_middle_snake["Middle snake search<br/>split finder<br/>[middle_snake.rs]"]
  node_histogram["Histogram diff<br/>anchor-based engine<br/>[histogram.rs]"]
  node_histogram_lcs["Low-frequency anchors<br/>anchor selection<br/>[lcs.rs]"]
  node_list_pool["Compact list pool<br/>working storage<br/>[list_pool.rs]"]
end

subgraph group_output["Edit delivery"]
  node_change_sink["Change sink<br/>callback boundary<br/>[lib.rs]"]
  node_postprocess["Edit postprocessing<br/>alignment stage<br/>[postprocess.rs]"]
  node_slider["Slider heuristic<br/>boundary heuristic"]
  node_unified_diff["Unified diff renderer<br/>text renderer<br/>[unified_diff.rs]"]
end

node_public_api["Public API &amp; dispatch<br/>Rust crate API<br/>[lib.rs]"]
node_library_consumers["Library consumers<br/>downstream integration"]

node_sources -->|"normalizes tokens"| node_intern
node_intern -->|"prepared inputs"| node_public_api
node_public_api -->|"selects"| node_myers
node_public_api -->|"selects"| node_histogram
node_myers -->|"prepares regions"| node_myers_preprocess
node_myers_preprocess -->|"searches splits"| node_middle_snake
node_middle_snake -->|"drives recursive regions"| node_myers
node_histogram -->|"selects anchors"| node_histogram_lcs
node_histogram -->|"uses"| node_list_pool
node_myers -->|"emits edit ranges"| node_change_sink
node_histogram -->|"emits edit ranges"| node_change_sink
node_change_sink -->|"valid edit script"| node_postprocess
node_postprocess -->|"refines boundaries"| node_slider
node_slider -->|"aligned changes"| node_unified_diff
node_change_sink -->|"callback events"| node_library_consumers
node_unified_diff -->|"formatted text"| node_library_consumers

click node_public_api "https://github.com/pascalkuthe/imara-diff/blob/master/src/lib.rs"
click node_sources "https://github.com/pascalkuthe/imara-diff/blob/master/src/sources.rs"
click node_intern "https://github.com/pascalkuthe/imara-diff/blob/master/src/intern.rs"
click node_change_sink "https://github.com/pascalkuthe/imara-diff/blob/master/src/lib.rs"
click node_myers "https://github.com/pascalkuthe/imara-diff/blob/master/src/myers.rs"
click node_myers_preprocess "https://github.com/pascalkuthe/imara-diff/blob/master/src/myers/preprocess.rs"
click node_middle_snake "https://github.com/pascalkuthe/imara-diff/blob/master/src/myers/middle_snake.rs"
click node_histogram "https://github.com/pascalkuthe/imara-diff/blob/master/src/histogram.rs"
click node_histogram_lcs "https://github.com/pascalkuthe/imara-diff/blob/master/src/histogram/lcs.rs"
click node_list_pool "https://github.com/pascalkuthe/imara-diff/blob/master/src/histogram/list_pool.rs"
click node_postprocess "https://github.com/pascalkuthe/imara-diff/blob/master/src/postprocess.rs"
click node_slider "https://github.com/pascalkuthe/imara-diff/blob/master/src/slider_heuristic.rs"
click node_unified_diff "https://github.com/pascalkuthe/imara-diff/blob/master/src/unified_diff.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sources,node_intern toneBlue
class node_myers,node_myers_preprocess,node_middle_snake,node_histogram,node_histogram_lcs,node_list_pool toneAmber
class node_change_sink,node_postprocess,node_slider,node_unified_diff toneMint
class node_public_api,node_library_consumers toneNeutral
```

## Components

### Input preparation

- [Token sources](https://github.com/pascalkuthe/imara-diff/blob/master/src/sources.rs): input adapter · Adapts arbitrary collections into diff token inputs. · `src/sources.rs`
- [Token interning](https://github.com/pascalkuthe/imara-diff/blob/master/src/intern.rs): preprocessing · Compacts and equates tokens for efficient comparison and reuse. · `src/intern.rs`

### Diff algorithms

- [Myers diff](https://github.com/pascalkuthe/imara-diff/blob/master/src/myers.rs): linear-space engine · Computes a valid edit script with divide-and-conquer search. · `src/myers.rs`
- [Myers preprocessing](https://github.com/pascalkuthe/imara-diff/blob/master/src/myers/preprocess.rs): pipeline stage · Trims and prepares common regions before search. · `src/myers/preprocess.rs`
- [Middle snake search](https://github.com/pascalkuthe/imara-diff/blob/master/src/myers/middle_snake.rs): split finder · Finds divide-and-conquer split points. · `src/myers/middle_snake.rs`
- [Histogram diff](https://github.com/pascalkuthe/imara-diff/blob/master/src/histogram.rs): anchor-based engine · Preferred histogram/patience-derived diff algorithm. · `src/histogram.rs`
- [Low-frequency anchors](https://github.com/pascalkuthe/imara-diff/blob/master/src/histogram/lcs.rs): anchor selection · Finds useful anchors that partition comparison regions. · `src/histogram/lcs.rs`
- [Compact list pool](https://github.com/pascalkuthe/imara-diff/blob/master/src/histogram/list_pool.rs): working storage · Manages histogram working structures. · `src/histogram/list_pool.rs`

### Edit delivery

- [Change sink](https://github.com/pascalkuthe/imara-diff/blob/master/src/lib.rs): callback boundary · Receives equal, inserted, and deleted token ranges. · `src/lib.rs`
- [Edit postprocessing](https://github.com/pascalkuthe/imara-diff/blob/master/src/postprocess.rs): alignment stage · Adjusts valid edits toward readable diff boundaries. · `src/postprocess.rs`
- [Slider heuristic](https://github.com/pascalkuthe/imara-diff/blob/master/src/slider_heuristic.rs): boundary heuristic · Shifts boundaries across equivalent surrounding tokens. · `src/slider_heuristic.rs`
- [Unified diff renderer](https://github.com/pascalkuthe/imara-diff/blob/master/src/unified_diff.rs): text renderer · Formats emitted changes as unified-diff text. · `src/unified_diff.rs`

### Other components

- [Public API & dispatch](https://github.com/pascalkuthe/imara-diff/blob/master/src/lib.rs): Rust crate API · Selects diff algorithms and exposes the shared sink contract. · `src/lib.rs`
- **Library consumers**: downstream integration · UI highlighters, secondary diffs, and renderers consume change events.

## Connections

- 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
