mirror of
https://github.com/esphome/esphome.git
synced 2026-09-30 16:30:22 +00:00
[scheduler] Eliminate heap allocation in full_cleanup_removed_items_ (#13837)
This commit is contained in:
+10
-11
@@ -390,20 +390,19 @@ void Scheduler::full_cleanup_removed_items_() {
|
|||||||
// 4. No operations inside can block or take other locks, so no deadlock risk
|
// 4. No operations inside can block or take other locks, so no deadlock risk
|
||||||
LockGuard guard{this->lock_};
|
LockGuard guard{this->lock_};
|
||||||
|
|
||||||
std::vector<std::unique_ptr<SchedulerItem>> valid_items;
|
// Compact in-place: move valid items forward, recycle removed ones
|
||||||
|
size_t write = 0;
|
||||||
// Move all non-removed items to valid_items, recycle removed ones
|
for (size_t read = 0; read < this->items_.size(); ++read) {
|
||||||
for (auto &item : this->items_) {
|
if (!is_item_removed_(this->items_[read].get())) {
|
||||||
if (!is_item_removed_(item.get())) {
|
if (write != read) {
|
||||||
valid_items.push_back(std::move(item));
|
this->items_[write] = std::move(this->items_[read]);
|
||||||
|
}
|
||||||
|
++write;
|
||||||
} else {
|
} else {
|
||||||
// Recycle removed items
|
this->recycle_item_main_loop_(std::move(this->items_[read]));
|
||||||
this->recycle_item_main_loop_(std::move(item));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this->items_.erase(this->items_.begin() + write, this->items_.end());
|
||||||
// Replace items_ with the filtered list
|
|
||||||
this->items_ = std::move(valid_items);
|
|
||||||
// Rebuild the heap structure since items are no longer in heap order
|
// Rebuild the heap structure since items are no longer in heap order
|
||||||
std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
|
std::make_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
|
||||||
this->to_remove_ = 0;
|
this->to_remove_ = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user