Repository navigation
Conversation
(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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #84, closes #50.
This makes
LCSand 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
LCSjumps straight to the iterations that can change a candidate. Each candidate'sbuffer2indexis 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 takesLCSfrom O(matching pairs) to about O(n log n).LCStrims 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.diff3Mergeno longer crashes on large inputs.okBuffer.push(...items)threwRangeError: Maximum call stack size exceededabove about 200k items on Node (about 1M on Bun). It now copies with a loop.merge,mergeDiff3andmergeDigInare no longer quadratic. They rebuilt the result withconcatfor every region, which took 3–4 s with many conflicts.diff3MergeRegionswalks hunks by index instead of callingshift()in a loop.Results (best of several runs, Bun / Node)
diffComma=o)mergediff3MergeThe only overhead is that
LCSalone runs about 10% slower on all-unique lines on Bun. That disappears in the full merge.Checking that output didn't change
mainon 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.Math.imul.diff3Mergeand amergewith 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:
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