]> git.sesse.net Git - plocate/commitdiff
Reuse zstd compression contexts, for a tiny speed boost.
authorSteinar H. Gunderson <steinar+git@gunderson.no>
Mon, 12 Oct 2020 18:08:58 +0000 (20:08 +0200)
committerSteinar H. Gunderson <steinar+git@gunderson.no>
Mon, 12 Oct 2020 18:08:58 +0000 (20:08 +0200)
plocate-build.cpp
plocate.cpp

index e1e2f644ef2ca891f93d48fdaab15e4f681bf418..ed252fc5a5ac755efb23b6d1ea621d3d28e3cd37 100644 (file)
@@ -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);
 }
 
index eb00adac8cced544b8ef0db9524f85cfcd146722..62bc6e331d04a9e61bf76b479258109e50bda100 100644 (file)
@@ -316,8 +316,9 @@ void scan_file_block(const vector<Needle> &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);