mirror of
https://github.com/esphome/esphome.git
synced 2025-09-07 13:52:20 +01:00
improve pool hit rate
This commit is contained in:
@@ -349,6 +349,8 @@ void HOT Scheduler::call(uint32_t now) {
|
|||||||
if (!this->should_skip_item_(item.get())) {
|
if (!this->should_skip_item_(item.get())) {
|
||||||
this->execute_item_(item.get(), now);
|
this->execute_item_(item.get(), now);
|
||||||
}
|
}
|
||||||
|
// Recycle the defer item after execution
|
||||||
|
this->recycle_item_(std::move(item));
|
||||||
}
|
}
|
||||||
#endif /* not ESPHOME_THREAD_SINGLE */
|
#endif /* not ESPHOME_THREAD_SINGLE */
|
||||||
|
|
||||||
|
@@ -144,33 +144,39 @@ script:
|
|||||||
then:
|
then:
|
||||||
- lambda: |-
|
- lambda: |-
|
||||||
// Simulate defer patterns (state changes, async operations)
|
// Simulate defer patterns (state changes, async operations)
|
||||||
ESP_LOGI("test", "Phase 4: Simulating defer patterns");
|
ESP_LOGI("test", "Phase 4: Simulating heavy defer patterns like ratgdo");
|
||||||
|
|
||||||
class TestComponent : public Component {
|
auto *component = id(test_sensor);
|
||||||
public:
|
|
||||||
void simulate_state_changes() {
|
|
||||||
// Defer state changes (common in switches, lights, etc)
|
|
||||||
this->defer("state_change_1", []() {
|
|
||||||
ESP_LOGD("test", "State change 1 applied");
|
|
||||||
id(create_count)++;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Another state change
|
// Simulate a burst of defer operations like ratgdo does with state updates
|
||||||
this->defer("state_change_2", []() {
|
// These should execute immediately and recycle quickly to the pool
|
||||||
ESP_LOGD("test", "State change 2 applied");
|
for (int i = 0; i < 10; i++) {
|
||||||
id(create_count)++;
|
std::string defer_name = "defer_" + std::to_string(i);
|
||||||
});
|
App.scheduler.set_timeout(component, defer_name, 0, [i]() {
|
||||||
|
ESP_LOGD("test", "Defer %d executed", i);
|
||||||
|
// Force a small delay between defer executions to see recycling
|
||||||
|
if (i == 5) {
|
||||||
|
ESP_LOGI("test", "Half of defers executed, checking pool status");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Cleanup operation
|
id(create_count) += 10;
|
||||||
this->defer("cleanup", []() {
|
ESP_LOGD("test", "Created 10 defer operations (0ms timeouts)");
|
||||||
ESP_LOGD("test", "Cleanup executed");
|
|
||||||
id(create_count)++;
|
// Also create some named defers that might get replaced
|
||||||
});
|
App.scheduler.set_timeout(component, "state_update", 0, []() {
|
||||||
}
|
ESP_LOGD("test", "State update 1");
|
||||||
};
|
});
|
||||||
|
|
||||||
|
// Replace the same named defer (should cancel previous)
|
||||||
|
App.scheduler.set_timeout(component, "state_update", 0, []() {
|
||||||
|
ESP_LOGD("test", "State update 2 (replaced)");
|
||||||
|
});
|
||||||
|
|
||||||
|
id(create_count) += 2;
|
||||||
|
id(cancel_count) += 1; // One cancelled due to replacement
|
||||||
|
|
||||||
static TestComponent test_comp;
|
|
||||||
test_comp.simulate_state_changes();
|
|
||||||
ESP_LOGI("test", "Phase 4 complete");
|
ESP_LOGI("test", "Phase 4 complete");
|
||||||
|
|
||||||
- id: test_pool_reuse_verification
|
- id: test_pool_reuse_verification
|
||||||
|
Reference in New Issue
Block a user