mirror of
https://github.com/esphome/esphome.git
synced 2025-09-27 07:32:22 +01:00
cleanup
This commit is contained in:
@@ -23,16 +23,10 @@ static const char *const TAG = "scheduler";
|
|||||||
// See https://github.com/esphome/backlog/issues/52
|
// See https://github.com/esphome/backlog/issues/52
|
||||||
static constexpr size_t MAX_POOL_SIZE = 5;
|
static constexpr size_t MAX_POOL_SIZE = 5;
|
||||||
|
|
||||||
// Cleanup is performed when cancelled items exceed this percentage of total items.
|
// Maximum number of logically deleted (cancelled) items before forcing cleanup.
|
||||||
// Using integer math: cleanup when (cancelled * 100 / total) > 50
|
// Set to 5 to match the pool size - when we have as many cancelled items as our
|
||||||
// This balances cleanup frequency with performance - we avoid O(n) cleanup
|
// pool can hold, it's time to clean up and recycle them.
|
||||||
// on every cancellation but don't let cancelled items accumulate excessively.
|
static constexpr uint32_t MAX_LOGICALLY_DELETED_ITEMS = 5;
|
||||||
static constexpr uint32_t CLEANUP_PERCENTAGE = 50;
|
|
||||||
|
|
||||||
// Minimum number of cancelled items before considering cleanup.
|
|
||||||
// Even if the fraction is exceeded, we need at least this many cancelled items
|
|
||||||
// to make the O(n) cleanup operation worthwhile.
|
|
||||||
static constexpr uint32_t MIN_CANCELLED_ITEMS_FOR_CLEANUP = 3;
|
|
||||||
|
|
||||||
// Half the 32-bit range - used to detect rollovers vs normal time progression
|
// Half the 32-bit range - used to detect rollovers vs normal time progression
|
||||||
static constexpr uint32_t HALF_MAX_UINT32 = std::numeric_limits<uint32_t>::max() / 2;
|
static constexpr uint32_t HALF_MAX_UINT32 = std::numeric_limits<uint32_t>::max() / 2;
|
||||||
@@ -446,18 +440,11 @@ void HOT Scheduler::call(uint32_t now) {
|
|||||||
}
|
}
|
||||||
#endif /* ESPHOME_DEBUG_SCHEDULER */
|
#endif /* ESPHOME_DEBUG_SCHEDULER */
|
||||||
|
|
||||||
// Check if we should perform cleanup based on percentage of cancelled items
|
// Check if we should perform cleanup based on number of cancelled items
|
||||||
// Cleanup when: cancelled items >= MIN_CANCELLED_ITEMS_FOR_CLEANUP AND
|
// Cleanup when we have accumulated MAX_LOGICALLY_DELETED_ITEMS cancelled items
|
||||||
// cancelled percentage > CLEANUP_PERCENTAGE
|
// This simple threshold ensures we don't waste memory on cancelled items
|
||||||
size_t total_items = this->items_.size();
|
// regardless of how many intervals are running
|
||||||
bool should_cleanup = false;
|
if (this->to_remove_ >= MAX_LOGICALLY_DELETED_ITEMS) {
|
||||||
|
|
||||||
if (this->to_remove_ >= MIN_CANCELLED_ITEMS_FOR_CLEANUP && total_items > 0) {
|
|
||||||
// Use integer math to avoid floating point: (cancelled * 100 / total) > CLEANUP_PERCENTAGE
|
|
||||||
should_cleanup = (this->to_remove_ * 100) > (total_items * CLEANUP_PERCENTAGE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (should_cleanup) {
|
|
||||||
// We hold the lock for the entire cleanup operation because:
|
// We hold the lock for the entire cleanup operation because:
|
||||||
// 1. We're rebuilding the entire items_ list, so we need exclusive access throughout
|
// 1. We're rebuilding the entire items_ list, so we need exclusive access throughout
|
||||||
// 2. Other threads must see either the old state or the new state, not intermediate states
|
// 2. Other threads must see either the old state or the new state, not intermediate states
|
||||||
|
Reference in New Issue
Block a user