Code changes aren't always as simple as added and removed lines.
Functions move. Variables get renamed. Large sections get reorganized. Sometimes, a relatively small refactor can make a traditional line-based diff surprisingly difficult to read.
We wanted to explore a different approach.
So we built Treediff, an open-source structural code comparison engine.
GitHub: https://github.com/A35-777/treediff
Website: https://treediff.pages.dev/
The problem we wanted to tackle
Traditional diff tools are extremely useful, but line-based comparison has limitations when code undergoes substantial structural changes.
Consider a function that moves to another part of a file. A line-oriented comparison may show a large deletion and addition rather than making the relationship between the two versions clear.
We wanted an engine that could reason about source-code structure.
What is Treediff?
Treediff is a structural code-diff engine written in Rust.
Rather than relying exclusively on textual line comparisons, it uses syntax-tree-based analysis to identify relationships between code elements.
Its architecture includes:
- Tree-sitter-based language parsing
- Structural matching and hierarchical comparison
- Merkle-style subtree fingerprints
- Move and rename detection
- Repository-level comparison capabilities
- CLI and reusable library interfaces
- Python bindings and machine-readable output
How does it approach code comparison?
The general process is:
- Parse the source files into structural representations.
- Generate fingerprints for relevant subtrees.
- Match corresponding structures across versions.
- Identify modifications, additions, removals and potential movements.
- Produce a structured comparison.
The goal is to make code changes easier for both developers and automated tools to understand.
Performance and benchmarking
We also wanted to measure performance rather than simply claim that our approach was faster.
Our repository includes a benchmark suite comparing Treediff with established tools, including Difftastic, Diffmantic, Diffsitter and GumTree.
The published results include different workload sizes and execution measurements.
You can inspect the benchmark methodology and results directly in the repository:
https://github.com/A35-777/treediff
These measurements are workload-dependent, and we welcome independent reproduction and comparisons.
Who might find it useful?
Treediff is intended for developers and teams working on:
- Code comparison and review
- Repository analysis
- Refactoring analysis
- Developer tooling
- Automated code-processing workflows
- Applications that need structured change information
It can be used as a standalone CLI or incorporated into software through its available library interfaces.
Try it and explore the project
GitHub: https://github.com/A35-777/treediff
Website: https://treediff.pages.dev/
We would especially appreciate feedback from developers who work with large repositories, complicated refactors and structural code analysis.
A question for other developers:
What do you think is the biggest weakness of existing code-diff tools when dealing with substantial refactoring?
We're interested in real examples and constructive criticism.
Top comments (0)