From 2a384dd69942e36f491ada0add6e2c5225b14a32 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 28 Sep 2020 09:54:49 +0200 Subject: [PATCH] Hold compressed filenames more efficiently in memory. std::string::resize() will rarely give memory back, so allocate the string with the right size to begin with. This means we don't carry around a lot of slop, saving ~15% RAM during build. --- plocate-build.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/plocate-build.cpp b/plocate-build.cpp index 42ff96d..0580e4b 100644 --- a/plocate-build.cpp +++ b/plocate-build.cpp @@ -123,14 +123,14 @@ void read_mlocate(const char *filename, vector *files) close(fd); } -string zstd_compress(const string &src) +string zstd_compress(const string &src, string *tempbuf) { size_t max_size = ZSTD_compressBound(src.size()); - string dst; - dst.resize(max_size); - size_t size = ZSTD_compress(&dst[0], max_size, src.data(), src.size(), /*level=*/6); - dst.resize(size); - return dst; + if (tempbuf->size() < max_size) { + tempbuf->resize(max_size); + } + size_t size = ZSTD_compress(&(*tempbuf)[0], max_size, src.data(), src.size(), /*level=*/6); + return string(tempbuf->data(), size); } void do_build(const char *infile, const char *outfile, int block_size) @@ -233,13 +233,12 @@ void do_build(const char *infile, const char *outfile, int block_size) string uncompressed_filenames; int num_files_this_block = 0; - string dst; - + string dst, tempbuf; for (string &filename : files) { uncompressed_filenames += filename; filename.clear(); if (++num_files_this_block == block_size) { - filename_blocks.push_back(zstd_compress(uncompressed_filenames)); + filename_blocks.push_back(zstd_compress(uncompressed_filenames, &tempbuf)); uncompressed_filenames.clear(); num_files_this_block = 0; } else { @@ -247,7 +246,7 @@ void do_build(const char *infile, const char *outfile, int block_size) } } if (num_files_this_block > 0) { - filename_blocks.push_back(zstd_compress(uncompressed_filenames)); + filename_blocks.push_back(zstd_compress(uncompressed_filenames, &tempbuf)); } files.clear(); -- 2.39.5