fix(profiler): reduce memory usage for compression#4058
Merged
Conversation
BenchmarksBenchmark execution time: 2025-10-28 12:14:12 Comparing candidate commit a37f42c in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 24 metrics, 0 unstable metrics. |
Contributor
|
nsrip-dd
force-pushed
the
PROF-12641-shared-zstd-encoder
branch
from
October 24, 2025 12:33
cfe3f71 to
6e78e2e
Compare
The zstd compression library uses ~8MiB per compressor by default, primarily for the back-reference window. See this parameter: https://pkg.go.dev/github.com/klauspost/compress/zstd#WithWindowSize Since we have an encoder per profile type, this leads to a noticable increase in memory usage after switching to zstd by default. We can make the window smaller, but this can negatively affect the compression ratio. Instead, we can just use a single encoder and share it between the profile types. This commit does the bare minimum to implement a single encoder. It's a bit kludgy to use a separate global lock to guard access to the encoder. But it's awkward to plumb the synchronization around and keep it more encapsulated without a bigger refactor.
nsrip-dd
force-pushed
the
PROF-12641-shared-zstd-encoder
branch
from
October 27, 2025 15:56
6e78e2e to
a6be279
Compare
nsrip-dd
marked this pull request as ready for review
October 27, 2025 16:06
7 tasks
felixge
approved these changes
Oct 28, 2025
Contributor
Author
|
/merge |
|
View all feedbacks in Devflow UI.
The expected merge time in
|
nsrip-dd
added a commit
that referenced
this pull request
Oct 29, 2025
Switching to zstd compression noticeably increased memory usage from the profiler, since we were creating a zstd encoder per profile type, and each encoder uses ~8MiB of memory. Because of this, we switched back to the privous compression scheme by default. #4058 improved the memory usage by sharing a single zstd encoder between all the profile types. With that fix in place we can switch back to zstd compression by default.
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.
The zstd compression library uses ~8MiB per compressor by default,
primarily for the back-reference window. See this parameter:
https://pkg.go.dev/github.com/klauspost/compress/zstd#WithWindowSize
Since we have an encoder per profile type, this leads to a noticable
increase in memory usage after switching to zstd by default. We can
make the window smaller, but this can negatively affect the compression
ratio. Instead, we can just use a single encoder and share it between
the profile types.
This PR does the bare minimum to implement a single encoder. It's a
bit kludgy to use a separate global lock to guard access to the encoder.
But it's awkward to plumb the synchronization around and keep it more
encapsulated without a bigger refactor.
This will probably make our cycle time slightly longer, since we now wait
for all the processing to complete serially before advancing to the next
profile cycle. It's hard to quantify exactly how much since it depends on
how much profiling data the program produces.
Also worth noting: the execution tracer and CPU profile APIs take a
writer when they start, rather than when we read the data. The tracer in
particular periodically writes out data as it's running. The CPU profiler
technically only writes data to the writer when it's stopped. Either way,
we don't want to hold the global lock in a way that would block either
of these things from completing. So this PR collects this data in a separate
buffer and (re)compresses with the lock held after stopping collection.
We should still come out ahead memory-usage wise by not using 8MiB
per profile type for compression.