Skip to content

Commit 2d0e749

Browse files
nepalvitstn
authored andcommitted
Fast TChunkedBuffer::Size()
commit_hash:6dd56a3c0d552a30b3e30a2b133d638716e1893d
1 parent 3d5bcb1 commit 2d0e749

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

yql/essentials/utils/chunked_buffer.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ namespace NYql {
66

77
TChunkedBuffer::TChunkedBuffer(TChunkedBuffer&& other) {
88
Items_ = std::move(other.Items_);
9+
Size_ = other.Size_;
10+
other.Size_ = 0;
911
}
1012

1113
TChunkedBuffer& TChunkedBuffer::operator=(TChunkedBuffer&& other) {
1214
Items_ = std::move(other.Items_);
15+
Size_ = other.Size_;
16+
other.Size_ = 0;
1317
return *this;
1418
}
1519

@@ -40,25 +44,10 @@ size_t TChunkedBuffer::CopyTo(IOutputStream& dst, size_t toCopy) const {
4044
return copied;
4145
}
4246

43-
size_t TChunkedBuffer::ContigousSize() const {
44-
return Items_.empty() ? 0 : Front().Buf.size();
45-
}
46-
47-
size_t TChunkedBuffer::Size() const {
48-
size_t result = 0;
49-
for (auto& item : Items_) {
50-
result += item.Buf.size();
51-
}
52-
return result;
53-
}
54-
55-
bool TChunkedBuffer::Empty() const {
56-
return Items_.empty();
57-
}
58-
5947
TChunkedBuffer& TChunkedBuffer::Append(TStringBuf buf, const std::shared_ptr<const void>& owner) {
6048
if (!buf.empty()) {
6149
Items_.emplace_back(TChunk{buf, owner});
50+
Size_ += buf.size();
6251
}
6352
return *this;
6453
}
@@ -67,20 +56,24 @@ TChunkedBuffer& TChunkedBuffer::Append(TString&& str) {
6756
if (!str.empty()) {
6857
auto owner = std::make_shared<TString>(std::move(str));
6958
Items_.emplace_back(TChunk{*owner, owner});
59+
Size_ += owner->size();
7060
}
7161
return *this;
7262
}
7363

7464
TChunkedBuffer& TChunkedBuffer::Append(TChunkedBuffer&& other) {
7565
while (!other.Items_.empty()) {
7666
Items_.emplace_back(std::move(other.Items_.front()));
67+
Size_ += Items_.back().Buf.size();
7768
other.Items_.pop_front();
7869
}
70+
other.Size_ = 0;
7971
return *this;
8072
}
8173

8274
TChunkedBuffer& TChunkedBuffer::Clear() {
8375
Items_.clear();
76+
Size_ = 0;
8477
return *this;
8578
}
8679

@@ -90,6 +83,7 @@ TChunkedBuffer& TChunkedBuffer::Erase(size_t size) {
9083
size_t toErase = std::min(buf.size(), size);
9184
buf.Skip(toErase);
9285
size -= toErase;
86+
Size_ -= toErase;
9387
if (buf.empty()) {
9488
Items_.pop_front();
9589
}

yql/essentials/utils/chunked_buffer.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ class TChunkedBuffer {
1818
explicit TChunkedBuffer(TStringBuf buf, const std::shared_ptr<const void>& owner);
1919
explicit TChunkedBuffer(TString&& str);
2020

21-
size_t ContigousSize() const;
22-
size_t Size() const;
23-
bool Empty() const;
21+
inline size_t ContigousSize() const {
22+
return Items_.empty() ? 0 : Front().Buf.size();
23+
}
24+
25+
inline size_t Size() const {
26+
return Size_;
27+
}
28+
29+
inline bool Empty() const {
30+
return Size_ == 0;
31+
}
2432

2533
struct TChunk {
2634
TStringBuf Buf;
@@ -39,6 +47,7 @@ class TChunkedBuffer {
3947

4048
private:
4149
std::deque<TChunk> Items_;
50+
size_t Size_ = 0;
4251
};
4352

4453
class TChunkedBufferOutput : public IOutputStream {

0 commit comments

Comments
 (0)