mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Reduce core RAM usage by 40 bytes with static initialization optimizations
This commit is contained in:
		| @@ -26,16 +26,22 @@ 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) | ||||||
|  |  | ||||||
|  | // 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<std::vector<std::pair<const Component *, const char *>>> component_error_messages; | ||||||
|  | // Setup priority overrides - freed after setup completes | ||||||
|  | std::unique_ptr<std::vector<std::pair<const Component *, float>>> setup_priority_overrides; | ||||||
|  | }  // namespace | ||||||
|  |  | ||||||
| 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 *>>> &get_component_error_messages() { | ||||||
|   static std::unique_ptr<std::vector<std::pair<const Component *, const char *>>> instance; |   return component_error_messages; | ||||||
|   return instance; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // Setup priority overrides - freed after setup completes |  | ||||||
| // Typically < 5 entries, lazy allocated |  | ||||||
| static std::unique_ptr<std::vector<std::pair<const Component *, float>>> &get_setup_priority_overrides() { | static std::unique_ptr<std::vector<std::pair<const Component *, float>>> &get_setup_priority_overrides() { | ||||||
|   static std::unique_ptr<std::vector<std::pair<const Component *, float>>> instance; |   return setup_priority_overrides; | ||||||
|   return instance; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| namespace setup_priority { | namespace setup_priority { | ||||||
|   | |||||||
| @@ -460,9 +460,15 @@ 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+/"; | ||||||
|  |  | ||||||
|  | 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 == '/')); } | ||||||
|  |  | ||||||
| @@ -531,7 +537,7 @@ std::vector<uint8_t> base64_decode(const std::string &encoded_string) { | |||||||
|     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); | ||||||
| @@ -548,7 +554,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); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user