mirror of
https://github.com/esphome/esphome.git
synced 2026-09-25 14:00:25 +00:00
[scheduler] Early exit cancel path in set_timer_common_ and fix benchmark
Add find_first parameter to cancel_item_locked_ and mark_matching_items_removed_locked_, defaulting to false (cancel all). When find_first=true, stops after the first match and exits across containers. set_timer_common_ passes find_first=true where the cancel-before-add invariant guarantees at most one match. The public cancel path uses the default find_first=false to cancel ALL matches, needed for DelayAction parallel mode where skip_cancel=true can create multiple items with the same key. Also fixes the Scheduler_SetInterval benchmark to call process_to_add() every kKeyCount registrations with a final drain after the inner loop, reflecting production behavior.
This commit is contained in:
@@ -99,11 +99,21 @@ BENCHMARK(Scheduler_SetTimeout);
|
||||
static void Scheduler_SetInterval(benchmark::State &state) {
|
||||
Scheduler scheduler;
|
||||
Component dummy_component;
|
||||
// Number of distinct interval keys; controls how many unique timers exist
|
||||
// simultaneously and the drain cadence for process_to_add().
|
||||
static constexpr int kKeyCount = 5;
|
||||
|
||||
for (auto _ : state) {
|
||||
for (int i = 0; i < kInnerIterations; i++) {
|
||||
scheduler.set_interval(&dummy_component, static_cast<uint32_t>(i % 5), 1000, []() {});
|
||||
scheduler.set_interval(&dummy_component, static_cast<uint32_t>(i % kKeyCount), 1000, []() {});
|
||||
// Drain to_add_ periodically to reflect production behavior where
|
||||
// process_to_add() runs each main loop iteration. Without this,
|
||||
// cancelled items accumulate in to_add_ causing O(n²) scan cost.
|
||||
if ((i + 1) % kKeyCount == 0) {
|
||||
scheduler.process_to_add();
|
||||
}
|
||||
}
|
||||
// Final drain in case kInnerIterations is not a multiple of 5
|
||||
scheduler.process_to_add();
|
||||
benchmark::DoNotOptimize(scheduler);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user