From e3f07954b9dc4cf73ef2e031d25cd89e64171e34 Mon Sep 17 00:00:00 2001 From: Compression Team Date: Tue, 10 Nov 2026 19:53:05 -0800 Subject: [PATCH] Evict oversized Zstd compression and decompression contexts instead of returning them to `RecyclingPool`. Manage `ZSTD_CCtx` and `ZSTD_DCtx` handles in `ZstdWriterBase` and `ZstdReaderBase` via `RecyclingPool::RawGet()` and `RawPut()`, returning only bounded-size contexts to the pool while freeing oversized compression and streaming decompression workspaces on completion, `Reset()`, or destruction instead of keeping them pinned in the LIFO pool across subsequent small streams. PiperOrigin-RevId: 1018771307 --- riegeli/zstd/zstd_reader.cc | 30 ++++++++++++++++++--- riegeli/zstd/zstd_reader.h | 13 +++++++++- riegeli/zstd/zstd_writer.cc | 52 +++++++++++++++++++++++++++---------- riegeli/zstd/zstd_writer.h | 16 +++++++++++- 4 files changed, 91 insertions(+), 20 deletions(-) diff --git a/riegeli/zstd/zstd_reader.cc b/riegeli/zstd/zstd_reader.cc index 449e316a..3fbb94f2 100644 --- a/riegeli/zstd/zstd_reader.cc +++ b/riegeli/zstd/zstd_reader.cc @@ -46,6 +46,25 @@ namespace riegeli { +namespace { + +// Maximum `ZSTD_DCtx` size to return to `RecyclingPool` (4 MiB). Covers +// streaming decompression up to `windowLog == 21` (~2.5 MiB), while evicting +// `windowLog >= 22` (> 4 MiB) workspaces instead of retaining them in the pool. +constexpr size_t kMaxRecycledDCtxBytes = size_t{4} << 20; + +} // namespace + +void ZstdReaderBase::ZSTD_DCtxRecycler::operator()(ZSTD_DCtx* ptr) const { + RIEGELI_ASSERT_NE(pool_, nullptr) + << "Failed precondition of ZSTD_DCtxRecycler: " + "default-constructed recycler used with an object"; + RecyclingPool::RawHandle handle(ptr); + if (ZSTD_sizeof_DCtx(ptr) <= kMaxRecycledDCtxBytes) { + pool_->RawPut(std::move(handle)); + } +} + void ZstdReaderBase::Initialize(Reader* src) { RIEGELI_ASSERT_NE(src, nullptr) << "Failed precondition of ZstdReader: null Reader pointer"; @@ -58,10 +77,11 @@ void ZstdReaderBase::Initialize(Reader* src) { } inline void ZstdReaderBase::InitializeDecompressor(Reader& src) { - decompressor_ = + RecyclingPool& pool = RecyclingPool::global( - recycling_pool_options_) - .Get( + recycling_pool_options_); + decompressor_ = std::unique_ptr( + pool.RawGet( [] { return std::unique_ptr( ZSTD_createDCtx()); @@ -85,7 +105,9 @@ inline void ZstdReaderBase::InitializeDecompressor(Reader& src) { << ZSTD_getErrorName(result); } #endif - }); + }) + .release(), + ZSTD_DCtxRecycler(&pool)); if (ABSL_PREDICT_FALSE(decompressor_ == nullptr)) { Fail(absl::InternalError("ZSTD_createDCtx() failed")); return; diff --git a/riegeli/zstd/zstd_reader.h b/riegeli/zstd/zstd_reader.h index ac099964..38e55646 100644 --- a/riegeli/zstd/zstd_reader.h +++ b/riegeli/zstd/zstd_reader.h @@ -185,6 +185,17 @@ class ZstdReaderBase : public BufferedReader { void operator()(ZSTD_DCtx* ptr) const { ZSTD_freeDCtx(ptr); } }; + class ZSTD_DCtxRecycler { + public: + ZSTD_DCtxRecycler() noexcept : pool_(nullptr) {} + explicit ZSTD_DCtxRecycler(RecyclingPool* pool) + : pool_(pool) {} + void operator()(ZSTD_DCtx* ptr) const; + + private: + RecyclingPool* pool_; + }; + static constexpr Position kMaxPosition = std::numeric_limits::max(); void InitializeDecompressor(Reader& src); @@ -205,7 +216,7 @@ class ZstdReaderBase : public BufferedReader { // If `ok()` but `decompressor_ == nullptr` then all data have been // decompressed, `exact_size() == limit_pos()`, and `ReadInternal()` must not // be called again. - RecyclingPool::Handle decompressor_; + std::unique_ptr decompressor_; }; // A `Reader` which decompresses data with Zstd after getting it from another diff --git a/riegeli/zstd/zstd_writer.cc b/riegeli/zstd/zstd_writer.cc index 6466116b..6c743bdc 100644 --- a/riegeli/zstd/zstd_writer.cc +++ b/riegeli/zstd/zstd_writer.cc @@ -53,6 +53,26 @@ static_assert(ZstdWriterBase::Options::kMinTargetCBlockSize >= static_assert(ZstdWriterBase::Options::kMaxTargetCBlockSize == ZSTD_TARGETCBLOCKSIZE_MAX); +namespace { + +// Maximum `ZSTD_CCtx` size to return to `RecyclingPool` (8 MiB). Covers +// unpledged compression levels up to 8 (`windowLog <= 21`, ~7.7 MiB) and +// pledged small-block streams, while evicting larger `> 8 MiB` workspaces +// instead of retaining them in the pool. +constexpr size_t kMaxRecycledCCtxBytes = size_t{8} << 20; + +} // namespace + +void ZstdWriterBase::ZSTD_CCtxRecycler::operator()(ZSTD_CCtx* ptr) const { + RIEGELI_ASSERT_NE(pool_, nullptr) + << "Failed precondition of ZSTD_CCtxRecycler: " + "default-constructed recycler used with an object"; + RecyclingPool::RawHandle handle(ptr); + if (ZSTD_sizeof_CCtx(ptr) <= kMaxRecycledCCtxBytes) { + pool_->RawPut(std::move(handle)); + } +} + void ZstdWriterBase::Initialize(Writer* dest, int compression_level, int window_log_or_0, int target_cblock_size_or_0, @@ -64,20 +84,24 @@ void ZstdWriterBase::Initialize(Writer* dest, int compression_level, return; } initial_compressed_pos_ = dest->pos(); - compressor_ = RecyclingPool::global( - recycling_pool_options_) - .Get( - [] { - return std::unique_ptr( - ZSTD_createCCtx()); - }, - [](ZSTD_CCtx* compressor) { - const size_t result = ZSTD_CCtx_reset( - compressor, ZSTD_reset_session_and_parameters); - RIEGELI_ASSERT(!ZSTD_isError(result)) - << "ZSTD_CCtx_reset() failed: " - << ZSTD_getErrorName(result); - }); + RecyclingPool& pool = + RecyclingPool::global( + recycling_pool_options_); + compressor_ = ZSTD_CCtxHandle( + pool.RawGet( + [] { + return std::unique_ptr( + ZSTD_createCCtx()); + }, + [](ZSTD_CCtx* compressor) { + const size_t result = ZSTD_CCtx_reset( + compressor, ZSTD_reset_session_and_parameters); + RIEGELI_ASSERT(!ZSTD_isError(result)) + << "ZSTD_CCtx_reset() failed: " + << ZSTD_getErrorName(result); + }) + .release(), + ZSTD_CCtxRecycler(&pool)); if (ABSL_PREDICT_FALSE(compressor_ == nullptr)) { Fail(absl::InternalError("ZSTD_createCCtx() failed")); return; diff --git a/riegeli/zstd/zstd_writer.h b/riegeli/zstd/zstd_writer.h index 82edd3a6..7535d7be 100644 --- a/riegeli/zstd/zstd_writer.h +++ b/riegeli/zstd/zstd_writer.h @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -322,6 +323,19 @@ class ZstdWriterBase : public BufferedWriter { void operator()(ZSTD_CCtx* ptr) const { ZSTD_freeCCtx(ptr); } }; + class ZSTD_CCtxRecycler { + public: + ZSTD_CCtxRecycler() noexcept : pool_(nullptr) {} + explicit ZSTD_CCtxRecycler(RecyclingPool* pool) + : pool_(pool) {} + void operator()(ZSTD_CCtx* ptr) const; + + private: + RecyclingPool* pool_; + }; + + using ZSTD_CCtxHandle = std::unique_ptr; + static constexpr Position kMaxPosition = std::numeric_limits::max(); bool WriteInternal(absl::string_view src, Writer& dest, @@ -335,7 +349,7 @@ class ZstdWriterBase : public BufferedWriter { RecyclingPoolOptions recycling_pool_options_; // If `ok()` but `compressor_ == nullptr` then `*pledged_size_` has been // reached. In this case `ZSTD_compressStream()` must not be called again. - RecyclingPool::Handle compressor_; + ZSTD_CCtxHandle compressor_; AssociatedReader> associated_reader_; };