Skip to content

diffview: diff a large file three times faster - #12

Merged
frinfo702 merged 1 commit into
mainfrom
diff-intern-lines
Nov 26, 2026
Merged

frinfo702 merged 1 commit into
mainfrom
diff-intern-lines

Conversation

@frinfo702

Copy link
Copy Markdown
Owner

A diff of a file of 100,000 lines took 66 ms before it drew a line. Most of the time went to the regular expression that scopesOf ran on each line, and to go-udiff's splitLines, which decodes every rune to find the line ends.

  • isDeclaration reads the words at the start of a line by hand instead of the regular expression. The cheap check for a word at column 0 now comes first.
  • Edits replaces udiff.Lines, in the way of @pierre/patches: it finds line ends with strings.IndexByte, diffs integer ids of the lines, and slices the new text instead of copying it. No new dependency.

Both give the same output as before. TestIsDeclarationMatchesRegexp checks 200,000 random lines against the old regular expression, and TestEditsMatchUdiff checks 10,000 random pairs of texts against udiff.Lines.

BenchmarkDiffHunks, 100,000 lines, Apple M4, mean of 10 runs:

Case Before After
one line changed in 2,000 65.9 ms, 40.9 MB 21.3 ms, 24.2 MB
2,000 lines inserted 40.2 ms, 41.6 MB 15.3 ms, 24.8 MB

The largest cost left is the splitLines inside udiff.ToUnifiedDiff, about a third of the time. Removing it needs our own hunk builder, so it is not in this PR.

A diff of a file of 100,000 lines took 66 ms before it drew a line. Most
of the time went to the regular expression that scopesOf ran on each
line to find the declarations, and to go-udiff's splitLines, which
decodes every rune to find the line ends.

isDeclaration now reads the words at the start of a line by hand, and
matches the same lines as the regular expression, which a test keeps as
the reference. The cheap check for a word at column 0 comes first, so
most top-level lines skip it.

Edits replaces udiff.Lines, in the way of @pierre/patches: it finds the
line ends with strings.IndexByte, diffs integer ids of the lines, and
slices the new text instead of copying it. It returns the same edits as
udiff.Lines.

BenchmarkDiffHunks, 100,000 lines, Apple M4:

    one line changed in 2,000:  65.9 ms, 40.9 MB -> 21.3 ms, 24.2 MB
    2,000 lines inserted:       40.2 ms, 41.6 MB -> 15.3 ms, 24.8 MB
@frinfo702
frinfo702 merged commit 75cfcf3 into main Nov 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant