Skip to content

compression: copy a mostly-empty compressed frame out instead of shrinking it - #323

Merged
iainmcgin merged 1 commit into
mainfrom
iain/gzip-copy-out
Sep 26, 2026
Merged

iainmcgin merged 1 commit into
mainfrom
iain/gzip-copy-out

Conversation

@iainmcgin

Copy link
Copy Markdown
Collaborator

#291 shrinks a compressed frame's working buffer with shrink_to_fit when it is more than half empty. For a large input that buffer is above glibc's mmap threshold, so the shrink is an mremap, and freeing the few-KB chunk it leaves never raises glibc's dynamic threshold. Every later compression of a large input then maps a fresh buffer, and flate2's Rust backends zero-fill its whole spare capacity, faulting in every page.

On a c7i.metal-24xl (turbo off, pinned), gzip of a 1 MiB compressible input:

per compress page faults per call
v0.9.0 553 µs 0
main (#291, shrink_to_fit) 957 µs 297
this PR (copy out) 497 µs 0

This copies the compressed bytes into a buffer of their own and drops the working buffer, in the helper gzip and zstd share, which raises the threshold (up to glibc's 32 MiB cap) so later calls reuse heap memory. A queued frame now retains exactly its compressed size; the retention tests assert that. They would also pass with shrink_to_fit, since the allocator effect is not testable in-process.

…nking it

`compressed_output` shrank a working buffer that was more than half
empty with `shrink_to_fit`. For a large input that buffer comes from
mmap, and glibc serves the shrink with mremap, leaving a chunk too small
to raise its dynamic mmap threshold when it is freed. Every later
compression of a large input therefore mapped a fresh buffer, and
flate2's Rust backends zero-fill the whole spare output capacity on each
call, faulting in every page of it.

Copy the compressed bytes into a buffer of their own and drop the
working buffer instead. Freeing the full-size buffer raises the
threshold, so later calls reuse heap memory, and the frame now retains
exactly its compressed size.

Signed-off-by: Iain McGinniss <309153+iainmcgin@users.noreply.github.com>
@iainmcgin
iainmcgin marked this pull request as ready for review September 26, 2026 01:53
@iainmcgin
iainmcgin added this pull request to the merge queue Sep 26, 2026
Merged via the queue into main with commit 1983106 Sep 26, 2026
14 checks passed
@iainmcgin
iainmcgin deleted the iain/gzip-copy-out branch September 26, 2026 02:09
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