Skip to content

core: copy zip entries through File.extract's pooled buffer - #14949

Merged
sipsma merged 1 commit into
mainfrom
sipsma/file-extract-zip-copy-buffer
Oct 29, 2026
Merged

sipsma merged 1 commit into
mainfrom
sipsma/file-extract-zip-copy-buffer

Conversation

@sipsma

@sipsma sipsma commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

File.extract writes each zip entry with io.CopyBuffer and a pooled 128 KiB buffer, but that buffer was never used. The destination is an *os.File, so io.CopyBuffer hands the copy to File.ReadFrom. That has no fast path from a zip entry, so it falls back to io.Copy, which allocates a new 32 KiB buffer for every file extracted.

This passes only the file's io.Writer, as internal/fsutil/send.go already does on the reader side, so the pooled buffer is used. Nothing else changes: the copy loop, short-write detection and error handling are the same. Chunks are 128 KiB instead of 32 KiB.

Measured on a cold first build of dagger's CLI with the per-package Go build (github.com/dagger/go), which extracts every module it downloads with File.extract. A 16-core arm64 machine, fresh engines, 6 rounds, each paired against the same engine without this change:

Change
Engine memory allocated −1.6 GiB of 8.9 GiB
Engine CPU −1.9 CPU-s of about 46, lower in every round
GC cycles −10 of 60, fewer in every round
First build, wall time −0.34 s median, faster in 5 of 6 rounds
Edit builds unchanged (they don't extract)
Engine peak memory unchanged

The existing TestExtract* unit tests in ./core cover what extraction produces, and they pass.

@grouville
grouville self-requested a review October 29, 2026 06:24
io.CopyBuffer hands the copy to the destination's ReadFrom when it has
one, and *os.File's falls back to io.Copy for a zip entry, which
allocates a new 32 KiB buffer per file: the pooled buffer was never
used. In a cold first build of dagger's CLI that was 1.6 GiB of the
engine's 8.9 GiB allocated.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/file-extract-zip-copy-buffer branch from 40b8c1c to 8b1b6a6 Compare October 29, 2026 06:26
@sipsma
sipsma merged commit 994fceb into main Oct 29, 2026
93 checks passed
@sipsma
sipsma deleted the sipsma/file-extract-zip-copy-buffer branch October 29, 2026 07:00
sipsma added a commit that referenced this pull request Oct 30, 2026
poolsCopy passed the opened *os.File to io.CopyBuffer, which hands the
copy to the file's WriteTo when it has one. A hash has no fast path to
take, so WriteTo fell back to a copy with its own new buffer for every
file, and the pooled 32 KiB buffer went unused. Hiding WriteTo makes
CopyBuffer use the pooled buffer, as #14949 did for
ReadFrom in zip extraction.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit that referenced this pull request Oct 30, 2026
poolsCopy passed the opened *os.File to io.CopyBuffer, which hands the
copy to the file's WriteTo when it has one. A hash has no fast path to
take, so WriteTo fell back to a copy with its own new buffer for every
file, and the pooled 32 KiB buffer went unused. Hiding WriteTo makes
CopyBuffer use the pooled buffer, as #14949 did for
ReadFrom in zip extraction.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit that referenced this pull request Oct 31, 2026
poolsCopy passed the opened *os.File to io.CopyBuffer, which hands the
copy to the file's WriteTo when it has one. A hash has no fast path to
take, so WriteTo fell back to a copy with its own new buffer for every
file, and the pooled 32 KiB buffer went unused. Hiding WriteTo makes
CopyBuffer use the pooled buffer, as #14949 did for
ReadFrom in zip extraction.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit that referenced this pull request Nov 2, 2026
poolsCopy passed the opened *os.File to io.CopyBuffer, which hands the
copy to the file's WriteTo when it has one. A hash has no fast path to
take, so WriteTo fell back to a copy with its own new buffer for every
file, and the pooled 32 KiB buffer went unused. Hiding WriteTo makes
CopyBuffer use the pooled buffer, as #14949 did for
ReadFrom in zip extraction.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit that referenced this pull request Nov 2, 2026
poolsCopy passed the opened *os.File to io.CopyBuffer, which hands the
copy to the file's WriteTo when it has one. A hash has no fast path to
take, so WriteTo fell back to a copy with its own new buffer for every
file, and the pooled 32 KiB buffer went unused. Hiding WriteTo makes
CopyBuffer use the pooled buffer, as #14949 did for
ReadFrom in zip extraction.

Signed-off-by: Erik Sipsma <[email protected]>
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