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.
Collaborator
Author
|
This has landed. |
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.
This is a squashed and rebased version of #907, so I can trigger CI on it in that state before landing.