Skip to content

Reduce diff allocations and fix line counting - #43

Merged
andrew merged 1 commit into
mainfrom
perf/diff-allocations
Sep 13, 2026
Merged

andrew merged 1 commit into
mainfrom
perf/diff-allocations

Conversation

@andrew

@andrew andrew commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Hunk lines in generateSimpleDiff and generateAddedDiff wrote each output line as prefix + line + "\n", two temporary strings per line (plus a []byte→string copy in the added-file path). Write the three parts directly to the builder.

countLines used bufio.Scanner, which stops silently at the first line over 64 KiB (minified JS, source maps) and returned a short count with no error. Replaced with bytes.Count plus a trailing-newline check; TestCountLines/long_line covers a 100 KB line that returned 0 before.

Compare accumulated TotalAdded/TotalDeleted only for modified files even though added files already reported LinesAdded. Deleted files now report LinesDeleted and both totals sum across all file types.

Adds BenchmarkGenerateAddedDiff (10k-line file) and BenchmarkGenerateSimpleDiff (5k lines, half changed). Interleaved benchstat, arm64 darwin, n=10:

                     │   before    │             after              │
                     │   sec/op    │   sec/op     vs base           │
GenerateAddedDiff      603.6µ ± 8%   440.3µ ± 6%  -27.05% (p=0.000)
GenerateSimpleDiff     307.8µ ± 8%   234.9µ ± 5%  -23.67% (p=0.000)
                     │  allocs/op  │  allocs/op   vs base           │
GenerateAddedDiff     10038.0 ± 0%    38.0 ± 0%   -99.62% (p=0.000)
GenerateSimpleDiff     2543.0 ± 0%    39.0 ± 3%   -98.47% (p=0.000)

Hunk lines and generateAddedDiff wrote prefix+line+newline via string
concat; write the three parts directly. countLines used bufio.Scanner,
which stops silently on any line over 64 KiB and returned a short count;
use bytes.Count instead. Deleted files now report LinesDeleted and both
totals include added and deleted files, not just modified hunks.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved blocking issues were identified.

Pull request overview

Improves diff generation performance, fixes long-line counting, and includes added/deleted files in aggregate totals.

Changes:

  • Reduces allocations when generating diffs.
  • Replaces scanner-based line counting.
  • Adds coverage and benchmarks for updated behavior.
File summaries
File Summary
diff/diff.go Optimizes diff output and corrects line-count and aggregate-total logic.
diff/diff_test.go Adds coverage for line counts, totals, and benchmarks.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@andrew
andrew merged commit 0760c09 into main Sep 13, 2026
6 checks passed
@andrew
andrew deleted the perf/diff-allocations branch September 13, 2026 13:31
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.

2 participants