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 | ||||
| // 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<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 *>>> instance; | ||||
|   return instance; | ||||
|   return component_error_messages; | ||||
| } | ||||
|  | ||||
| // 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>>> instance; | ||||
|   return instance; | ||||
|   return setup_priority_overrides; | ||||
| } | ||||
|  | ||||
| namespace setup_priority { | ||||
|   | ||||
| @@ -460,10 +460,16 @@ int8_t step_to_accuracy_decimals(float step) { | ||||
|   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) | ||||
| 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 == '/')); } | ||||
|  | ||||
| std::string base64_encode(const std::vector<uint8_t> &buf) { return base64_encode(buf.data(), buf.size()); } | ||||
| @@ -531,7 +537,7 @@ std::vector<uint8_t> 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<uint8_t> 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); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user