diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp index d023f19e3..466b93c25 100644 --- a/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp +++ b/fs_mgr/libsnapshot/libsnapshot_cow/cow_compress.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -174,12 +175,18 @@ class BrotliCompressor final : public ICompressor { class ZstdCompressor final : public ICompressor { public: - ZstdCompressor(uint32_t compression_level) : ICompressor(compression_level){}; + ZstdCompressor(uint32_t compression_level) + : ICompressor(compression_level), zstd_context_(ZSTD_createCCtx(), ZSTD_freeCCtx) { + ZSTD_CCtx_setParameter(zstd_context_.get(), ZSTD_c_compressionLevel, compression_level); + // FIXME: hardcoding a value of 12 here for 4k blocks, should change to be either set by + // user, or optimized depending on block size + ZSTD_CCtx_setParameter(zstd_context_.get(), ZSTD_c_windowLog, 12); + }; std::basic_string Compress(const void* data, size_t length) const override { std::basic_string buffer(ZSTD_compressBound(length), '\0'); const auto compressed_size = - ZSTD_compress(buffer.data(), buffer.size(), data, length, GetCompressionLevel()); + ZSTD_compress2(zstd_context_.get(), buffer.data(), buffer.size(), data, length); if (compressed_size <= 0) { LOG(ERROR) << "ZSTD compression failed " << compressed_size; return {}; @@ -193,6 +200,9 @@ class ZstdCompressor final : public ICompressor { } return buffer; }; + + private: + std::unique_ptr zstd_context_; }; bool CompressWorker::CompressBlocks(const void* buffer, size_t num_blocks,