aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2020-01-11 12:01:08 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2020-01-11 12:01:08 +0000
commit83b9fd60639415f651315c533d15321078a0eede (patch)
tree2bdbfb8f00bda16f4837501137daaa93e41f92e8 /searchlib
parent6af036ff1be58aed8806610d5769952ac0192bdc (diff)
Shrink the buffer if it is too big.
Diffstat (limited to 'searchlib')
-rw-r--r--searchlib/src/vespa/searchlib/docstore/value.cpp27
1 files changed, 18 insertions, 9 deletions
diff --git a/searchlib/src/vespa/searchlib/docstore/value.cpp b/searchlib/src/vespa/searchlib/docstore/value.cpp
index ea29c894cba..6f665fdc009 100644
--- a/searchlib/src/vespa/searchlib/docstore/value.cpp
+++ b/searchlib/src/vespa/searchlib/docstore/value.cpp
@@ -42,6 +42,19 @@ Value::set(vespalib::DataBuffer &&buf, ssize_t len) {
set(std::move(buf), len, CompressionConfig());
}
+namespace {
+
+vespalib::alloc::Alloc
+compact(size_t sz, vespalib::alloc::Alloc buf) {
+ if (sz <= (buf.size() << 2)) {
+ vespalib::alloc::Alloc shrunk = buf.create(sz);
+ memcpy(shrunk.get(), buf.get(), sz);
+ return shrunk;
+ }
+ return buf;
+}
+
+}
void
Value::set(vespalib::DataBuffer &&buf, ssize_t len, const CompressionConfig &compression) {
assert(len < std::numeric_limits<uint32_t>::max());
@@ -50,20 +63,16 @@ Value::set(vespalib::DataBuffer &&buf, ssize_t len, const CompressionConfig &com
vespalib::ConstBufferRef input(buf.getData(), len);
CompressionConfig::Type type = compress(compression, input, compressed, true);
_compressedSize = compressed.getDataLen();
+ _compression = type;
+ _uncompressedSize = len;
+ _uncompressedCrc = XXH64(input.c_str(), input.size(), 0);
+ _buf = std::make_shared<Alloc>(compact(_compressedSize,
+ (buf.getData() == compressed.getData()) ? buf.stealBuffer() : compressed.stealBuffer()));
- if (buf.getData() == compressed.getData()) {
- // Uncompressed so we can just steal the underlying buffer.
- _buf = std::make_shared<Alloc>(buf.stealBuffer());
- } else {
- _buf = std::make_shared<Alloc>(compressed.stealBuffer());
- }
assert(((type == CompressionConfig::NONE) &&
(len == ssize_t(_compressedSize))) ||
((type != CompressionConfig::NONE) &&
(len > ssize_t(_compressedSize))));
- _compression = type;
- _uncompressedSize = len;
- _uncompressedCrc = XXH64(input.c_str(), input.size(), 0);
}
Value::Result