Skip to content

Speed up LCS and merges on large or repetitive inputs - #95

Merged
bhousel merged 2 commits into
mainfrom
perf
Nov 12, 2026
Merged

bhousel merged 2 commits into
mainfrom
perf

Conversation

@bhousel

@bhousel bhousel commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Closes #84, closes #50.

This makes LCS and the merge functions much faster on large or repetitive inputs. Output is unchanged.

Where the time went

Profiling showed 99% of the time inside LCS's inner loop, which visits every pair of positions where the two inputs have equal items. A 50k-line file with lots of blank lines and } has about 80 million such pairs. More than 99.8% of those iterations did nothing, because a candidate already ended at that position. Only about one per row did real work.

Changes

  • LCS jumps straight to the iterations that can change a candidate. Each candidate's buffer2index is marked as taken. Small equivalence classes (16 or fewer items) scan a flag array, and large ones (e.g. blank lines) use a segment tree to find the next free index. On repetitive input, this takes LCS from O(matching pairs) to about O(n log n).
  • LCS trims the common prefix. A shared prefix is always matched item for item, so this can't change the result, and unchanged or nearly unchanged inputs become nearly free. Suffix trimming is left out because it changes which LCS is picked when there are ties.
  • diff3Merge no longer crashes on large inputs. okBuffer.push(...items) threw RangeError: Maximum call stack size exceeded above about 200k items on Node (about 1M on Bun). It now copies with a loop.
  • merge, mergeDiff3 and mergeDigIn are no longer quadratic. They rebuilt the result with concat for every region, which took 3–4 s with many conflicts.
  • diff3MergeRegions walks hunks by index instead of calling shift() in a loop.

Results (best of several runs, Bun / Node)

Workload Before After
50k-line merge (blank lines, braces) 3.9 s / 3.5 s 9.5 ms / 10.5 ms
10k blank lines, diffComm 1.3 s / 1.0 s 1.4 ms / 1.7 ms
#84 case 1 (word lists) 5.0 ms / 5.2 ms 0.5 ms / 1.3 ms
#84 case 2 (100k-character strings, a = o) 178 ms / 164 ms 1.3 ms / 1.4 ms
10k conflicts, merge 2.5 s / 758 ms 63 ms / 106 ms
600k lines, diff3Merge 126 ms / crash 10 ms / 44 ms
All-unique lines, many hunks 77 ms / 168 ms 59 ms / 108 ms

The only overhead is that LCS alone runs about 10% slower on all-unique lines on Bun. That disappears in the full merge.

Checking that output didn't change

  • Every exported function was compared against the previous main on 330k seeded random inputs plus all of the benchmark workloads. The output is identical. The same check was also run against variants that force every class through the tree path or through the scan path.
  • The seeded PRNG in the LCS random test lost precision and cycled after about 15k steps. It now uses Math.imul.
  • New tests cover a 1M-line diff3Merge and a merge with 10k conflicts (1 s timeout). Both fail on the old code, by crashing and by timing out.

axosoft/diff3

#84 pointed to axosoft/diff3, which replaces Hunt–McIlroy with the O(NP) algorithm by Wu, Manber and Myers. A comparison against an O(NP) implementation written from the paper found:

  • On near-identical inputs it is a few milliseconds faster.
  • With many edits it is much slower: 2.2 s against 25 ms on the many-hunks case, and 172 ms against 11 ms on unrelated 5k-line texts.
  • It picks a different LCS when there are ties, so diff and merge output would change for existing users.

So this PR keeps Hunt–McIlroy and only takes the two smaller ideas: walking hunks by index, and copying with loops instead of spread.

One case is still slow: long, completely unrelated inputs with few distinct items. For example, two unrelated 5k-length DNA strings take about 90 ms. Every exact LCS algorithm pays that cost.

This PR also includes a02b832, which adds tests for #50 (empty lines) and #89.

🤖 Generated with Claude Code

bhousel and others added 2 commits November 11, 2026 22:52
(closes: #50, re: #89)

- diffComm: the empty-lines example from #50
- LCS: check against a brute-force LCS on mostly-blank random inputs
- LCS: regression test with a timeout for large inputs with many empty lines

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(closes: #84, closes: #50)

- LCS: trim the common prefix, and skip straight to the matches that can
  change a candidate (a flag scan for small equivalence classes, a segment
  tree for large ones), instead of visiting every matching pair
- merge, mergeDiff3, mergeDigIn: build the result in place instead of
  with `concat` in a loop, which was quadratic
- diff3Merge: copy with a loop instead of `push(...items)`, which overflowed
  the call stack on large inputs
- diff3MergeRegions: iterate hunks by index instead of `shift()`
- Fix the seeded PRNG in the LCS random test (use `Math.imul`)
- Add tests for very large merges and many conflicts

Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bhousel
bhousel merged commit a95466b into main Nov 12, 2026
1 check passed
@bhousel
bhousel deleted the perf branch November 12, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Poor performance Tests for data with empty lines

1 participant