Reduce core RAM usage by 40 bytes with static initialization optimizations (#9340)

This commit is contained in:
J. Nick Koston 2025-07-07 15:28:14 -05:00 committed by GitHub
parent 053feb5e3b
commit e2de6ee29d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 30 deletions

View File

@ -26,17 +26,17 @@ static const char *const TAG = "component";
// 1. Components are never destroyed in ESPHome // 1. Components are never destroyed in ESPHome
// 2. Failed components remain failed (no recovery mechanism) // 2. Failed components remain failed (no recovery mechanism)
// 3. Memory usage is minimal (only failures with custom messages are stored) // 3. Memory usage is minimal (only failures with custom messages are stored)
static std::unique_ptr<std::vector<std::pair<const Component *, const char *>>> &get_component_error_messages() {
static std::unique_ptr<std::vector<std::pair<const Component *, const char *>>> 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<std::vector<std::pair<const Component *, const char *>>> component_error_messages;
// Setup priority overrides - freed after setup completes // Setup priority overrides - freed after setup completes
// Typically < 5 entries, lazy allocated // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static std::unique_ptr<std::vector<std::pair<const Component *, float>>> &get_setup_priority_overrides() { std::unique_ptr<std::vector<std::pair<const Component *, float>>> setup_priority_overrides;
static std::unique_ptr<std::vector<std::pair<const Component *, float>>> instance; } // namespace
return instance;
}
namespace setup_priority { namespace setup_priority {
@ -130,8 +130,8 @@ void Component::call_dump_config() {
if (this->is_failed()) { if (this->is_failed()) {
// Look up error message from global vector // Look up error message from global vector
const char *error_msg = "unspecified"; const char *error_msg = "unspecified";
if (get_component_error_messages()) { if (component_error_messages) {
for (const auto &pair : *get_component_error_messages()) { for (const auto &pair : *component_error_messages) {
if (pair.first == this) { if (pair.first == this) {
error_msg = pair.second; error_msg = pair.second;
break; 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); ESP_LOGE(TAG, "Component %s set Error flag: %s", this->get_component_source(), message);
if (strcmp(message, "unspecified") != 0) { if (strcmp(message, "unspecified") != 0) {
// Lazy allocate the error messages vector if needed // Lazy allocate the error messages vector if needed
if (!get_component_error_messages()) { if (!component_error_messages) {
get_component_error_messages() = std::make_unique<std::vector<std::pair<const Component *, const char *>>>(); component_error_messages = std::make_unique<std::vector<std::pair<const Component *, const char *>>>();
} }
// Check if this component already has an error message // 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) { if (pair.first == this) {
pair.second = message; pair.second = message;
return; return;
} }
} }
// Add new error message // Add new error message
get_component_error_messages()->emplace_back(this, message); component_error_messages->emplace_back(this, message);
} }
} }
void Component::status_clear_warning() { 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() {} void Component::dump_config() {}
float Component::get_actual_setup_priority() const { float Component::get_actual_setup_priority() const {
// Check if there's an override in the global vector // 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) // 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) { if (pair.first == this) {
return pair.second; return pair.second;
} }
@ -334,14 +334,14 @@ float Component::get_actual_setup_priority() const {
} }
void Component::set_setup_priority(float priority) { void Component::set_setup_priority(float priority) {
// Lazy allocate the vector if needed // Lazy allocate the vector if needed
if (!get_setup_priority_overrides()) { if (!setup_priority_overrides) {
get_setup_priority_overrides() = std::make_unique<std::vector<std::pair<const Component *, float>>>(); setup_priority_overrides = std::make_unique<std::vector<std::pair<const Component *, float>>>();
// Reserve some space to avoid reallocations (most configs have < 10 overrides) // 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 // 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) { if (pair.first == this) {
pair.second = priority; pair.second = priority;
return; return;
@ -349,7 +349,7 @@ void Component::set_setup_priority(float priority) {
} }
// Add new override // Add new override
get_setup_priority_overrides()->emplace_back(this, priority); setup_priority_overrides->emplace_back(this, priority);
} }
bool Component::has_overridden_loop() const { bool Component::has_overridden_loop() const {
@ -414,7 +414,7 @@ WarnIfComponentBlockingGuard::~WarnIfComponentBlockingGuard() {}
void clear_setup_priority_overrides() { void clear_setup_priority_overrides() {
// Free the setup priority map completely // Free the setup priority map completely
get_setup_priority_overrides().reset(); setup_priority_overrides.reset();
} }
} // namespace esphome } // namespace esphome

View File

@ -360,9 +360,22 @@ int8_t step_to_accuracy_decimals(float step) {
return str.length() - dot_pos - 1; return str.length() - dot_pos - 1;
} }
static const std::string BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // Use C-style string constant to store in ROM instead of RAM (saves 24 bytes)
"abcdefghijklmnopqrstuvwxyz" static constexpr const char *BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789+/"; "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 == '/')); } 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; char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; (i < 4); i++) for (i = 0; (i < 4); i++)
ret += BASE64_CHARS[char_array_4[i]]; ret += BASE64_CHARS[static_cast<uint8_t>(char_array_4[i])];
i = 0; 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; char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++) for (j = 0; (j < i + 1); j++)
ret += BASE64_CHARS[char_array_4[j]]; ret += BASE64_CHARS[static_cast<uint8_t>(char_array_4[j])];
while ((i++ < 3)) while ((i++ < 3))
ret += '='; ret += '=';
@ -426,12 +439,15 @@ std::vector<uint8_t> base64_decode(const std::string &encoded_string) {
uint8_t char_array_4[4], char_array_3[3]; uint8_t char_array_4[4], char_array_3[3];
std::vector<uint8_t> ret; std::vector<uint8_t> 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])) { while (in_len-- && (encoded_string[in] != '=') && is_base64(encoded_string[in])) {
char_array_4[i++] = encoded_string[in]; char_array_4[i++] = encoded_string[in];
in++; in++;
if (i == 4) { if (i == 4) {
for (i = 0; i < 4; i++) 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[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); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
@ -448,7 +464,7 @@ std::vector<uint8_t> base64_decode(const std::string &encoded_string) {
char_array_4[j] = 0; char_array_4[j] = 0;
for (j = 0; j < 4; j++) 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[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); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);