fix: optimize LCS diff from O(m*n) to O(n) space#329
Merged
tomasz-tomczyk merged 2 commits intomainfrom Apr 21, 2026
Merged
Conversation
…rg's algorithm The previous LCS implementation allocated a full (m+1)*(n+1) int matrix, which for a 10K-line file consumes ~400MB. Replace with Hirschberg's divide-and-conquer algorithm that uses only two rows (O(n) space, ~80KB for 10K lines) while producing identical diff output. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Fixes gocritic ifElseChain lint. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
c0ad555 to
abbc126
Compare
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.
Summary
ComputeLineDiffwith Hirschberg's divide-and-conquer algorithmBenchmark results (Apple M2 Pro, count=3)
Before (O(m*n) DP matrix)
After (Hirschberg O(n) space)
Impact
The Hirschberg algorithm trades modest CPU time for dramatically less memory. At 5K lines: 206MB → 6.6MB. This prevents OOM on large AI-generated plans while keeping runtime under 75ms — well within acceptable for an interactive tool.
Test plan
-race🤖 Generated with Claude Code