casblob: reuse the compressed-output buffer to cut upload memory churn#907
Open
mjonss wants to merge 2 commits into
Open
casblob: reuse the compressed-output buffer to cut upload memory churn#907mjonss wants to merge 2 commits into
mjonss wants to merge 2 commits into
Conversation
Under a burst of concurrent uploads, bazel-remote could be OOM-killed by a transient Go heap overshoot even though idle/steady-state memory is small. Heap profiling (alloc_space) pointed at zstd.(*Encoder).encodeAll: the write path compresses each blob in 1 MiB chunks and called EncodeAll(in, nil) per chunk, so a fresh output slice was allocated for every chunk of every upload. The chunk size is exactly 1<<20, and klauspost's EncodeAll only pre-allocates an output buffer when len(src) < 1<<20 (strictly less-than). A full chunk therefore starts from a nil dst and grows it by repeated append-doubling, so each 1 MiB chunk churned several MiB of transient garbage. With no memory backpressure on concurrent Puts, the allocation rate outran the GC and the process was killed. Thread a reusable dst through zstdimpl.EncodeAll (both the pure-Go and cgo backends already accept one) and have casblob.WriteAndClose allocate a single output buffer, sized to compressBound(chunkSize), reused across all chunks of the blob. Sizing to the ZSTD_compressBound worst case ensures EncodeAll never has to grow (and reallocate) the buffer, even for incompressible chunks whose output is slightly larger than the input. A microbenchmark of the write path (16 MiB blob, incompressible data) shows per-upload allocations drop from ~79 MB/op to ~1.1 MB/op.
Author
|
I found this while setting up bazel-remote on a small node with limited memory, and it had OOM issues, so after investigating I came up with this PR. |
Author
|
I decided to make a smaller change, instead of using a sync.Pool since the benefit was very good to start with for my use case. If you find it better to also include a sync.Pool also for the |
mostynb
reviewed
Jul 13, 2026
mostynb
left a comment
Collaborator
There was a problem hiding this comment.
Thanks- this looks pretty good, I'm a little worried that the go zstd library might not provide a guarantee on the upper limit of the size of the compressed output. Maybe we should ask Klaus?
- zstdimpl.EncodeAll: rename params to (src, dst), matching the order github.com/klauspost/compress/zstd uses. - WriteAndClose: drop the compressBound() helper; size the reused output buffer inline to ZSTD_COMPRESSBOUND (srcSize + srcSize>>8 for a >= 128 KiB input) and document the bound for both backends. - GetZstdReadCloser: pass a nil dst (restores the pre-change allocate-fresh behaviour on this one-shot, rarely-hit recompress path). - benchmark: fix a stale comment reference (link to the PR instead of a non-existent local file) and trim the comments.
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.
Problem
Under a burst of concurrent uploads, bazel-remote can suffer a large transient heap overshoot (and OOM on memory-constrained hosts) despite small steady-state memory.
alloc_spaceheap profiling attributes the bulk tozstd.(*Encoder).encodeAll, reached viacasblob.WriteAndClose.Root cause
The zstd write path compresses each blob in 1 MiB chunks and called
EncodeAll(in, nil)per chunk, allocating a fresh output slice for every chunk of every upload. The chunk size is exactly1<<20, and klauspost'sEncodeAllonly pre-allocates an output buffer whenlen(src) < 1<<20(strictly less-than). A full 1 MiB chunk therefore starts from anildst and grows it by repeatedappend-doubling — several MiB of transient garbage per chunk.Fix
Thread a reusable
dstthroughzstdimpl.EncodeAll(both the pure-Go and cgo backends already accept one) and havecasblob.WriteAndCloseallocate a single output buffer, sized tocompressBound(chunkSize), reused across all chunks of the blob. Sizing to theZSTD_compressBoundworst case guaranteesEncodeAllnever has to grow (and reallocate) the buffer, even for incompressible chunks whose compressed output is slightly larger than the input.This complements #775 (which pooled the input chunk buffer) by removing the per-chunk output allocation, and is complementary to #475 (which tunes the encoder's internal memory rather than the output buffer).
Numbers
BenchmarkWriteAndCloseZstd(16 MiB blob, incompressible data), added in this PR:All existing tests pass.