Add CompressionContext for reusing compression working memory - #250
Add CompressionContext for reusing compression working memory#250WillemKauf wants to merge 3 commits into
Conversation
Extract InternalCompress, which uses a caller-provided WorkingMemory instead of allocating its own. No behavior change: the public Compress overload allocates the working memory exactly as before. This prepares for reusing working memory across compressions.
Today, every Compress()/RawCompress() call allocates and frees an internal WorkingMemory. Callers that compress frequently, or that run with custom or per-thread allocators sensitive to large contiguous allocations, currently have no way to avoid this per-call allocation, and other mainstream codecs offer reusable contexts for exactly this purpose. Add an opaque CompressionContext holding a reusable WorkingMemory sized for kBlockSize (usable with inputs of any size and both compression levels), plus Compress()/RawCompress() overloads that use it instead of allocating internally.
Callers that must avoid heap allocation entirely can now construct a CompressionContext over a caller-provided workspace of WorkspaceSize() bytes. Internally, WorkingMemory gains a non-allocating constructor laying out its scratch space in a caller-provided buffer, and RequiredSize() factoring out the size computation its allocating constructor already performed.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Looks like an existing snappy test is broken 🙂 |
Yes, new clang compiler on macos does not like something. I'll take a look today and tomorrow and hopefully merge it without any problems. On a first pass, all looks good |
sounds good to me, TYVM for review! |
As discussed in #248,
snappyis limited today in terms of providing users a way to avoid heap allocations during compression, as well as in providing a way to reuse that scratch space across compressions.This PR adds new APIs (while maintaining existing ABI stability) which allow users to provide their own
CompressionContextto compression calls. ThisCompressionContextobject can be used to pay the price of the heap allocation withinWorkingMemoryup-front once, and is reusable for all following compressions. ACompressionContextis not thread-safe, and the intended usage is one context per thread. This is largely implemented in the second commit, whereas the first commit is a simple refactoring to enable reuse ofWorkingMemoryacross multiple calls toCompress(). In the third commit, we extend this further by allowing users to provide their own chunk of memory toCompressionContextusing whatever allocator they see fit to avoid a rawstd::allocator<char>().allocate()call (making use of thesesnappyAPIs completely allocation free). The user is responsible for the lifetime of this memory and ensuring it is freed after theCompressionContextobject is destroyed.Fixes #248.