Reduce Component blocking threshold memory usage by 2 bytes per component (#9081)

This commit is contained in:
J. Nick Koston 2025-06-15 16:33:38 -05:00 committed by Jesse Hills
parent e435e72654
commit 20d7ba5d7c
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
2 changed files with 12 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include <cinttypes> #include <cinttypes>
#include <limits>
#include <utility> #include <utility>
#include "esphome/core/application.h" #include "esphome/core/application.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
@ -41,8 +42,8 @@ const uint8_t STATUS_LED_OK = 0x00;
const uint8_t STATUS_LED_WARNING = 0x04; // Bit 2 const uint8_t STATUS_LED_WARNING = 0x04; // Bit 2
const uint8_t STATUS_LED_ERROR = 0x08; // Bit 3 const uint8_t STATUS_LED_ERROR = 0x08; // Bit 3
const uint32_t WARN_IF_BLOCKING_OVER_MS = 50U; ///< Initial blocking time allowed without warning const uint16_t WARN_IF_BLOCKING_OVER_MS = 50U; ///< Initial blocking time allowed without warning
const uint32_t WARN_IF_BLOCKING_INCREMENT_MS = 10U; ///< How long the blocking time must be larger to warn again const uint16_t WARN_IF_BLOCKING_INCREMENT_MS = 10U; ///< How long the blocking time must be larger to warn again
uint32_t global_state = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) uint32_t global_state = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
@ -122,7 +123,13 @@ const char *Component::get_component_source() const {
} }
bool Component::should_warn_of_blocking(uint32_t blocking_time) { bool Component::should_warn_of_blocking(uint32_t blocking_time) {
if (blocking_time > this->warn_if_blocking_over_) { if (blocking_time > this->warn_if_blocking_over_) {
this->warn_if_blocking_over_ = blocking_time + WARN_IF_BLOCKING_INCREMENT_MS; // Prevent overflow when adding increment - if we're about to overflow, just max out
if (blocking_time + WARN_IF_BLOCKING_INCREMENT_MS < blocking_time ||
blocking_time + WARN_IF_BLOCKING_INCREMENT_MS > std::numeric_limits<uint16_t>::max()) {
this->warn_if_blocking_over_ = std::numeric_limits<uint16_t>::max();
} else {
this->warn_if_blocking_over_ = static_cast<uint16_t>(blocking_time + WARN_IF_BLOCKING_INCREMENT_MS);
}
return true; return true;
} }
return false; return false;

View File

@ -65,7 +65,7 @@ extern const uint8_t STATUS_LED_ERROR;
enum class RetryResult { DONE, RETRY }; enum class RetryResult { DONE, RETRY };
extern const uint32_t WARN_IF_BLOCKING_OVER_MS; extern const uint16_t WARN_IF_BLOCKING_OVER_MS;
class Component { class Component {
public: public:
@ -318,7 +318,7 @@ class Component {
uint8_t component_state_{0x00}; uint8_t component_state_{0x00};
float setup_priority_override_{NAN}; float setup_priority_override_{NAN};
const char *component_source_{nullptr}; const char *component_source_{nullptr};
uint32_t warn_if_blocking_over_{WARN_IF_BLOCKING_OVER_MS}; uint16_t warn_if_blocking_over_{WARN_IF_BLOCKING_OVER_MS}; ///< Warn if blocked for this many ms (max 65.5s)
std::string error_message_{}; std::string error_message_{};
}; };