Skip to content

Commit

Permalink
refactor: slot_set don't use stack memory anymore (#4386)
Browse files Browse the repository at this point in the history
  • Loading branch information
BorysTheDev authored Jan 1, 2025
1 parent 810af83 commit c5b3584
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/server/cluster/slot_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ class SlotSet {
using TBitSet = std::bitset<kSlotsNumber>;

SlotSet(bool full_house = false) {
slots_ = std::make_unique<TBitSet>();
if (full_house)
slots_->flip();
}

SlotSet(const SlotRanges& slot_ranges) {
slots_ = std::make_unique<TBitSet>();
Set(slot_ranges, true);
}

SlotSet(const TBitSet& s) {
*slots_ = s;
}

SlotSet(const SlotSet& s) {
*slots_ = *s.slots_;
slots_ = std::make_unique<TBitSet>(*s.slots_);
}

SlotSet(SlotSet&& s) = default;

bool Contains(SlotId slot) const {
return slots_->test(slot);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -85,7 +89,12 @@ class SlotSet {
}

private:
std::unique_ptr<TBitSet> slots_{std::make_unique<TBitSet>()};
SlotSet(std::unique_ptr<TBitSet> s) {
slots_ = std::move(s);
}

private:
std::unique_ptr<TBitSet> slots_;
};

} // namespace dfly::cluster

0 comments on commit c5b3584

Please sign in to comment.