Skip to content

Commit 70abe7e

Browse files
authored
1 parent b8f761f commit 70abe7e

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/vs/editor/common/diff/algorithms/myersDiffAlgorithm.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,23 @@ export class MyersDiffAlgorithm implements IDiffAlgorithm {
3939

4040
loop: while (true) {
4141
d++;
42-
for (k = -d; k <= d; k += 2) {
43-
if (!timeout.isValid()) {
44-
return DiffAlgorithmResult.trivialTimedOut(seq1, seq2);
45-
}
46-
47-
const maxXofDLineTop = k === d ? -1 : V.get(k + 1); // We take a vertical non-diagonal
48-
const maxXofDLineLeft = k === -d ? -1 : V.get(k - 1) + 1; // We take a horizontal non-diagonal (+1 x)
42+
if (!timeout.isValid()) {
43+
return DiffAlgorithmResult.trivialTimedOut(seq1, seq2);
44+
}
45+
// The paper has `for (k = -d; k <= d; k += 2)`, but we can ignore diagonals that cannot influence the result.
46+
const lowerBound = -Math.min(d, seq2.length + (d % 2));
47+
const upperBound = Math.min(d, seq1.length + (d % 2));
48+
for (k = lowerBound; k <= upperBound; k += 2) {
49+
// We can use the X values of (d-1)-lines to compute X value of the longest d-lines.
50+
const maxXofDLineTop = k === upperBound ? -1 : V.get(k + 1); // We take a vertical non-diagonal (add a symbol in seq1)
51+
const maxXofDLineLeft = k === lowerBound ? -1 : V.get(k - 1) + 1; // We take a horizontal non-diagonal (+1 x) (delete a symbol in seq1)
4952
const x = Math.min(Math.max(maxXofDLineTop, maxXofDLineLeft), seq1.length);
5053
const y = x - k;
54+
if (x > seq1.length || y > seq2.length) {
55+
// This diagonal is irrelevant for the result.
56+
// TODO: Don't pay the cost for this in the next iteration.
57+
continue;
58+
}
5159
const newMaxX = getXAfterSnake(x, y);
5260
V.set(k, newMaxX);
5361
const lastPath = x === maxXofDLineTop ? paths.get(k + 1) : paths.get(k - 1);

0 commit comments

Comments
 (0)