fix(diff): correct overlap detection and bounds in linear space Myers#46
fix(diff): correct overlap detection and bounds in linear space Myers#46KimNorgaard merged 2 commits intomainfrom
Conversation
This commit resolves two critical pathfinding bugs in the linear space Myers algorithm (`findMiddleSnake`) that caused it to produce suboptimal diffs or silently fail to explore valid search spaces. 1. Fix forward search overlap detection: During the forward search overlap check (when `delta` is odd), the algorithm incorrectly checked the reverse vector `vr` at `offset + (delta - k)`. However, the reverse search already maps its furthest-reaching `x` bounds to the same forward diagonal `k`. Checking the wrong index caused the algorithm to miss the true optimal middle snake, leading to longer, suboptimal edit scripts. This is fixed by correctly checking `reverseIdx := offset + k`. 2. Fix vector sizing and offset for negative delta: When sequence A is shorter than sequence B (`delta < 0`), the forward-diagonal `k` in the reverse search can reach down to `-maxDiff + delta`. Because `offset` was previously only adjusted for positive `delta`, calculating `offset + k` would result in negative indices. This caused the reverse search to silently trigger bounds checks and skip exploring half of its required search grid. The `vectorSize` and `offset` are now correctly adjusted when `delta < 0` to accommodate the full bounds.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses two significant bugs in the linear space Myers diff algorithm's Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request addresses two critical bugs in the linear space Myers diff algorithm (findMiddleSnake). The first fix correctly calculates the vector size and offset when the first sequence is shorter than the second (delta < 0), preventing silent out-of-bounds errors and ensuring the entire search space is covered. The second fix corrects the overlap detection logic during the forward search, ensuring the algorithm finds the truly optimal edit script by checking the correct index in the reverse search vector. The changes are well-explained and appear to correctly resolve the bugs. I have one minor suggestion to improve the code's readability for future maintenance.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit resolves two critical pathfinding bugs in the linear space Myers algorithm (
findMiddleSnake) that caused it to produce suboptimal diffs or silently fail to explore valid search spaces.Fix forward search overlap detection: During the forward search overlap check (when
deltais odd), the algorithm incorrectly checked the reverse vectorvratoffset + (delta - k). However, the reverse search already maps its furthest-reachingxbounds to the same forward diagonalk. Checking the wrong index caused the algorithm to miss the true optimal middle snake, leading to longer, suboptimal edit scripts. This is fixed by correctly checkingreverseIdx := offset + k.Fix vector sizing and offset for negative delta: When sequence A is shorter than sequence B (
delta < 0), the forward-diagonalkin the reverse search can reach down to-maxDiff + delta. Becauseoffsetwas previously only adjusted for positivedelta, calculatingoffset + kwould result in negative indices. This caused the reverse search to silently trigger bounds checks and skip exploring half of its required search grid. ThevectorSizeandoffsetare now correctly adjusted whendelta < 0to accommodate the full bounds.