chore: get rid of possible recursion when unwinding structured reply (#5012)

This commit is contained in:
Roman Gershman 2025-04-30 18:17:38 +03:00 committed by GitHub
parent dced0371d3
commit befb36d477
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 8 deletions

View file

@ -81,24 +81,24 @@ void CapturingReplyBuilder::SendDirect(Payload&& val) {
}
}
void CapturingReplyBuilder::Capture(Payload val) {
void CapturingReplyBuilder::Capture(Payload val, bool collapse_if_needed) {
if (!stack_.empty()) {
stack_.top().first->arr.push_back(std::move(val));
stack_.top().second--;
auto& last = stack_.top();
last.first->arr.push_back(std::move(val));
if (collapse_if_needed && last.second-- == 1) {
CollapseFilledCollections();
}
} else {
DCHECK_EQ(current_.index(), 0u);
current_ = std::move(val);
}
// Check if we filled up a collection.
CollapseFilledCollections();
}
void CapturingReplyBuilder::CollapseFilledCollections() {
while (!stack_.empty() && stack_.top().second == 0) {
auto pl = std::move(stack_.top());
stack_.pop();
Capture(std::move(pl.first));
Capture(std::move(pl.first), false);
}
}

View file

@ -79,7 +79,7 @@ class CapturingReplyBuilder : public RedisReplyBuilder {
void SendDirect(Payload&& val);
// Capture value and store eiter in current topmost collection or as a standalone value.
void Capture(Payload val);
void Capture(Payload val, bool collapse_if_needed = true);
// While topmost collection in stack is full, finalize it and add it as a regular value.
void CollapseFilledCollections();