Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions riegeli/zstd/zstd_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZSTD_DCtx, ZSTD_DCtxDeleter>::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";
Expand All @@ -58,10 +77,11 @@ void ZstdReaderBase::Initialize(Reader* src) {
}

inline void ZstdReaderBase::InitializeDecompressor(Reader& src) {
decompressor_ =
RecyclingPool<ZSTD_DCtx, ZSTD_DCtxDeleter>& pool =
RecyclingPool<ZSTD_DCtx, ZSTD_DCtxDeleter>::global(
recycling_pool_options_)
.Get(
recycling_pool_options_);
decompressor_ = std::unique_ptr<ZSTD_DCtx, ZSTD_DCtxRecycler>(
pool.RawGet(
[] {
return std::unique_ptr<ZSTD_DCtx, ZSTD_DCtxDeleter>(
ZSTD_createDCtx());
Expand All @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion riegeli/zstd/zstd_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZSTD_DCtx, ZSTD_DCtxDeleter>* pool)
: pool_(pool) {}
void operator()(ZSTD_DCtx* ptr) const;

private:
RecyclingPool<ZSTD_DCtx, ZSTD_DCtxDeleter>* pool_;
};

static constexpr Position kMaxPosition = std::numeric_limits<Position>::max();

void InitializeDecompressor(Reader& src);
Expand All @@ -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<ZSTD_DCtx, ZSTD_DCtxDeleter>::Handle decompressor_;
std::unique_ptr<ZSTD_DCtx, ZSTD_DCtxRecycler> decompressor_;
};

// A `Reader` which decompresses data with Zstd after getting it from another
Expand Down
52 changes: 38 additions & 14 deletions riegeli/zstd/zstd_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZSTD_CCtx, ZSTD_CCtxDeleter>::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,
Expand All @@ -64,20 +84,24 @@ void ZstdWriterBase::Initialize(Writer* dest, int compression_level,
return;
}
initial_compressed_pos_ = dest->pos();
compressor_ = RecyclingPool<ZSTD_CCtx, ZSTD_CCtxDeleter>::global(
recycling_pool_options_)
.Get(
[] {
return std::unique_ptr<ZSTD_CCtx, ZSTD_CCtxDeleter>(
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<ZSTD_CCtx, ZSTD_CCtxDeleter>& pool =
RecyclingPool<ZSTD_CCtx, ZSTD_CCtxDeleter>::global(
recycling_pool_options_);
compressor_ = ZSTD_CCtxHandle(
pool.RawGet(
[] {
return std::unique_ptr<ZSTD_CCtx, ZSTD_CCtxDeleter>(
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;
Expand Down
16 changes: 15 additions & 1 deletion riegeli/zstd/zstd_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <stddef.h>

#include <limits>
#include <memory>
#include <optional>
#include <utility>

Expand Down Expand Up @@ -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<ZSTD_CCtx, ZSTD_CCtxDeleter>* pool)
: pool_(pool) {}
void operator()(ZSTD_CCtx* ptr) const;

private:
RecyclingPool<ZSTD_CCtx, ZSTD_CCtxDeleter>* pool_;
};

using ZSTD_CCtxHandle = std::unique_ptr<ZSTD_CCtx, ZSTD_CCtxRecycler>;

static constexpr Position kMaxPosition = std::numeric_limits<Position>::max();

bool WriteInternal(absl::string_view src, Writer& dest,
Expand All @@ -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<ZSTD_CCtx, ZSTD_CCtxDeleter>::Handle compressor_;
ZSTD_CCtxHandle compressor_;

AssociatedReader<ZstdReader<Reader*>> associated_reader_;
};
Expand Down
Loading