From ebc97b8db5edf7a8e3af1f79c2dd9be8928c1ad5 Mon Sep 17 00:00:00 2001 From: Borys Date: Mon, 30 Dec 2024 16:08:37 +0200 Subject: [PATCH] refactor: slot_set don't use stack memory anymore --- src/server/cluster/slot_set.h | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/server/cluster/slot_set.h b/src/server/cluster/slot_set.h index de54631fdcc6..c1eb23a7faf2 100644 --- a/src/server/cluster/slot_set.h +++ b/src/server/cluster/slot_set.h @@ -18,22 +18,22 @@ class SlotSet { using TBitSet = std::bitset; SlotSet(bool full_house = false) { + slots_ = std::make_unique(); if (full_house) slots_->flip(); } SlotSet(const SlotRanges& slot_ranges) { + slots_ = std::make_unique(); Set(slot_ranges, true); } - SlotSet(const TBitSet& s) { - *slots_ = s; - } - SlotSet(const SlotSet& s) { - *slots_ = *s.slots_; + slots_ = std::make_unique(*s.slots_); } + SlotSet(SlotSet&& s) = default; + bool Contains(SlotId slot) const { return slots_->test(slot); } @@ -64,7 +64,11 @@ class SlotSet { // Get SlotSet that are absent in the slots SlotSet GetRemovedSlots(const SlotSet& slots) const { - return *slots_ & ~*slots.slots_; + // we need to avoid stack usage to prevent stack overflow + SlotSet res(slots); + res.slots_->flip(); + *res.slots_ &= *slots_; + return res; } SlotRanges ToSlotRanges() const { @@ -85,7 +89,12 @@ class SlotSet { } private: - std::unique_ptr slots_{std::make_unique()}; + SlotSet(std::unique_ptr s) { + slots_ = std::move(s); + } + + private: + std::unique_ptr slots_; }; } // namespace dfly::cluster