Skip to content

Add CompressionContext for reusing compression working memory - #250

Open
WillemKauf wants to merge 3 commits into
google:mainfrom
WillemKauf:compression-context-static
Open

Add CompressionContext for reusing compression working memory#250
WillemKauf wants to merge 3 commits into
google:mainfrom
WillemKauf:compression-context-static

Conversation

@WillemKauf

Copy link
Copy Markdown

As discussed in #248, snappy is 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 CompressionContext to compression calls. This CompressionContext object can be used to pay the price of the heap allocation within WorkingMemory up-front once, and is reusable for all following compressions. A CompressionContext is 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 of WorkingMemory across multiple calls to Compress(). In the third commit, we extend this further by allowing users to provide their own chunk of memory to CompressionContext using whatever allocator they see fit to avoid a raw std::allocator<char>().allocate() call (making use of these snappy APIs completely allocation free). The user is responsible for the lifetime of this memory and ensuring it is freed after the CompressionContext object is destroyed.

Fixes #248.

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.
@google-cla

google-cla Bot commented Jul 29, 2026

Copy link
Copy Markdown

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.

@WillemKauf

Copy link
Copy Markdown
Author
 /Users/runner/work/snappy/snappy/snappy-test.cc:469:52: error: variable 'dummyin' is uninitialized when passed as a const pointer argument here [-Werror,-Wuninitialized-const-pointer]
  469 |   if ( UncompressChunkOrAll(&dummyout, &dummylen, &dummyin, 0, Z_FINISH)
      |   

Looks like an existing snappy test is broken 🙂

@danilak-G

Copy link
Copy Markdown
Collaborator
 /Users/runner/work/snappy/snappy/snappy-test.cc:469:52: error: variable 'dummyin' is uninitialized when passed as a const pointer argument here [-Werror,-Wuninitialized-const-pointer]
  469 |   if ( UncompressChunkOrAll(&dummyout, &dummylen, &dummyin, 0, Z_FINISH)
      |   

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

@WillemKauf

Copy link
Copy Markdown
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow users to reuse working memory for snappy compression

2 participants