Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(DiffViewer): line diffs for changes up to 10,000 lines in linear memory; files over 2,000 changed lines start folded (#9521)

Cindy Zhang7261e2c2cf05
diff --git a/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/benchmark.ts b/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/benchmark.ts
index 7c68dd21fd..872f888ac3 100644
--- a/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/benchmark.ts
+++ b/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/benchmark.ts
@@ -142,8 +142,9 @@ interface EditedLines {
}
/**
- * `createDiffFile` shows a file with more changed lines than this as one
- * replaced block, so a sparse-edited file must not go above it.
+ * Most changed lines in a sparse-edited file. It stays under the limit past
+ * which `createDiffFile` shows one replaced block, so sparse edits always get
+ * a line diff.
*/
const DIFF_EDIT_LIMIT = 2000;
/** A site replaces up to three lines and inserts up to two more. */
diff --git a/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx b/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx
index 3296b3a274..5685e43c13 100644
--- a/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx
+++ b/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx
@@ -426,9 +426,12 @@ export default function DiffViewerPerfPage() {
// Looks for a rendered diff now and then once per frame, so a file
// that renders late is caught at the frame it appears.
const watchFirstFile = () => {
- // Unified view renders a <table>; split view, a role="table" grid.
+ // Unified view renders a <table>; split view, a role="table" grid;
+ // a file folded for its size, only its fold row.
if (
- surfaceRef.current?.querySelector('table, [role="table"]') != null
+ surfaceRef.current?.querySelector(
+ 'table, [role="table"], [data-diff-fold]',
+ ) != null
) {
afterNextPaint(renderStart, ms => {
firstFileMs = ms;
diff --git a/packages/lab/src/DiffViewer/DiffViewer.doc.mjs b/packages/lab/src/DiffViewer/DiffViewer.doc.mjs
index 9165a5af5b..4d45e620be 100644
--- a/packages/lab/src/DiffViewer/DiffViewer.doc.mjs
+++ b/packages/lab/src/DiffViewer/DiffViewer.doc.mjs
@@ -30,7 +30,7 @@ export const docs = {
usage: {
description:
- 'Read-only, syntax-highlighted diff of one file for code review surfaces. Renders a file header (path, Added/Deleted/Renamed token, +/− line counts, a headerEnd slot, and an optional disclosure) over the file\'s hunks in unified or split (side-by-side) view. Rows use the same syntax-theme diff slots as CodeBlock with isDiff, so one syntax theme styles both. renderLineAnnotation adds a full-width row under any line (for review comments) and onLineNumberClick turns line numbers into buttons. Build the file model with parseUnifiedDiff(patch), which returns one file per file in a git or diff -u patch, or with createDiffFile({oldValue, newValue}) when you hold two versions of a text. Display options cover how a change is marked (classic +/− glyphs, bars, or none), whether changed rows are tinted, and word- or character-level highlighting of what changed inside each removed/added line pair. Each changed line carries a visually hidden "Added"/"Removed" label, so the change reads without colour in every mode. Unwrapped, line numbers and change indicators stay pinned while the code scrolls; in split view each side is its own scroller, so the split stays fixed at half and half, the sides scroll sideways in lockstep, and a row is as tall as its taller side. While code overflows sideways, sideways overscroll is off, so trackpads neither bounce past a line nor swipe the browser back. In right-to-left pages the hunk area stays left-to-right, like a code editor: annotation content reads in the page direction, and annotation rows scroll with the code. When a change is too large to compare line by line, createDiffFile marks its hunk with isReplacedBlock: it lists every old line as removed and every new line as added. DiffViewer folds those lines behind a "Large change: N lines" row, where N counts the block\'s removed plus added lines, with a Show full diff button. A block opens by itself when one of its lines has an annotation, so no review comment is hidden. Rows are plain DOM with no virtualization: collapse or window very large files.',
+ 'Read-only, syntax-highlighted diff of one file for code review surfaces. Renders a file header (path, Added/Deleted/Renamed token, +/− line counts, a headerEnd slot, and an optional disclosure) over the file\'s hunks in unified or split (side-by-side) view. Rows use the same syntax-theme diff slots as CodeBlock with isDiff, so one syntax theme styles both. renderLineAnnotation adds a full-width row under any line (for review comments) and onLineNumberClick turns line numbers into buttons. Build the file model with parseUnifiedDiff(patch), which returns one file per file in a git or diff -u patch, or with createDiffFile({oldValue, newValue}) when you hold two versions of a text. Display options cover how a change is marked (classic +/− glyphs, bars, or none), whether changed rows are tinted, and word- or character-level highlighting of what changed inside each removed/added line pair. Each changed line carries a visually hidden "Added"/"Removed" label, so the change reads without colour in every mode. Unwrapped, line numbers and change indicators stay pinned while the code scrolls; in split view each side is its own scroller, so the split stays fixed at half and half, the sides scroll sideways in lockstep, and a row is as tall as its taller side. While code overflows sideways, sideways overscroll is off, so trackpads neither bounce past a line nor swipe the browser back. In right-to-left pages the hunk area stays left-to-right, like a code editor: annotation content reads in the page direction, and annotation rows scroll with the code. When a change is too large to compare line by line, createDiffFile marks its hunk with isReplacedBlock: it lists every old line as removed and every new line as added. DiffViewer folds those lines behind a "Large change: N lines" row, where N counts the block\'s removed plus added lines, with a Show full diff button. A file with more than 2,000 other changed lines starts folded the same way, because every row costs render time. Its line diff is still there when the reader opens it. A fold opens by itself when a line it would hide has an annotation, so no review comment is hidden. Rows are plain DOM with no virtualization: collapse or window very large files.',
bestPractices: [
{
guidance: true,
diff --git a/packages/lab/src/DiffViewer/DiffViewer.test.tsx b/packages/lab/src/DiffViewer/DiffViewer.test.tsx
index daecf4afc0..eb645902b4 100644
--- a/packages/lab/src/DiffViewer/DiffViewer.test.tsx
+++ b/packages/lab/src/DiffViewer/DiffViewer.test.tsx
@@ -763,6 +763,80 @@ describe('DiffViewer', () => {
});
});
+ describe('large files', () => {
+ /** One hunk that adds `count` lines to an empty file. */
+ function addedFile(count: number): DiffViewerFile {
+ return {
+ oldPath: null,
+ newPath: 'generated.txt',
+ status: 'added',
+ isBinary: false,
+ hunks: [
+ {
+ header: `@@ -0,0 +1,${count} @@`,
+ oldStart: 0,
+ oldLines: 0,
+ newStart: 1,
+ newLines: count,
+ lines: Array.from({length: count}, (_, i) => ({
+ kind: 'added' as const,
+ content: `line ${i}`,
+ oldLineNumber: null,
+ newLineNumber: i + 1,
+ })),
+ },
+ ],
+ };
+ }
+
+ it('folds a file with more than 2,000 changed lines until the reader opens it', async () => {
+ const user = userEvent.setup();
+ const {container} = render(<DiffViewer file={addedFile(2001)} />);
+ expect(screen.getByText('Large change: 2,001 lines')).toBeTruthy();
+ expect(container.querySelector('table')).toBeNull();
+ // The header still counts every line.
+ expect(
+ screen.getByText('2,001 lines added, 0 lines removed'),
+ ).toBeTruthy();
+ const button = screen.getByRole('button', {name: 'Show full diff'});
+ button.focus();
+ await user.keyboard('{Enter}');
+ const hide = screen.getByRole('button', {name: 'Hide full diff'});
+ expect(hide.getAttribute('aria-expanded')).toBe('true');
+ expect(hide).toBe(document.activeElement);
+ expect(container.querySelectorAll('[data-diff="added"]')).toHaveLength(
+ 2001,
+ );
+ await user.click(hide);
+ expect(container.querySelector('table')).toBeNull();
+ });
+
+ it('shows a file with 2,000 changed lines without a fold', () => {
+ const {container} = render(<DiffViewer file={addedFile(2000)} />);
+ expect(screen.queryByRole('button', {name: 'Show full diff'})).toBeNull();
+ expect(container.querySelectorAll('[data-diff="added"]')).toHaveLength(
+ 2000,
+ );
+ });
+
+ it('opens a large file that has an annotated line', () => {
+ render(
+ <DiffViewer
+ file={addedFile(2001)}
+ renderLineAnnotation={ref =>
+ ref.lineNumber === 1500 ? <span>Generated?</span> : null
+ }
+ />,
+ );
+ expect(screen.getByText('Generated?')).toBeTruthy();
+ expect(
+ screen
+ .getByRole('button', {name: 'Hide full diff'})
+ .getAttribute('aria-expanded'),
+ ).toBe('true');
+ });
+ });
+
it('shows a message for binary files', () => {
const [binary] = parseUnifiedDiff(
'diff --git a/x.png b/x.png\nBinary files a/x.png and b/x.png differ\n',
diff --git a/packages/lab/src/DiffViewer/DiffViewer.tsx b/packages/lab/src/DiffViewer/DiffViewer.tsx
index 27d5827f66..a34892a15a 100644
--- a/packages/lab/src/DiffViewer/DiffViewer.tsx
+++ b/packages/lab/src/DiffViewer/DiffViewer.tsx
@@ -12,8 +12,9 @@
*
* Renders one DiffViewerFile: a file header (path, status, +/- counts, a
* caller slot, optional disclosure) over its hunks in unified or split view.
- * A hunk marked `isReplacedBlock` shows a labeled fold in place of its
- * changed lines until the reader opens it.
+ * A hunk marked `isReplacedBlock`, and a file with more than
+ * MAX_UNFOLDED_CHANGED_LINES other changed lines, show a labeled fold until
+ * the reader opens it.
* Unified view is one table. Split view is two side-by-side panes (each an
* ARIA table) whose rows share one grid's row tracks through `subgrid`, so a
* row is as tall as its taller side while each pane scrolls sideways on its
@@ -358,33 +359,6 @@ function lineAnnotation(
return isRenderable(note) ? note : null;
}
-/**
- * Replaced blocks with an annotation on one of their changed lines. Such a
- * block starts open, so a review comment is never hidden behind the fold.
- */
-function findAnnotatedBlocks(
- file: DiffViewerFile,
- renderLineAnnotation: ((ref: DiffViewerLineRef) => ReactNode) | undefined,
-): Set<number> {
- const blocks = new Set<number>();
- if (renderLineAnnotation == null) {
- return blocks;
- }
- file.hunks.forEach((hunk, hunkIndex) => {
- if (hunk.isReplacedBlock !== true) {
- return;
- }
- const isAnnotated = hunk.lines.some(line => {
- const ref = line.kind === 'context' ? null : lineRef(line, hunkIndex);
- return ref != null && lineAnnotation(renderLineAnnotation, ref) != null;
- });
- if (isAnnotated) {
- blocks.add(hunkIndex);
- }
- });
- return blocks;
-}
-
function lineRef(
line: DiffViewerLine,
hunkIndex: number,
@@ -435,6 +409,42 @@ function addInlineEmphasis(
}
}
+/**
+ * Most changed lines a file shows before the reader asks for them. Rows are
+ * not virtualized, and each row costs render time. A file with more changed
+ * lines starts folded. Lines in replaced blocks do not count, because those
+ * blocks fold on their own.
+ */
+const MAX_UNFOLDED_CHANGED_LINES = 2000;
+
+/**
+ * Hunks with an annotation on one of their lines. A fold that would hide such
+ * a line starts open, so a review comment is never hidden.
+ */
+function findAnnotatedHunks(
+ file: DiffViewerFile,
+ renderLineAnnotation: ((ref: DiffViewerLineRef) => ReactNode) | undefined,
+): Set<number> {
+ const hunks = new Set<number>();
+ if (renderLineAnnotation == null) {
+ return hunks;
+ }
+ file.hunks.forEach((hunk, hunkIndex) => {
+ const isAnnotated = hunk.lines.some(line => {
+ // A replaced block's fold keeps its context lines in view.
+ if (hunk.isReplacedBlock === true && line.kind === 'context') {
+ return false;
+ }
+ const ref = lineRef(line, hunkIndex);
+ return ref != null && lineAnnotation(renderLineAnnotation, ref) != null;
+ });
+ if (isAnnotated) {
+ hunks.add(hunkIndex);
+ }
+ });
+ return hunks;
+}
+
/**
* Tokenize a run of hunk lines with its old and new side each as continuous
* text, so constructs that span lines (block comments, template strings) keep
@@ -1300,16 +1310,48 @@ export function DiffViewer({
onCollapsedChange?.(next);
};
- // A replaced block starts folded unless one of its lines is annotated. The
- // reader's toggles are kept per file; a new file starts over.
+ let unfoldedChangedLines = 0;
+ let hasReplacedBlock = false;
+ for (const hunk of file.hunks) {
+ if (hunk.isReplacedBlock === true) {
+ hasReplacedBlock = true;
+ continue;
+ }
+ for (const line of hunk.lines) {
+ if (line.kind !== 'context') {
+ unfoldedChangedLines++;
+ }
+ }
+ }
+ const isLargeFile = unfoldedChangedLines > MAX_UNFOLDED_CHANGED_LINES;
+ const annotatedHunks =
+ isLargeFile || hasReplacedBlock
+ ? findAnnotatedHunks(file, renderLineAnnotation)
+ : null;
+
+ // A large file and a replaced block start folded unless a line they would
+ // hide is annotated. The reader's toggles are kept per file; a new file
+ // starts over.
+ const [fileToggle, setFileToggle] = useState<{
+ file: DiffViewerFile;
+ isExpanded: boolean;
+ } | null>(null);
+ const isFileFolded =
+ isLargeFile &&
+ !(fileToggle?.file === file
+ ? fileToggle.isExpanded
+ : (annotatedHunks?.size ?? 0) > 0);
+ const toggleFile = () => {
+ setFileToggle({file, isExpanded: isFileFolded});
+ };
+
const [blockToggles, setBlockToggles] = useState<{
file: DiffViewerFile;
isExpanded: ReadonlyMap<number, boolean>;
} | null>(null);
const toggles = blockToggles?.file === file ? blockToggles.isExpanded : null;
- const annotatedBlocks = findAnnotatedBlocks(file, renderLineAnnotation);
const isBlockExpanded = (hunkIndex: number) =>
- toggles?.get(hunkIndex) ?? annotatedBlocks.has(hunkIndex);
+ toggles?.get(hunkIndex) ?? annotatedHunks?.has(hunkIndex) ?? false;
const toggleBlock = (hunkIndex: number) => {
const next = new Map(toggles);
next.set(hunkIndex, !isBlockExpanded(hunkIndex));
@@ -1329,17 +1371,25 @@ export function DiffViewer({
[expandedKey],
);
+ // A folded file builds no cells, so its lines are not tokenized.
const cells = useMemo(
- () => buildCells(file, language, inlineHighlight, expandedBlocks),
- [file, language, inlineHighlight, expandedBlocks],
+ () =>
+ isFileFolded
+ ? []
+ : buildCells(file, language, inlineHighlight, expandedBlocks),
+ [isFileFolded, file, language, inlineHighlight, expandedBlocks],
);
const unifiedRows = useMemo(
- () => (viewMode === 'unified' ? buildUnifiedRows(file, cells) : []),
- [viewMode, file, cells],
+ () =>
+ viewMode === 'unified' && !isFileFolded
+ ? buildUnifiedRows(file, cells)
+ : [],
+ [viewMode, isFileFolded, file, cells],
);
const splitRows = useMemo(
- () => (viewMode === 'split' ? buildSplitRows(file, cells) : []),
- [viewMode, file, cells],
+ () =>
+ viewMode === 'split' && !isFileFolded ? buildSplitRows(file, cells) : [],
+ [viewMode, isFileFolded, file, cells],
);
let addedCount = 0;
@@ -1523,26 +1573,37 @@ export function DiffViewer({
);
};
- const renderFold = (fold: ReplacedBlockFold) => (
+ const renderFoldControl = (
+ changedLines: number,
+ isExpanded: boolean,
+ onToggle: () => void,
+ isSticky: boolean,
+ ) => (
<span
- {...stylex.props(styles.foldContent, isPinned && styles.hunkTextSticky)}>
- <span>
- {t('@astryx.diffViewer.largeChange', {count: fold.changedLines})}
- </span>
+ {...stylex.props(styles.foldContent, isSticky && styles.hunkTextSticky)}>
+ <span>{t('@astryx.diffViewer.largeChange', {count: changedLines})}</span>
<Button
size="sm"
variant="secondary"
label={
- fold.isExpanded
+ isExpanded
? t('@astryx.diffViewer.hideFullDiff')
: t('@astryx.diffViewer.showFullDiff')
}
- aria-expanded={fold.isExpanded}
- onClick={() => toggleBlock(fold.hunkIndex)}
+ aria-expanded={isExpanded}
+ onClick={onToggle}
/>
</span>
);
+ const renderFold = (fold: ReplacedBlockFold) =>
+ renderFoldControl(
+ fold.changedLines,
+ fold.isExpanded,
+ () => toggleBlock(fold.hunkIndex),
+ isPinned,
+ );
+
const renderHunkText = (header: string) => (
<span {...stylex.props(styles.ltr, isPinned && styles.hunkTextSticky)}>
{header}
@@ -1890,8 +1951,6 @@ export function DiffViewer({
);
};
- const table = viewMode === 'split' ? renderSplit() : renderUnified();
-
let body: ReactNode;
if (file.isBinary) {
body = (
@@ -1906,16 +1965,34 @@ export function DiffViewer({
</div>
);
} else {
+ // The fold row stays first whether the file is folded or not, so its
+ // button keeps focus when it toggles.
body = (
- <div
- {...withoutInlineOverscroll(
- getViewportProps<HTMLDivElement>({
- ...stylex.props(styles.viewport),
- style: scrollStyle,
- }),
- )}>
- <div {...getContentProps<HTMLDivElement>({})}>{table}</div>
- </div>
+ <>
+ {isLargeFile && (
+ <div data-diff-fold="" {...stylex.props(styles.fold)}>
+ {renderFoldControl(
+ unfoldedChangedLines,
+ !isFileFolded,
+ toggleFile,
+ false,
+ )}
+ </div>
+ )}
+ {!isFileFolded && (
+ <div
+ {...withoutInlineOverscroll(
+ getViewportProps<HTMLDivElement>({
+ ...stylex.props(styles.viewport),
+ style: scrollStyle,
+ }),
+ )}>
+ <div {...getContentProps<HTMLDivElement>({})}>
+ {viewMode === 'split' ? renderSplit() : renderUnified()}
+ </div>
+ </div>
+ )}
+ </>
);
}
diff --git a/packages/lab/src/DiffViewer/createDiffFile.test.ts b/packages/lab/src/DiffViewer/createDiffFile.test.ts
index b353a4d36f..f3928dbb17 100644
--- a/packages/lab/src/DiffViewer/createDiffFile.test.ts
+++ b/packages/lab/src/DiffViewer/createDiffFile.test.ts
@@ -21,6 +21,31 @@ function sides(file: DiffViewerFile) {
return {oldSide, newSide};
}
+/** Fewest added plus removed lines between two texts, by dynamic programming. */
+function minimalEditCount(a: string[], b: string[]): number {
+ let previous = new Array<number>(b.length + 1).fill(0);
+ for (let i = 1; i <= a.length; i++) {
+ const row = [0];
+ for (let j = 1; j <= b.length; j++) {
+ row[j] =
+ a[i - 1] === b[j - 1]
+ ? previous[j - 1] + 1
+ : Math.max(previous[j], row[j - 1]);
+ }
+ previous = row;
+ }
+ return a.length + b.length - 2 * previous[b.length];
+}
+
+/** Seeded generator, so a failing case can be replayed. */
+function createRandom(seed: number): () => number {
+ let state = seed;
+ return () => {
+ state = (state * 1103515245 + 12345) % 2147483648;
+ return state / 2147483648;
+ };
+}
+
describe('createDiffFile', () => {
it('returns no hunks for identical text', () => {
const file = createDiffFile({oldValue: 'a\nb\n', newValue: 'a\nb\n'});
@@ -159,6 +184,117 @@ describe('createDiffFile', () => {
expect(file.hunks[0].newLines).toBe(lineCount + 2);
});
+ it('finds a minimal diff for many small texts', () => {
+ // Few distinct lines make many ties and repeated lines.
+ const random = createRandom(7);
+ for (let run = 0; run < 2000; run++) {
+ const alphabet = 1 + Math.floor(random() * 4);
+ const text = () =>
+ Array.from(
+ {length: Math.floor(random() * 10)},
+ () => `line ${Math.floor(random() * alphabet)}`,
+ );
+ const a = text();
+ const b = text();
+ const file = createDiffFile({
+ oldValue: a.join('\n'),
+ newValue: b.join('\n'),
+ contextLines: 100,
+ });
+ const lines = file.hunks.flatMap(hunk => hunk.lines);
+ const {oldSide, newSide} = sides(file);
+ if (lines.length > 0) {
+ expect(oldSide).toEqual(a);
+ expect(newSide).toEqual(b);
+ }
+ expect(lines.filter(line => line.kind !== 'context')).toHaveLength(
+ minimalEditCount(a, b),
+ );
+ // Each run of changes lists its removals before its additions.
+ lines.forEach((line, i) => {
+ if (line.kind === 'removed' && i > 0) {
+ expect(lines[i - 1].kind).not.toBe('added');
+ }
+ });
+ }
+ });
+
+ it('finds a minimal diff for texts with more than a thousand changes', () => {
+ // Past a thousand changed lines a second, linear-space search takes over.
+ const random = createRandom(11);
+ for (let run = 0; run < 3; run++) {
+ const text = () =>
+ Array.from({length: 1200}, () => `line ${Math.floor(random() * 40)}`);
+ const a = text();
+ const b = text();
+ const file = createDiffFile({
+ oldValue: a.join('\n'),
+ newValue: b.join('\n'),
+ contextLines: 10_000,
+ });
+ const expected = minimalEditCount(a, b);
+ expect(expected).toBeGreaterThan(1000);
+ const {oldSide, newSide} = sides(file);
+ expect(oldSide).toEqual(a);
+ expect(newSide).toEqual(b);
+ expect(
+ file.hunks
+ .flatMap(hunk => hunk.lines)
+ .filter(line => line.kind !== 'context'),
+ ).toHaveLength(expected);
+ }
+ });
+
+ it('keeps a line diff for thousands of changes spread through a long file', () => {
+ // 1,500 lines changed in place: 3,000 changed lines in a 20,000-line file.
+ const base = Array.from({length: 20_000}, (_, i) => `line ${i}`);
+ const next = base.map((line, i) =>
+ i % 13 === 5 ? `${line} changed` : line,
+ );
+ const changedPairs = base.filter((_, i) => i % 13 === 5).length;
+ const file = createDiffFile({
+ oldValue: base.join('\n'),
+ newValue: next.join('\n'),
+ contextLines: 0,
+ });
+ expect(file.hunks.some(hunk => hunk.isReplacedBlock)).toBe(false);
+ expect(file.hunks).toHaveLength(changedPairs);
+ expect(file.hunks[0].header).toBe('@@ -6 +6 @@');
+ });
+
+ it('lowers the edit limit for a very long file', () => {
+ // 4,000 changed lines through 150,000 lines: a line diff in a shorter
+ // file, but past the work the search may spend on this one.
+ const changes = (lineCount: number) => {
+ const base = Array.from({length: lineCount}, (_, i) => `line ${i}`);
+ const step = Math.floor(lineCount / 2000);
+ const next = base.map((line, i) =>
+ i % step === 1 ? `${line} changed` : line,
+ );
+ return createDiffFile({
+ oldValue: base.join('\n'),
+ newValue: next.join('\n'),
+ });
+ };
+ expect(changes(20_000).hunks.some(hunk => hunk.isReplacedBlock)).toBe(
+ false,
+ );
+ expect(changes(150_000).hunks[0].isReplacedBlock).toBe(true);
+ });
+
+ it('diffs up to 10,000 changed lines and replaces the block past that', () => {
+ const rewrite = (lineCount: number) => {
+ const oldLines = Array.from({length: lineCount}, (_, i) => `old ${i}`);
+ const newLines = Array.from({length: lineCount}, (_, i) => `new ${i}`);
+ return createDiffFile({
+ oldValue: ['head', ...oldLines, 'tail'].join('\n'),
+ newValue: ['head', ...newLines, 'tail'].join('\n'),
+ });
+ };
+ expect(rewrite(5000).hunks[0].isReplacedBlock).toBeUndefined();
+ expect(rewrite(5001).hunks[0].isReplacedBlock).toBe(true);
+ });
+
it('handles a very long text changed only at both ends', () => {
// Every line in between is matched by the search, so the edit script
// holds one op per line: more than a call can take as spread arguments.
diff --git a/packages/lab/src/DiffViewer/createDiffFile.ts b/packages/lab/src/DiffViewer/createDiffFile.ts
index ba0bc29a23..47d953941a 100644
--- a/packages/lab/src/DiffViewer/createDiffFile.ts
+++ b/packages/lab/src/DiffViewer/createDiffFile.ts
@@ -8,11 +8,6 @@
* @position Lab utility; computes a line diff (Myers) when the caller has two
* texts rather than a patch
*
- * Shared leading and trailing lines are matched without a search. The minimal
- * search over the rest stops after MAX_EDIT_DISTANCE changed lines, so memory
- * stays bounded for any input; a middle that differs more than that becomes
- * one replaced block, marked with `isReplacedBlock` on its hunk.
- *
* SYNC: When modified, update:
* - /packages/lab/src/DiffViewer/createDiffFile.test.ts
* - /packages/lab/src/DiffViewer/DiffViewer.doc.mjs (utilities section)
@@ -64,11 +59,26 @@ function splitLines(value: string): string[] {
}
/**
- * Most changed lines the minimal search explores before giving up. The trace
- * it keeps grows with the square of this, so the cap bounds memory to a few
- * megabytes; past it the changed middle is shown as one replaced block.
+ * Most changed lines the search compares line by line; past it the changed
+ * middle is shown as one replaced block. The search keeps a few arrays the
+ * size of the input, so its memory does not grow with this limit.
+ */
+const MAX_EDIT_DISTANCE = 10_000;
+
+/**
+ * Most search work, counted as changed-middle lines times edits. The search
+ * time grows with both, so a long file gets a lower edit limit. A file of up
+ * to 50,000 lines keeps the full MAX_EDIT_DISTANCE.
+ */
+const MAX_SEARCH_WORK = 1_000_000_000;
+
+/**
+ * Most changed lines the traced search tries first. Most changes are small,
+ * and for them the traced search is faster than the linear-space one. Its
+ * trace grows with the square of the edits, so at this limit it stays near
+ * 4 MB.
*/
-const MAX_EDIT_DISTANCE = 2000;
+const TRACED_SEARCH_LIMIT = 1000;
interface EditScript {
ops: Op[];
@@ -78,9 +88,11 @@ interface EditScript {
/**
* Line edit script from `a` to `b`. Shared leading and trailing lines are
- * context without a search; the middle uses Myers' O(ND) shortest edit script,
- * or, when it needs more than MAX_EDIT_DISTANCE edits, removes the old middle
- * and adds the new one.
+ * context without a search. The middle uses a shortest edit script, or, when
+ * it needs more edits than the limit, removes the old middle and adds the new
+ * one. The limit is MAX_EDIT_DISTANCE, lowered for a long middle so the work
+ * stays under MAX_SEARCH_WORK. The traced search runs first; the linear-space
+ * search takes over when the change is larger than TRACED_SEARCH_LIMIT.
*/
function diffLines(a: string[], b: string[]): EditScript {
let head = 0;
@@ -101,7 +113,20 @@ function diffLines(a: string[], b: string[]): EditScript {
// When one side of the middle is empty, adding or removing the other side
// is already the minimal script.
const isOneSided = tailA === head || tailB === head;
- const middle = isOneSided ? null : myers(a, b, head, tailA, tailB);
+ const editLimit = Math.min(
+ MAX_EDIT_DISTANCE,
+ Math.floor(MAX_SEARCH_WORK / (tailA - head + (tailB - head))),
+ );
+ const middle = isOneSided
+ ? null
+ : (tracedEditScript(
+ a,
+ b,
+ head,
+ tailA,
+ tailB,
+ Math.min(TRACED_SEARCH_LIMIT, editLimit),
+ ) ?? shortestEditScript(a, b, head, tailA, tailB, editLimit));
if (middle != null) {
// A loop, not a spread: one call argument per op overflows the stack on
// long inputs.
@@ -123,16 +148,237 @@ function diffLines(a: string[], b: string[]): EditScript {
}
/**
- * Myers' shortest edit script over `a[start, endA)` and `b[start, endB)`,
- * with absolute indexes in the ops; null when it needs more than
- * MAX_EDIT_DISTANCE edits.
+ * Shortest edit script over `a[start, endA)` and `b[start, endB)`, with
+ * absolute indexes in the ops; null when it needs more than `maxEdits` edits.
+ *
+ * This is Myers' O(ND) search in linear space. Each step searches forward
+ * from the start and backward from the end at the same time until the two
+ * searches meet. The meeting point is on a shortest path, so the step then
+ * solves the part before it and the part after it. Lines are compared as
+ * integer ids.
+ */
+function shortestEditScript(
+ a: string[],
+ b: string[],
+ start: number,
+ endA: number,
+ endB: number,
+ maxEdits: number,
+): Op[] | null {
+ const ids = new Map<string, number>();
+ const toIds = (lines: string[], end: number) => {
+ const out = new Int32Array(end - start);
+ for (let i = start; i < end; i++) {
+ let id = ids.get(lines[i]);
+ if (id === undefined) {
+ id = ids.size;
+ ids.set(lines[i], id);
+ }
+ out[i - start] = id;
+ }
+ return out;
+ };
+ const oldIds = toIds(a, endA);
+ const newIds = toIds(b, endB);
+
+ // Each copy of a line that one side has more often than the other is an
+ // edit, so this count is a lower bound on the edits. When it is past the
+ // limit (a rewrite, for example), skip the search.
+ const surplus = new Int32Array(ids.size);
+ for (const id of oldIds) {
+ surplus[id]++;
+ }
+ for (const id of newIds) {
+ surplus[id]--;
+ }
+ let minEdits = 0;
+ for (const count of surplus) {
+ minEdits += Math.abs(count);
+ }
+ if (minEdits > maxEdits) {
+ return null;
+ }
+ // The furthest x each search reached on diagonal k (x - y), stored at
+ // index k + offset. The backward search counts x and y from the end.
+ const offset = oldIds.length + newIds.length + 1;
+ const forward = new Int32Array(2 * offset + 1);
+ const backward = new Int32Array(2 * offset + 1);
+ const ops: Op[] = [];
+
+ // A point on a shortest path through the box, or null when the path needs
+ // more than `limit` edits.
+ const meetingPoint = (
+ aLo: number,
+ aHi: number,
+ bLo: number,
+ bHi: number,
+ limit: number,
+ ): [number, number] | null => {
+ const n = aHi - aLo;
+ const m = bHi - bLo;
+ const delta = n - m;
+ const isOdd = (delta & 1) !== 0;
+ forward[offset + 1] = 0;
+ backward[offset + 1] = 0;
+ for (let d = 0; d <= Math.ceil((n + m) / 2); d++) {
+ // Forward step d can find a path of 2d - 1 edits, backward step d one
+ // of 2d edits.
+ if (2 * d - 1 > limit) {
+ return null;
+ }
+ for (let k = -d; k <= d; k += 2) {
+ let x =
+ k === -d ||
+ (k !== d && forward[offset + k - 1] < forward[offset + k + 1])
+ ? forward[offset + k + 1]
+ : forward[offset + k - 1] + 1;
+ let y = x - k;
+ while (x < n && y < m && oldIds[aLo + x] === newIds[bLo + y]) {
+ x++;
+ y++;
+ }
+ forward[offset + k] = x;
+ // The backward search has done d - 1 steps, so it holds diagonals
+ // -(d - 1)..d - 1, in its own coordinates.
+ const back = delta - k;
+ if (
+ isOdd &&
+ x <= n &&
+ y <= m &&
+ back >= 1 - d &&
+ back <= d - 1 &&
+ x + backward[offset + back] >= n
+ ) {
+ return [aLo + x, bLo + y];
+ }
+ }
+ if (2 * d > limit) {
+ return null;
+ }
+ for (let k = -d; k <= d; k += 2) {
+ let x =
+ k === -d ||
+ (k !== d && backward[offset + k - 1] < backward[offset + k + 1])
+ ? backward[offset + k + 1]
+ : backward[offset + k - 1] + 1;
+ let y = x - k;
+ while (x < n && y < m && oldIds[aHi - 1 - x] === newIds[bHi - 1 - y]) {
+ x++;
+ y++;
+ }
+ backward[offset + k] = x;
+ const front = delta - k;
+ if (
+ !isOdd &&
+ x <= n &&
+ y <= m &&
+ front >= -d &&
+ front <= d &&
+ x + forward[offset + front] >= n
+ ) {
+ const meetX = forward[offset + front];
+ return [aLo + meetX, bLo + meetX - front];
+ }
+ }
+ }
+ return null;
+ };
+
+ // Appends the script for the box to `ops`; false when the box needs more
+ // than `limit` edits. Only the outermost box sets a limit.
+ const solve = (
+ aLo: number,
+ aHi: number,
+ bLo: number,
+ bHi: number,
+ limit: number,
+ ): boolean => {
+ while (aLo < aHi && bLo < bHi && oldIds[aLo] === newIds[bLo]) {
+ ops.push({kind: 'context', a: start + aLo, b: start + bLo});
+ aLo++;
+ bLo++;
+ }
+ let tail = 0;
+ while (
+ aHi - tail > aLo &&
+ bHi - tail > bLo &&
+ oldIds[aHi - 1 - tail] === newIds[bHi - 1 - tail]
+ ) {
+ tail++;
+ }
+ const aEnd = aHi - tail;
+ const bEnd = bHi - tail;
+ if (aLo === aEnd || bLo === bEnd) {
+ if (aEnd - aLo + (bEnd - bLo) > limit) {
+ return false;
+ }
+ for (let i = aLo; i < aEnd; i++) {
+ ops.push({kind: 'removed', a: start + i});
+ }
+ for (let j = bLo; j < bEnd; j++) {
+ ops.push({kind: 'added', b: start + j});
+ }
+ } else {
+ const point = meetingPoint(aLo, aEnd, bLo, bEnd, limit);
+ if (
+ point == null ||
+ !solve(aLo, point[0], bLo, point[1], Infinity) ||
+ !solve(point[0], aEnd, point[1], bEnd, Infinity)
+ ) {
+ return false;
+ }
+ }
+ for (let t = tail; t > 0; t--) {
+ ops.push({kind: 'context', a: start + aHi - t, b: start + bHi - t});
+ }
+ return true;
+ };
+
+ if (!solve(0, oldIds.length, 0, newIds.length, maxEdits)) {
+ return null;
+ }
+ return removalsFirst(ops);
+}
+
+/**
+ * Put the removals of each run of changes before its additions, so split view
+ * pairs each removed line with the added line that replaced it.
+ */
+function removalsFirst(ops: Op[]): Op[] {
+ const sorted: Op[] = [];
+ const added: Op[] = [];
+ for (const op of ops) {
+ if (op.kind === 'added') {
+ added.push(op);
+ continue;
+ }
+ if (op.kind === 'context') {
+ for (const addedOp of added) {
+ sorted.push(addedOp);
+ }
+ added.length = 0;
+ }
+ sorted.push(op);
+ }
+ for (const addedOp of added) {
+ sorted.push(addedOp);
+ }
+ return sorted;
+}
+
+/**
+ * Myers' O(ND) shortest edit script over `a[start, endA)` and
+ * `b[start, endB)`, with absolute indexes in the ops; null when it needs more
+ * than `maxEdits` edits. It keeps every step's diagonals to trace the path
+ * back, so its memory grows with the square of the edits.
*/
-function myers(
+function tracedEditScript(
a: string[],
b: string[],
start: number,
endA: number,
endB: number,
+ maxEdits: number,
): Op[] | null {
const n = endA - start;
const m = endB - start;
@@ -143,7 +389,7 @@ function myers(
const trace: Int32Array[] = [];
outer: for (let d = 0; d <= max; d++) {
- if (d > MAX_EDIT_DISTANCE) {
+ if (d > maxEdits) {
return null;
}
trace.push(v.slice(offset - d, offset + d + 1));