Reduce diff allocations and fix line counting - #43
Merged
Merged
Conversation
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.
There was a problem hiding this comment.
🟢 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hunk lines in
generateSimpleDiffandgenerateAddedDiffwrote each output line asprefix + line + "\n", two temporary strings per line (plus a[]byte→stringcopy in the added-file path). Write the three parts directly to the builder.countLinesusedbufio.Scanner, which stops silently at the first line over 64 KiB (minified JS, source maps) and returned a short count with no error. Replaced withbytes.Countplus a trailing-newline check;TestCountLines/long_linecovers a 100 KB line that returned 0 before.CompareaccumulatedTotalAdded/TotalDeletedonly for modified files even though added files already reportedLinesAdded. Deleted files now reportLinesDeletedand both totals sum across all file types.Adds
BenchmarkGenerateAddedDiff(10k-line file) andBenchmarkGenerateSimpleDiff(5k lines, half changed). Interleaved benchstat, arm64 darwin, n=10: