Table of Contents
Fetching ...

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. Lyu

TL;DR

BDiff introduces a block-aware, text-based code differencing algorithm that identifies block-level edit actions (BM and BC) and line-level edit actions (LD, LA, LU, LS, LM). It constructs comprehensive line/block mappings, then applies the Kuhn-Munkres algorithm on a weighted bipartite graph to minimize the edit script ($ES$) while preserving developer intent, yielding more compact and meaningful diffs. Evaluations on 2,977 real-world changes across Python, Java, and XML show BDiff achieves smaller $ES$ than baselines, competitive runtime, and high ground-truth ES correctness (95.3% match rate; 82.4% fully correct), though LLM-based tools struggle with both quality and efficiency. The work also provides an open-source, web-based visualization tool and discusses integration with SE workflows, enabling improved defect localization, change analysis, and downstream task support.

Abstract

Code differencing is a fundamental technique in software engineering practice and research. While researchers have proposed text-based differencing techniques capable of identifying line changes over the past decade, existing methods exhibit a notable limitation in identifying edit actions (EAs) that operate on text blocks spanning multiple lines. Such EAs are common in developers' practice, such as moving a code block for conditional branching or duplicating a method definition block for overloading. Existing tools represent such block-level operations as discrete sequences of line-level EAs, compelling developers to manually correlate them and thereby substantially impeding the efficiency of change comprehension. To address this issue, we propose BDiff, a text-based differencing algorithm capable of identifying two types of block-level EAs and five types of line-level EAs. Building on traditional differencing algorithms, we first construct a candidate set containing all possible line mappings and block mappings. Leveraging the Kuhn-Munkres algorithm, we then compute the optimal mapping set that can minimize the size of the edit script (ES) while closely aligning with the original developer's intent. To validate the effectiveness of BDiff, we selected five state-of-the-art tools, including large language models (LLMs), as baselines and adopted a combined qualitative and quantitative approach to evaluate their performance in terms of ES size, result quality, and running time. Experimental results show that BDiff produces higher-quality differencing results than baseline tools while maintaining competitive runtime performance. Our experiments also show the unreliability of LLMs in code differencing tasks regarding result quality and their infeasibility in terms of runtime efficiency. We have implemented a web-based visual differencing tool.

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

TL;DR

BDiff introduces a block-aware, text-based code differencing algorithm that identifies block-level edit actions (BM and BC) and line-level edit actions (LD, LA, LU, LS, LM). It constructs comprehensive line/block mappings, then applies the Kuhn-Munkres algorithm on a weighted bipartite graph to minimize the edit script () while preserving developer intent, yielding more compact and meaningful diffs. Evaluations on 2,977 real-world changes across Python, Java, and XML show BDiff achieves smaller than baselines, competitive runtime, and high ground-truth ES correctness (95.3% match rate; 82.4% fully correct), though LLM-based tools struggle with both quality and efficiency. The work also provides an open-source, web-based visualization tool and discusses integration with SE workflows, enabling improved defect localization, change analysis, and downstream task support.

Abstract

Code differencing is a fundamental technique in software engineering practice and research. While researchers have proposed text-based differencing techniques capable of identifying line changes over the past decade, existing methods exhibit a notable limitation in identifying edit actions (EAs) that operate on text blocks spanning multiple lines. Such EAs are common in developers' practice, such as moving a code block for conditional branching or duplicating a method definition block for overloading. Existing tools represent such block-level operations as discrete sequences of line-level EAs, compelling developers to manually correlate them and thereby substantially impeding the efficiency of change comprehension. To address this issue, we propose BDiff, a text-based differencing algorithm capable of identifying two types of block-level EAs and five types of line-level EAs. Building on traditional differencing algorithms, we first construct a candidate set containing all possible line mappings and block mappings. Leveraging the Kuhn-Munkres algorithm, we then compute the optimal mapping set that can minimize the size of the edit script (ES) while closely aligning with the original developer's intent. To validate the effectiveness of BDiff, we selected five state-of-the-art tools, including large language models (LLMs), as baselines and adopted a combined qualitative and quantitative approach to evaluate their performance in terms of ES size, result quality, and running time. Experimental results show that BDiff produces higher-quality differencing results than baseline tools while maintaining competitive runtime performance. Our experiments also show the unreliability of LLMs in code differencing tasks regarding result quality and their infeasibility in terms of runtime efficiency. We have implemented a web-based visual differencing tool.
Paper Structure (42 sections, 3 equations, 21 figures, 3 tables, 2 algorithms)

This paper contains 42 sections, 3 equations, 21 figures, 3 tables, 2 algorithms.

Figures (21)

  • Figure 1: A real case of line updating, block moving, and block copying (psf/black, e1e8909, black.py)
  • Figure 2: Examples of block-level EAs
  • Figure 3: The BDiff algorithm framework
  • Figure 4: The multiple mapping problem
  • Figure 5: A screenshot of BDiff webpage
  • ...and 16 more figures