refactor(zstd): drop zstd_context_manager from zstd compressor

This commit is contained in:
Marcus Holland-Moritz 2025-05-20 18:54:55 +02:00
parent a93776c0af
commit 121422f35c

View File

@ -26,8 +26,6 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#include <mutex>
#include <zstd.h> #include <zstd.h>
#include <fmt/format.h> #include <fmt/format.h>
@ -38,7 +36,6 @@
#include <dwarfs/fstypes.h> #include <dwarfs/fstypes.h>
#include <dwarfs/malloc_byte_buffer.h> #include <dwarfs/malloc_byte_buffer.h>
#include <dwarfs/option_map.h> #include <dwarfs/option_map.h>
#include <dwarfs/zstd_context_manager.h>
#include "base.h" #include "base.h"
@ -56,8 +53,7 @@ namespace {
class zstd_block_compressor final : public block_compressor::impl { class zstd_block_compressor final : public block_compressor::impl {
public: public:
explicit zstd_block_compressor(int level) explicit zstd_block_compressor(int level)
: ctxmgr_{get_context_manager()} : level_{level} {}
, level_{level} {}
zstd_block_compressor(zstd_block_compressor const& rhs) = default; zstd_block_compressor(zstd_block_compressor const& rhs) = default;
@ -82,19 +78,6 @@ class zstd_block_compressor final : public block_compressor::impl {
} }
private: private:
static std::shared_ptr<zstd_context_manager> get_context_manager() {
std::lock_guard lock(s_mx);
auto mgr = s_ctxmgr.lock();
if (!mgr) {
s_ctxmgr = mgr = std::make_shared<zstd_context_manager>();
}
return mgr;
}
static inline std::mutex s_mx;
static inline std::weak_ptr<zstd_context_manager> s_ctxmgr;
std::shared_ptr<zstd_context_manager> ctxmgr_;
int const level_; int const level_;
}; };
@ -103,9 +86,8 @@ zstd_block_compressor::compress(shared_byte_buffer const& data,
std::string const* /*metadata*/) const { std::string const* /*metadata*/) const {
auto compressed = malloc_byte_buffer::create(); // TODO: make configurable auto compressed = malloc_byte_buffer::create(); // TODO: make configurable
compressed.resize(ZSTD_compressBound(data.size())); compressed.resize(ZSTD_compressBound(data.size()));
auto ctx = ctxmgr_->make_context(); auto size = ZSTD_compress(compressed.data(), compressed.size(), data.data(),
auto size = ZSTD_compressCCtx(ctx.get(), compressed.data(), compressed.size(), data.size(), level_);
data.data(), data.size(), level_);
if (ZSTD_isError(size)) { if (ZSTD_isError(size)) {
DWARFS_THROW(runtime_error, DWARFS_THROW(runtime_error,
fmt::format("ZSTD: {}", ZSTD_getErrorName(size))); fmt::format("ZSTD: {}", ZSTD_getErrorName(size)));