diff --git a/esphome/core/component.cpp b/esphome/core/component.cpp index 3542a9547c..8bd7afd8c7 100644 --- a/esphome/core/component.cpp +++ b/esphome/core/component.cpp @@ -26,16 +26,22 @@ 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) + +// 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 +std::unique_ptr>> component_error_messages; +// Setup priority overrides - freed after setup completes +std::unique_ptr>> setup_priority_overrides; +} // namespace + static std::unique_ptr>> &get_component_error_messages() { - static std::unique_ptr>> instance; - return instance; + return 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; + return setup_priority_overrides; } namespace setup_priority { diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 7d9b86fccd..6074d95757 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -460,9 +460,15 @@ 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+/"; + +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 == '/')); } @@ -531,7 +537,7 @@ std::vector base64_decode(const std::string &encoded_string) { 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); @@ -548,7 +554,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);