diff options
author | aneporada <aneporada@yandex-team.com> | 2024-11-08 22:23:41 +0300 |
---|---|---|
committer | Vitaly Stoyan <vvvv@ydb.tech> | 2024-11-09 12:01:53 +0300 |
commit | 2d0e7498c5e5f795c1a040623052b112691fac7e (patch) | |
tree | 5980d9fab8f80b3cad8903248d01397f79763634 | |
parent | 3d5bcb1e40a790292e69c6d3ba03c530f9564454 (diff) | |
download | ydb-2d0e7498c5e5f795c1a040623052b112691fac7e.tar.gz |
Fast TChunkedBuffer::Size()
commit_hash:6dd56a3c0d552a30b3e30a2b133d638716e1893d
-rw-r--r-- | yql/essentials/utils/chunked_buffer.cpp | 26 | ||||
-rw-r--r-- | yql/essentials/utils/chunked_buffer.h | 15 |
2 files changed, 22 insertions, 19 deletions
diff --git a/yql/essentials/utils/chunked_buffer.cpp b/yql/essentials/utils/chunked_buffer.cpp index 906da8dcae..7ae66d04ef 100644 --- a/yql/essentials/utils/chunked_buffer.cpp +++ b/yql/essentials/utils/chunked_buffer.cpp @@ -6,10 +6,14 @@ namespace NYql { TChunkedBuffer::TChunkedBuffer(TChunkedBuffer&& other) { Items_ = std::move(other.Items_); + Size_ = other.Size_; + other.Size_ = 0; } TChunkedBuffer& TChunkedBuffer::operator=(TChunkedBuffer&& other) { Items_ = std::move(other.Items_); + Size_ = other.Size_; + other.Size_ = 0; return *this; } @@ -40,25 +44,10 @@ size_t TChunkedBuffer::CopyTo(IOutputStream& dst, size_t toCopy) const { return copied; } -size_t TChunkedBuffer::ContigousSize() const { - return Items_.empty() ? 0 : Front().Buf.size(); -} - -size_t TChunkedBuffer::Size() const { - size_t result = 0; - for (auto& item : Items_) { - result += item.Buf.size(); - } - return result; -} - -bool TChunkedBuffer::Empty() const { - return Items_.empty(); -} - TChunkedBuffer& TChunkedBuffer::Append(TStringBuf buf, const std::shared_ptr<const void>& owner) { if (!buf.empty()) { Items_.emplace_back(TChunk{buf, owner}); + Size_ += buf.size(); } return *this; } @@ -67,6 +56,7 @@ TChunkedBuffer& TChunkedBuffer::Append(TString&& str) { if (!str.empty()) { auto owner = std::make_shared<TString>(std::move(str)); Items_.emplace_back(TChunk{*owner, owner}); + Size_ += owner->size(); } return *this; } @@ -74,13 +64,16 @@ TChunkedBuffer& TChunkedBuffer::Append(TString&& str) { TChunkedBuffer& TChunkedBuffer::Append(TChunkedBuffer&& other) { while (!other.Items_.empty()) { Items_.emplace_back(std::move(other.Items_.front())); + Size_ += Items_.back().Buf.size(); other.Items_.pop_front(); } + other.Size_ = 0; return *this; } TChunkedBuffer& TChunkedBuffer::Clear() { Items_.clear(); + Size_ = 0; return *this; } @@ -90,6 +83,7 @@ TChunkedBuffer& TChunkedBuffer::Erase(size_t size) { size_t toErase = std::min(buf.size(), size); buf.Skip(toErase); size -= toErase; + Size_ -= toErase; if (buf.empty()) { Items_.pop_front(); } diff --git a/yql/essentials/utils/chunked_buffer.h b/yql/essentials/utils/chunked_buffer.h index 6f5b2a8ea7..684e185fbe 100644 --- a/yql/essentials/utils/chunked_buffer.h +++ b/yql/essentials/utils/chunked_buffer.h @@ -18,9 +18,17 @@ public: explicit TChunkedBuffer(TStringBuf buf, const std::shared_ptr<const void>& owner); explicit TChunkedBuffer(TString&& str); - size_t ContigousSize() const; - size_t Size() const; - bool Empty() const; + inline size_t ContigousSize() const { + return Items_.empty() ? 0 : Front().Buf.size(); + } + + inline size_t Size() const { + return Size_; + } + + inline bool Empty() const { + return Size_ == 0; + } struct TChunk { TStringBuf Buf; @@ -39,6 +47,7 @@ public: private: std::deque<TChunk> Items_; + size_t Size_ = 0; }; class TChunkedBufferOutput : public IOutputStream { |