From 7deeb69934ecddba55bd7af9d7b5b87355dd6427 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 5 Jan 2022 15:45:34 +0000 Subject: If TLS buffer has more than 8x overhead, shrink it to fit prior to posting task for compression. Intention to reduce amount of allocated memory in flight in the case there are cpu starvation, or something else causing hickups. --- searchlib/src/tests/transactionlog/chunks_test.cpp | 21 +++++++++++++++++++++ .../src/vespa/searchlib/transactionlog/common.cpp | 13 +++++++++++++ .../src/vespa/searchlib/transactionlog/common.h | 2 ++ .../src/vespa/searchlib/transactionlog/domain.cpp | 1 + 4 files changed, 37 insertions(+) diff --git a/searchlib/src/tests/transactionlog/chunks_test.cpp b/searchlib/src/tests/transactionlog/chunks_test.cpp index 8d6620c9f8b..ca03a47634d 100644 --- a/searchlib/src/tests/transactionlog/chunks_test.cpp +++ b/searchlib/src/tests/transactionlog/chunks_test.cpp @@ -115,4 +115,25 @@ TEST("test multi element commitchunk") { } EXPECT_EQUAL(0u, counter); } + +TEST("shrinkToFit if difference is larger than 8x") { + Packet p(16000); + p.add(Packet::Entry(1, 3, ConstBufferRef(TEXT, strlen(TEXT)))); + EXPECT_EQUAL(150u, p.sizeBytes()); + EXPECT_EQUAL(16384u, p.getHandle().capacity()); + p.shrinkToFit(); + EXPECT_EQUAL(150u, p.sizeBytes()); + EXPECT_EQUAL(150u, p.getHandle().capacity()); +} + +TEST("not shrinkToFit if difference is less than 8x") { + Packet p(1000); + p.add(Packet::Entry(1, 3, ConstBufferRef(TEXT, strlen(TEXT)))); + EXPECT_EQUAL(150u, p.sizeBytes()); + EXPECT_EQUAL(1024u, p.getHandle().capacity()); + p.shrinkToFit(); + EXPECT_EQUAL(150u, p.sizeBytes()); + EXPECT_EQUAL(1024u, p.getHandle().capacity()); +} + TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/searchlib/src/vespa/searchlib/transactionlog/common.cpp b/searchlib/src/vespa/searchlib/transactionlog/common.cpp index 967f8748174..4130ad0bc06 100644 --- a/searchlib/src/vespa/searchlib/transactionlog/common.cpp +++ b/searchlib/src/vespa/searchlib/transactionlog/common.cpp @@ -116,6 +116,14 @@ Packet::add(const Packet::Entry & e) _range.to(e.serial()); } +void +Packet::shrinkToFit() { + if (_buf.size() * 8 < _buf.capacity()) { + nbostream::Buffer shrunkToFit(_buf.data(), _buf.data() + _buf.size()); + _buf.swap(shrunkToFit); + } +} + Writer::CommitResult::CommitResult() : _callBacks() {} @@ -151,4 +159,9 @@ CommitChunk::createCommitResult() const { return Writer::CommitResult(_callBacks); } +void +CommitChunk::shrinkPayloadToFit() { + _data.shrinkToFit(); +} + } diff --git a/searchlib/src/vespa/searchlib/transactionlog/common.h b/searchlib/src/vespa/searchlib/transactionlog/common.h index 135681037ef..87150f2cfa9 100644 --- a/searchlib/src/vespa/searchlib/transactionlog/common.h +++ b/searchlib/src/vespa/searchlib/transactionlog/common.h @@ -83,6 +83,7 @@ public: bool empty() const { return _count == 0; } size_t sizeBytes() const { return _buf.size(); } void merge(const Packet & packet); + void shrinkToFit(); private: size_t _count; SerialNumRange _range; @@ -143,6 +144,7 @@ public: Writer::CommitResult createCommitResult() const; void setCommitDoneCallback(Writer::DoneCallback onDone) { _onCommitDone = std::move(onDone); } Writer::CommitPayload stealCallbacks() { return std::move(_callBacks); } + void shrinkPayloadToFit(); private: Packet _data; Writer::CommitPayload _callBacks; diff --git a/searchlib/src/vespa/searchlib/transactionlog/domain.cpp b/searchlib/src/vespa/searchlib/transactionlog/domain.cpp index bbde39a42f6..ff35847aa77 100644 --- a/searchlib/src/vespa/searchlib/transactionlog/domain.cpp +++ b/searchlib/src/vespa/searchlib/transactionlog/domain.cpp @@ -396,6 +396,7 @@ void Domain::commitChunk(std::unique_ptr chunk, const UniqueLock & chunkOrderGuard) { assert(chunkOrderGuard.mutex() == &_currentChunkMonitor && chunkOrderGuard.owns_lock()); if (chunk->getPacket().empty()) return; + chunk->shrinkPayloadToFit(); std::promise promise; std::future future = promise.get_future(); _executor.execute(makeLambdaTask([promise=std::move(promise), chunk = std::move(chunk), -- cgit v1.2.3