fix quadratic backtracking in UnifiedDiffReader BINARY_EDITED by netliomax25-code · Pull Request #252 · java-diff-utils/java-diff-utils
Repository navigation
fix quadratic backtracking in UnifiedDiffReader BINARY_EDITED - #252
Open
netliomax25-code wants to merge 1 commit into java-diff-utils:master from netliomax25-code:binary-edited-atomic-group
Open
fix quadratic backtracking in UnifiedDiffReader BINARY_EDITED #252 netliomax25-code wants to merge 1 commit into java-diff-utils:master from netliomax25-code:binary-edited-atomic-group
Conversation
netliomax25-code commented Dec 6, 2026
Copy link Copy Markdown
Contributor
BINARY_EDITEDis^Binary\sfiles\sa/(.+)\sand\sb/(.+)\sdiffer, and the reader tries that rule on every header line it reads, before it knows what the line is.- Both groups are unbounded and greedy, so a line that keeps the
Binary files a/prefix and repeatsand b/without ever reachingdiffermakes the matcher retry every separator position, and the cost grows with the square of the line length. ThroughparseUnifiedDiffa single 128 KB line takes about 18 seconds here and a 256 KB line about 53 seconds, against single digit milliseconds for an ordinary header line of the same size. Diff text is usually something the caller received rather than produced, so the line can be supplied. - Wrapped the first group and its separator in an atomic group so the match commits to the last
and b/in the line rather than walking back through the earlier ones. The sibling rulesBINARY_ADDEDandBINARY_DELETEDeach have a single group followed by a fixed literal and stay linear on the same input, so I left them as they are.
Validation: mvn -B package on JDK 17 is green, 176 tests in the core module and 0 checkstyle violations. The new test in UnifiedDiffReaderTest fails on master after 53 seconds against its 10 second budget and passes in milliseconds with the change. I also compared the old and new patterns over the realistic and awkward header lines I could think of, including file paths that themselves contain and b/ and a line with trailing text after differ, and the captured names agree in every case.
fix quadratic backtracking in UnifiedDiffReader BINARY_EDITED
07dfeaf
This branch has not been deployed
No deployments
Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment