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:
J. Nick Koston
2026-03-17 01:58:25 -10:00
parent f654f70c39
commit 55a76fcbca
2 changed files with 7 additions and 7 deletions
+5 -5
View File
@@ -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);
+2 -2
View File
@@ -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.