mirror of
https://github.com/esphome/esphome.git
synced 2026-09-15 17:18:40 +00:00
Change cleanup_() to return bool instead of size_t
cleanup_() was computing items_.size() on every call even on the fast path where nothing was removed. All callers only check if items remain (== 0), so return bool and use items_.empty() instead.
This commit is contained in:
@@ -396,7 +396,7 @@ optional<uint32_t> HOT Scheduler::next_schedule_in(uint32_t now) {
|
||||
// safe when called from the main thread. Other threads must not call this method.
|
||||
|
||||
// If no items, return empty optional
|
||||
if (this->cleanup_() == 0)
|
||||
if (!this->cleanup_())
|
||||
return {};
|
||||
|
||||
SchedulerItem *item = this->items_[0];
|
||||
@@ -633,12 +633,12 @@ void HOT Scheduler::process_to_add() {
|
||||
this->to_add_.clear();
|
||||
this->to_add_count_clear_();
|
||||
}
|
||||
size_t HOT Scheduler::cleanup_() {
|
||||
// Fast path: if nothing to remove, just return the current size.
|
||||
bool HOT Scheduler::cleanup_() {
|
||||
// Fast path: if nothing to remove, just check if items exist.
|
||||
// Uses atomic load on platforms with atomics, falls back to always taking the lock otherwise.
|
||||
// Worst case is a one-loop-iteration delay in cleanup.
|
||||
if (this->to_remove_empty_())
|
||||
return this->items_.size();
|
||||
return !this->items_.empty();
|
||||
|
||||
// We must hold the lock for the entire cleanup operation because:
|
||||
// 1. We're modifying items_ (via pop_raw_locked_) which requires exclusive access
|
||||
@@ -656,7 +656,7 @@ size_t HOT Scheduler::cleanup_() {
|
||||
this->to_remove_decrement_();
|
||||
this->recycle_item_main_loop_(this->pop_raw_locked_());
|
||||
}
|
||||
return this->items_.size();
|
||||
return !this->items_.empty();
|
||||
}
|
||||
Scheduler::SchedulerItem *HOT Scheduler::pop_raw_locked_() {
|
||||
std::pop_heap(this->items_.begin(), this->items_.end(), SchedulerItem::cmp);
|
||||
|
||||
@@ -284,9 +284,9 @@ class Scheduler {
|
||||
#endif
|
||||
}
|
||||
// Cleanup logically deleted items from the scheduler
|
||||
// Returns the number of items remaining after cleanup
|
||||
// Returns true if items remain after cleanup
|
||||
// IMPORTANT: This method should only be called from the main thread (loop task).
|
||||
size_t cleanup_();
|
||||
bool cleanup_();
|
||||
// Remove and return the front item from the heap as a raw pointer.
|
||||
// Caller takes ownership and must either recycle or delete the item.
|
||||
// IMPORTANT: Caller must hold the scheduler lock before calling this function.
|
||||
|
||||
Reference in New Issue
Block a user