Skip to content

perf: add pooling for contexts - #167

Closed
KowalskiThomas wants to merge 3 commits into
1.xfrom
kowalski/perf-reuse-contexts
Closed

perf: add pooling for contexts#167
KowalskiThomas wants to merge 3 commits into
1.xfrom
kowalski/perf-reuse-contexts

Conversation

@KowalskiThomas

@KowalskiThomasKowalskiThomas commented Jun 19, 2026

Copy link
Copy Markdown

What is this PR?

This PR is a proposal to make compression and decompression of single payloads faster across the board by pooling compression and decompression contexts instead of constantly creating new ones and discarding after operation.

The implementation is simple:

  • We add two new cctxWrapper and dctxWrapper structs as well as two sync.Pool's to manage them.
  • We use ZSTD_compressCCtx (respectively ZSTD_decompressDCtx) instead of ZSTD_compress (respectively ZSTD_decompress), passing a pooled context object into them.

As far as I can tell, this change doesn't really come with a significant trade-off. We effectively delegate memory management for contexts to the two Pool's, but I expect this to be a non issue in the vast majority of cases: an application that would repeatedly compress/decompress payloads would in practice constantly be allocating/freeing contexts (leading to probably higher memory usage due to objects waiting to be GC'd, and higher fragmentation). On the other hand, an application that makes a "burst" of calls to compression/decompression functions would potentially allocate a lot of contexts at once, but sync.Pool (eventually) automatically frees unused objects during GC.

Testing

I ran the benchmarks I created and temporarily added for that optimisation (see commit history). The results are as follows.

macOS / MacBook Pro M4 Max

BenchmarkBeforeAfterImprovement
Compress 1K4425 ns4120 ns~7%
Compress 8K18900 ns16830 ns~11%
Compress 64K149900 ns148900 ns~1%
Decompress 1K1983 ns1862 ns~6%
Decompress 8K3908 ns3747 ns~4%
Decompress 64K23748 ns22919 ns~3.5%
Compress 8K (parallel)1551 ns1404 ns~9.5%

Linux / DoE workspace

BenchmarkBeforeAfterImprovement
Compress 1K18900 ns11420 ns~40%
Compress 8K62020 ns52810 ns~15%
Compress 64K463900 ns446900 ns~3.7%
Decompress 1K10251 ns3193 ns~69%
Decompress 8K14533 ns7330 ns~50%
Decompress 64K53790 ns46770 ns~13%
Compress 8K (parallel)15690 ns13410 ns~15%

@datadog-prod-us1-6

This comment has been minimized.

@KowalskiThomas
KowalskiThomasforce-pushed the kowalski/perf-reuse-contexts branch from bd5bca4 to 1cd20a8CompareJune 19, 2026 08:13
@KowalskiThomas
KowalskiThomasforce-pushed the kowalski/perf-reuse-contexts branch from 1cd20a8 to 30f7a4eCompareJune 19, 2026 08:19
@KowalskiThomasKowalskiThomas changed the title perf: reuse contextsperf: add pooling for contextsJun 19, 2026
@KowalskiThomas
KowalskiThomas marked this pull request as ready for review June 19, 2026 08:25
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@KowalskiThomas