aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/local-store.cc
diff options
context:
space:
mode:
authoreldritch horrors <pennae@lix.systems>2024-03-04 07:32:31 +0100
committereldritch horrors <pennae@lix.systems>2024-03-04 07:32:31 +0100
commitdd180911d8ecf737e6b2ceb89d6797965fcc3b78 (patch)
tree633078cc7b7f743421e04dc51ec8a4bbda76ada6 /src/libstore/local-store.cc
parent076844e3868f108ce0d1523e2db069f7e71c9990 (diff)
Merge pull request #9582 from pennae/misc-opts
a packet of small optimizations (cherry picked from commit ee439734e924eb337a869ff2e48aff8b989198bc) Change-Id: I125d870710750a32a0dece48f39a3e9132b0d023
Diffstat (limited to 'src/libstore/local-store.cc')
-rw-r--r--src/libstore/local-store.cc22
1 files changed, 17 insertions, 5 deletions
diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc
index 17b4ecc73..554e13e3d 100644
--- a/src/libstore/local-store.cc
+++ b/src/libstore/local-store.cc
@@ -15,6 +15,8 @@
#include <algorithm>
#include <cstring>
+#include <memory>
+#include <new>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
@@ -1286,7 +1288,11 @@ StorePath LocalStore::addToStoreFromDump(Source & source0, std::string_view name
path. */
bool inMemory = false;
- std::string dump;
+ struct Free {
+ void operator()(void* v) { free(v); }
+ };
+ std::unique_ptr<char, Free> dumpBuffer(nullptr);
+ std::string_view dump;
/* Fill out buffer, and decide whether we are working strictly in
memory based on whether we break out because the buffer is full
@@ -1295,13 +1301,18 @@ StorePath LocalStore::addToStoreFromDump(Source & source0, std::string_view name
auto oldSize = dump.size();
constexpr size_t chunkSize = 65536;
auto want = std::min(chunkSize, settings.narBufferSize - oldSize);
- dump.resize(oldSize + want);
+ if (auto tmp = realloc(dumpBuffer.get(), oldSize + want)) {
+ dumpBuffer.release();
+ dumpBuffer.reset((char*) tmp);
+ } else {
+ throw std::bad_alloc();
+ }
auto got = 0;
Finally cleanup([&]() {
- dump.resize(oldSize + got);
+ dump = {dumpBuffer.get(), dump.size() + got};
});
try {
- got = source.read(dump.data() + oldSize, want);
+ got = source.read(dumpBuffer.get() + oldSize, want);
} catch (EndOfFile &) {
inMemory = true;
break;
@@ -1327,7 +1338,8 @@ StorePath LocalStore::addToStoreFromDump(Source & source0, std::string_view name
else
writeFile(tempPath, bothSource);
- dump.clear();
+ dumpBuffer.reset();
+ dump = {};
}
auto [hash, size] = hashSink->finish();