mirror of
https://github.com/esphome/esphome.git
synced 2026-05-31 17:06:40 +08:00
[scheduler] Use integer math for interval offset calculation (#14755)
This commit is contained in:
@@ -105,10 +105,11 @@ static void validate_static_string(const char *name) {
|
|||||||
// avoid the main thread modifying the list while it is being accessed.
|
// avoid the main thread modifying the list while it is being accessed.
|
||||||
|
|
||||||
// Calculate random offset for interval timers
|
// Calculate random offset for interval timers
|
||||||
// Extracted from set_timer_common_ to reduce code size - float math + random_float()
|
// Extracted from set_timer_common_ to reduce code size - only needed for intervals, not timeouts
|
||||||
// only needed for intervals, not timeouts
|
|
||||||
uint32_t Scheduler::calculate_interval_offset_(uint32_t delay) {
|
uint32_t Scheduler::calculate_interval_offset_(uint32_t delay) {
|
||||||
return static_cast<uint32_t>(std::min(delay / 2, MAX_INTERVAL_DELAY) * random_float());
|
uint32_t max_offset = std::min(delay / 2, MAX_INTERVAL_DELAY);
|
||||||
|
// Multiply-and-shift: uniform random in [0, max_offset) without floating point
|
||||||
|
return static_cast<uint32_t>((static_cast<uint64_t>(random_uint32()) * max_offset) >> 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if a retry was already cancelled in items_ or to_add_
|
// Check if a retry was already cancelled in items_ or to_add_
|
||||||
|
|||||||
Reference in New Issue
Block a user