Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

diffs: bound word-diff cost, memoize, strip CRLF

Pranav P6cf2fd588358
diff --git a/packages/diffs/src/parse.ts b/packages/diffs/src/parse.ts
index 4f06839d..2fa4a5e1 100644
--- a/packages/diffs/src/parse.ts
+++ b/packages/diffs/src/parse.ts
@@ -40,7 +40,9 @@ function finalizeFile(file: FileDiff): FileDiff {
*/
export function parsePatch(patch: string): ParsedPatch {
const files: FileDiff[] = [];
- const lines = patch.split("\n");
+ // Normalize CRLF so Windows-style patches don't leak a trailing "\r" into
+ // every line's text and the hunk header.
+ const lines = patch.replace(/\r\n/g, "\n").split("\n");
let current: FileDiff | null = null;
let currentHunk: DiffHunk | null = null;
diff --git a/packages/diffs/src/react/DiffBody.tsx b/packages/diffs/src/react/DiffBody.tsx
index 507260c2..4f67e655 100644
--- a/packages/diffs/src/react/DiffBody.tsx
+++ b/packages/diffs/src/react/DiffBody.tsx
@@ -1,4 +1,4 @@
-import { Fragment, type ReactNode } from "react";
+import { Fragment, useMemo, type ReactNode } from "react";
import { languageFromPath, tokenizeLine, type Language } from "../highlight";
import type { DiffHunk, DiffLine, DiffSegment, FileDiff } from "../types";
@@ -102,7 +102,10 @@ function HunkRows({
lineDiffType: LineDiffType;
hunkIndex: number;
}) {
- const lines = lineDiffType === "word" ? applyWordDiff(hunk.lines) : hunk.lines;
+ const lines = useMemo(
+ () => (lineDiffType === "word" ? applyWordDiff(hunk.lines) : hunk.lines),
+ [hunk, lineDiffType],
+ );
return (
<>
<div className="cgd-line cgd-line--hunk" role="row">
diff --git a/packages/diffs/src/react/FilesChanged.tsx b/packages/diffs/src/react/FilesChanged.tsx
index af2fe6db..1fa40ff6 100644
--- a/packages/diffs/src/react/FilesChanged.tsx
+++ b/packages/diffs/src/react/FilesChanged.tsx
@@ -1,4 +1,4 @@
-import { useState, type ReactNode } from "react";
+import { memo, useState, type ReactNode } from "react";
import { PatchDiff, type PatchDiffOptions } from "./PatchDiff";
@@ -25,7 +25,7 @@ export interface FilesChangedProps {
emptyState?: ReactNode;
}
-function FileRow({ item, diffOptions }: { item: FilesChangedItem; diffOptions?: PatchDiffOptions }) {
+const FileRow = memo(function FileRow({ item, diffOptions }: { item: FilesChangedItem; diffOptions?: PatchDiffOptions }) {
const [open, setOpen] = useState(item.defaultOpen ?? false);
return (
<div className={`cgd-fc-row${open ? " cgd-fc-row--open" : ""}`}>
@@ -58,7 +58,7 @@ function FileRow({ item, diffOptions }: { item: FilesChangedItem; diffOptions?:
) : null}
</div>
);
-}
+});
export function FilesChanged({ files, className, diffOptions, emptyState }: FilesChangedProps) {
const rootClassName = className != null ? `cgd-fc ${className}` : "cgd-fc";
diff --git a/packages/diffs/src/wordDiff.ts b/packages/diffs/src/wordDiff.ts
index 337cc129..47eb2835 100644
--- a/packages/diffs/src/wordDiff.ts
+++ b/packages/diffs/src/wordDiff.ts
@@ -19,6 +19,19 @@ export function diffWords(before: string, after: string): {
const n = a.length;
const m = b.length;
+ // The LCS table is O(n·m) in time and memory. A single long minified/bundled
+ // line pair (tens of thousands of symbols each) would allocate billions of
+ // cells and hang/OOM the tab — and this runs synchronously during render.
+ // Bail out to a single removed/added segment when the pairing is too large;
+ // the line still renders, just without intra-line word highlighting.
+ const LCS_CELL_BUDGET = 200_000;
+ if (n * m > LCS_CELL_BUDGET) {
+ return {
+ before: [{ kind: "removed", value: before }],
+ after: [{ kind: "added", value: after }],
+ };
+ }
+
// LCS table.
const lcs: number[][] = Array.from({ length: n + 1 }, () => new Array<number>(m + 1).fill(0));
for (let i = n - 1; i >= 0; i -= 1) {