BDiff: Block-aware and Accurate Text-based Code Differencing

Yao Lu, Wanwei Liu, Tanghaoran Zhang, Kang Yang, Yang Zhang, Wenyu Xu, Longfei Sun, Xinjun Mao, Shuzheng Gao, Michael R. LyuarXiv 2025 · DOI

Abstract

Standard code differencing tools often fail to capture the structural intent of developers, representing block-level changes as disjointed line edits. BDiff is a novel text-based algorithm that recognizes complex edit actions like block moves and copies. By modeling code changes as a weighted bipartite graph and applying the Kuhn-Munkres algorithm, BDiff generates edit scripts that are 28% smaller and significantly more readable than traditional tools, while maintaining the speed and language-neutrality of text-based methods.

Introduction

Code reviews are a cornerstone of collaborative programming, yet the tools used to visualize changes—"diffs"—have remained largely unchanged for decades. Most tools, including git diff, utilize the Myers algorithm, which operates at a textual line level. While efficient, this approach often fails to capture the structural reality of code changes. For instance, moving a function or wrapping a block in a conditional is often displayed as a series of unrelated deletions and insertions.

Tree-based differencing tools offer a solution by analyzing Abstract Syntax Trees (ASTs), but they are often slow, language-specific, and prone to failure on syntactically incorrect code. BDiff bridges this gap, providing a "middle way" that combines the speed of text-based diffing with the structural awareness of tree-based methods.

Methodology

BDiff operates through a three-phase pipeline designed to identify and optimize cross-version mappings:

  1. Candidate Mapping: The process begins with a standard Git diff to establish a baseline of deleted, added, and unchanged lines. BDiff then scans these "hunks" for more complex Edit Actions (EAs), specifically targeting Line Splitting, Line Merging, Line Updating, Block Moving (BM), and Block Copying (BC).
  2. Optimal Mapping via Kuhn-Munkres: To resolve ambiguities—such as whether a line is a new insertion or a moved version of an old line—BDiff models the problem as a weighted bipartite graph. It employs the Kuhn-Munkres (KM) algorithm to find the optimal set of mappings that minimizes the final Edit Script size. The weighting mechanism prioritizes larger blocks, high context similarity, and physical proximity.
  3. Edit Script Deduction: Finally, the algorithm generates a sequence of instructions to transform the source code into the target version based on the optimal mappings.
The BDiff workflow: from initial Git diff to final Edit Script generation via the Kuhn-Munkres algorithm.

Evaluation and Results

The researchers evaluated BDiff across Java, Python, and XML datasets, comprising nearly 3,000 real-world cases and 3,000 mutation-based cases.

  • Conciseness and Accuracy: BDiff reduced Edit Script (ES) size by an average of 28% compared to standard text-based tools. In mutation testing, it achieved a 95.3% accuracy rate in identifying intended edit actions.
  • Human Readability: In a study involving 10 raters, BDiff achieved a readability score of 5.73/6, outperforming both Git (4.61) and the tree-based state-of-the-art, GumTree (4.09).
  • Performance: BDiff maintains near-instant execution, averaging 0.085 seconds per diff, making it suitable for real-time version control workflows.
BDiff identifies complex edit actions like Update, Move, and Copy, providing a more intuitive view of code changes than standard line-by-line diffs.

Discussion

A key insight from the study is that "minimal" diffs are not always the most readable. By prioritizing block-level actions, BDiff aligns more closely with human cognitive models of refactoring. Interestingly, the study also found that current Large Language Models (LLMs) are not yet suitable for this task; models like GPT-5-mini and Qwen3-32B were significantly slower and prone to "hallucinating" line numbers. BDiff’s use of indentation as a proxy for structure allows it to remain language-agnostic while achieving high structural awareness.

Conclusion

BDiff represents a significant advancement in code differencing technology. By combining text-based efficiency with block-level intelligence, it provides developers with clearer, more accurate insights into code evolution. Future work will focus on refining the weighting algorithms using machine learning to further adapt to diverse coding styles.

The project is open-source and available at https://github.com/bdiff/bdiff.

Summary updated

BibTeX
@misc{lu2025bdiff,
  author = {Yao Lu and Wanwei Liu and Tanghaoran Zhang and Kang Yang and Yang Zhang and Wenyu Xu and Longfei Sun and Xinjun Mao and Shuzheng Gao and Michael R. Lyu},
  title = {{BDiff: Block-aware and Accurate Text-based Code Differencing}},
  year = {2025},
  eprint = {2510.21094},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.21094},
}
Figure