about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2017-03-01T13·54+0100
committerEelco Dolstra <edolstra@gmail.com>2017-03-01T13·54+0100
commit374908726b87f6cd137ea7d097fdcda57003594e (patch)
treeced2c4fac0c4166d986c7fbc977b7798371095e8
parent07a0b8ca671669794f51f9d886397bf038a8881e (diff)
readString(): Read directly into std::string
When reading a huge string, this halves memory consumption.

(Strictly speaking, this appears only valid in C++17, but who cares...)
-rw-r--r--src/libutil/serialise.cc6
1 files changed, 3 insertions, 3 deletions
diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc
index 6064e15f5e67..950e6362a245 100644
--- a/src/libutil/serialise.cc
+++ b/src/libutil/serialise.cc
@@ -207,10 +207,10 @@ size_t readString(unsigned char * buf, size_t max, Source & source)
 string readString(Source & source)
 {
     auto len = readNum<size_t>(source);
-    auto buf = std::make_unique<unsigned char[]>(len);
-    source(buf.get(), len);
+    std::string res(len, 0);
+    source((unsigned char*) res.data(), len);
     readPadding(len, source);
-    return string((char *) buf.get(), len);
+    return res;
 }
 
 Source & operator >> (Source & in, string & s)