From db96666312961f730f7ea9e4a966fc43d73dbc1c Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 12 Oct 2020 20:08:58 +0200 Subject: [PATCH] Reuse zstd compression contexts, for a tiny speed boost. --- plocate-build.cpp | 3 ++- plocate.cpp | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/plocate-build.cpp b/plocate-build.cpp index e1e2f64..ed252fc 100644 --- a/plocate-build.cpp +++ b/plocate-build.cpp @@ -300,7 +300,8 @@ string zstd_compress(const string &src, string *tempbuf) if (tempbuf->size() < max_size) { tempbuf->resize(max_size); } - size_t size = ZSTD_compress(&(*tempbuf)[0], max_size, src.data(), src.size(), /*level=*/6); + static ZSTD_CCtx *ctx = ZSTD_createCCtx(); // Reused across calls. + size_t size = ZSTD_compressCCtx(ctx, &(*tempbuf)[0], max_size, src.data(), src.size(), /*level=*/6); return string(tempbuf->data(), size); } diff --git a/plocate.cpp b/plocate.cpp index eb00ada..62bc6e3 100644 --- a/plocate.cpp +++ b/plocate.cpp @@ -316,8 +316,9 @@ void scan_file_block(const vector &needles, string_view compressed, string block; block.resize(uncompressed_len + 1); - size_t err = ZSTD_decompress(&block[0], block.size(), compressed.data(), - compressed.size()); + static ZSTD_DCtx *ctx = ZSTD_createDCtx(); // Reused across calls. + size_t err = ZSTD_decompressDCtx(ctx, &block[0], block.size(), compressed.data(), + compressed.size()); if (ZSTD_isError(err)) { fprintf(stderr, "ZSTD_decompress(): %s\n", ZSTD_getErrorName(err)); exit(1); -- 2.39.2