perf: add pooling for contexts#167
Closed
KowalskiThomas wants to merge 3 commits into
Closed
Conversation
This comment has been minimized.
This comment has been minimized.
bd5bca4 to
1cd20a8
Compare
1cd20a8 to
30f7a4e
Compare
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.
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:
cctxWrapperanddctxWrapperstructs as well as twosync.Pool's to manage them.ZSTD_compressCCtx(respectivelyZSTD_decompressDCtx) instead ofZSTD_compress(respectivelyZSTD_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, butsync.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
Linux / DoE workspace