1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-15 01:32:19 +01:00

Reduce Component blocking threshold memory usage by 2 bytes per component

This commit is contained in:
J. Nick Koston
2025-06-14 16:42:08 -05:00
parent ee37d2f9c8
commit 80cbe5c7c9
2 changed files with 12 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
#include "esphome/core/component.h"
#include <cinttypes>
#include <limits>
#include <utility>
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
@@ -39,8 +40,8 @@ const uint32_t STATUS_LED_OK = 0x0000;
const uint32_t STATUS_LED_WARNING = 0x0100;
const uint32_t STATUS_LED_ERROR = 0x0200;
const uint32_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_OVER_MS = 50U; ///< Initial blocking time allowed without warning
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)
@@ -120,7 +121,13 @@ const char *Component::get_component_source() const {
}
bool Component::should_warn_of_blocking(uint32_t blocking_time) {
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 false;

View File

@@ -65,7 +65,7 @@ extern const uint32_t STATUS_LED_ERROR;
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 {
public:
@@ -301,7 +301,7 @@ class Component {
uint32_t component_state_{0x0000}; ///< State of this component.
float setup_priority_override_{NAN};
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_{};
};