Skip to content

perf: reuse buffer when hashing source files for checksums#2925

Open
vmaerten wants to merge 1 commit into
mainfrom
perf/checksum-copybuffer-alloc
Open

perf: reuse buffer when hashing source files for checksums#2925
vmaerten wants to merge 1 commit into
mainfrom
perf/checksum-copybuffer-alloc

Conversation

@vmaerten

@vmaerten vmaerten commented Jul 19, 2026

Copy link
Copy Markdown
Member

Summary

io.CopyBuffer ignored the pre-allocated buffer because *os.File implements WriterTo: it took the (*os.File).WriteTo path, which allocates a fresh 32KiB buffer per file via os.genericWriteTo. Hashing many small source files thus allocated ~32KiB per file (~640MB for 20,000 files).

Wrapping the file in a plain io.Reader forces io.CopyBuffer to reuse the caller's buffer, keeping the loop allocation-free. The checksum value is unchanged.

Metric Baseline Après Delta
B/op (mémoire) 638.95 MiB 14.26 MiB −97.77%
sec/op (wall-clock) 701 ms 653 ms −6.80% (p = 0.004)
allocs/op 164.9k 164.9k ~ (unchanged)

io.CopyBuffer ignored the pre-allocated buffer because *os.File
implements WriterTo: it took the (*os.File).WriteTo path, which
allocates a fresh 32KiB buffer per file via os.genericWriteTo.
Hashing many small source files thus allocated ~32KiB per file
(~640MB for 20,000 files).

Wrapping the file in a plain io.Reader forces io.CopyBuffer to reuse
the caller's buffer, keeping the loop allocation-free. The checksum
value is unchanged.
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