From e2de6ee29d7a7f29df04fd0f869f212cbf33a01d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Jul 2025 15:28:14 -0500 Subject: [PATCH] Reduce core RAM usage by 40 bytes with static initialization optimizations (#9340) --- esphome/core/component.cpp | 46 +++++++++++++++++++------------------- esphome/core/helpers.cpp | 30 +++++++++++++++++++------ 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/esphome/core/component.cpp b/esphome/core/component.cpp index 9ef30081aa..9d863e56cd 100644 --- a/esphome/core/component.cpp +++ b/esphome/core/component.cpp @@ -26,17 +26,17 @@ static const char *const TAG = "component"; // 1. Components are never destroyed in ESPHome // 2. Failed components remain failed (no recovery mechanism) // 3. Memory usage is minimal (only failures with custom messages are stored) -static std::unique_ptr>> &get_component_error_messages() { - static std::unique_ptr>> instance; - return instance; -} +// Using namespace-scope static to avoid guard variables (saves 16 bytes total) +// This is safe because ESPHome is single-threaded during initialization +namespace { +// Error messages for failed components +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +std::unique_ptr>> component_error_messages; // Setup priority overrides - freed after setup completes -// Typically < 5 entries, lazy allocated -static std::unique_ptr>> &get_setup_priority_overrides() { - static std::unique_ptr>> instance; - return instance; -} +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +std::unique_ptr>> setup_priority_overrides; +} // namespace namespace setup_priority { @@ -130,8 +130,8 @@ void Component::call_dump_config() { if (this->is_failed()) { // Look up error message from global vector const char *error_msg = "unspecified"; - if (get_component_error_messages()) { - for (const auto &pair : *get_component_error_messages()) { + if (component_error_messages) { + for (const auto &pair : *component_error_messages) { if (pair.first == this) { error_msg = pair.second; break; @@ -285,18 +285,18 @@ void Component::status_set_error(const char *message) { ESP_LOGE(TAG, "Component %s set Error flag: %s", this->get_component_source(), message); if (strcmp(message, "unspecified") != 0) { // Lazy allocate the error messages vector if needed - if (!get_component_error_messages()) { - get_component_error_messages() = std::make_unique>>(); + if (!component_error_messages) { + component_error_messages = std::make_unique>>(); } // Check if this component already has an error message - for (auto &pair : *get_component_error_messages()) { + for (auto &pair : *component_error_messages) { if (pair.first == this) { pair.second = message; return; } } // Add new error message - get_component_error_messages()->emplace_back(this, message); + component_error_messages->emplace_back(this, message); } } void Component::status_clear_warning() { @@ -322,9 +322,9 @@ void Component::status_momentary_error(const std::string &name, uint32_t length) void Component::dump_config() {} float Component::get_actual_setup_priority() const { // Check if there's an override in the global vector - if (get_setup_priority_overrides()) { + if (setup_priority_overrides) { // Linear search is fine for small n (typically < 5 overrides) - for (const auto &pair : *get_setup_priority_overrides()) { + for (const auto &pair : *setup_priority_overrides) { if (pair.first == this) { return pair.second; } @@ -334,14 +334,14 @@ float Component::get_actual_setup_priority() const { } void Component::set_setup_priority(float priority) { // Lazy allocate the vector if needed - if (!get_setup_priority_overrides()) { - get_setup_priority_overrides() = std::make_unique>>(); + if (!setup_priority_overrides) { + setup_priority_overrides = std::make_unique>>(); // Reserve some space to avoid reallocations (most configs have < 10 overrides) - get_setup_priority_overrides()->reserve(10); + setup_priority_overrides->reserve(10); } // Check if this component already has an override - for (auto &pair : *get_setup_priority_overrides()) { + for (auto &pair : *setup_priority_overrides) { if (pair.first == this) { pair.second = priority; return; @@ -349,7 +349,7 @@ void Component::set_setup_priority(float priority) { } // Add new override - get_setup_priority_overrides()->emplace_back(this, priority); + setup_priority_overrides->emplace_back(this, priority); } bool Component::has_overridden_loop() const { @@ -414,7 +414,7 @@ WarnIfComponentBlockingGuard::~WarnIfComponentBlockingGuard() {} void clear_setup_priority_overrides() { // Free the setup priority map completely - get_setup_priority_overrides().reset(); + setup_priority_overrides.reset(); } } // namespace esphome diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 72722169d4..22b74e11fa 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -360,9 +360,22 @@ int8_t step_to_accuracy_decimals(float step) { return str.length() - dot_pos - 1; } -static const std::string BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; +// Use C-style string constant to store in ROM instead of RAM (saves 24 bytes) +static constexpr const char *BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +// Helper function to find the index of a base64 character in the lookup table. +// Returns the character's position (0-63) if found, or 0 if not found. +// NOTE: This returns 0 for both 'A' (valid base64 char at index 0) and invalid characters. +// This is safe because is_base64() is ALWAYS checked before calling this function, +// preventing invalid characters from ever reaching here. The base64_decode function +// stops processing at the first invalid character due to the is_base64() check in its +// while loop condition, making this edge case harmless in practice. +static inline uint8_t base64_find_char(char c) { + const char *pos = strchr(BASE64_CHARS, c); + return pos ? (pos - BASE64_CHARS) : 0; +} static inline bool is_base64(char c) { return (isalnum(c) || (c == '+') || (c == '/')); } @@ -384,7 +397,7 @@ std::string base64_encode(const uint8_t *buf, size_t buf_len) { char_array_4[3] = char_array_3[2] & 0x3f; for (i = 0; (i < 4); i++) - ret += BASE64_CHARS[char_array_4[i]]; + ret += BASE64_CHARS[static_cast(char_array_4[i])]; i = 0; } } @@ -399,7 +412,7 @@ std::string base64_encode(const uint8_t *buf, size_t buf_len) { char_array_4[3] = char_array_3[2] & 0x3f; for (j = 0; (j < i + 1); j++) - ret += BASE64_CHARS[char_array_4[j]]; + ret += BASE64_CHARS[static_cast(char_array_4[j])]; while ((i++ < 3)) ret += '='; @@ -426,12 +439,15 @@ std::vector base64_decode(const std::string &encoded_string) { uint8_t char_array_4[4], char_array_3[3]; std::vector ret; + // SAFETY: The loop condition checks is_base64() before processing each character. + // This ensures base64_find_char() is only called on valid base64 characters, + // preventing the edge case where invalid chars would return 0 (same as 'A'). while (in_len-- && (encoded_string[in] != '=') && is_base64(encoded_string[in])) { char_array_4[i++] = encoded_string[in]; in++; if (i == 4) { for (i = 0; i < 4; i++) - char_array_4[i] = BASE64_CHARS.find(char_array_4[i]); + char_array_4[i] = base64_find_char(char_array_4[i]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); @@ -448,7 +464,7 @@ std::vector base64_decode(const std::string &encoded_string) { char_array_4[j] = 0; for (j = 0; j < 4; j++) - char_array_4[j] = BASE64_CHARS.find(char_array_4[j]); + char_array_4[j] = base64_find_char(char_array_4[j]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);