mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-03 08:31:47 +00:00 
			
		
		
		
	Eliminate API component guard variable to save 8 bytes RAM (#9341)
This commit is contained in:
		@@ -24,6 +24,14 @@ static const char *const TAG = "api";
 | 
				
			|||||||
// APIServer
 | 
					// APIServer
 | 
				
			||||||
APIServer *global_api_server = nullptr;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
 | 
					APIServer *global_api_server = nullptr;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef USE_API_YAML_SERVICES
 | 
				
			||||||
 | 
					// Global empty vector to avoid guard variables (saves 8 bytes)
 | 
				
			||||||
 | 
					// This is initialized at program startup before any threads
 | 
				
			||||||
 | 
					static const std::vector<UserServiceDescriptor *> empty_user_services{};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const std::vector<UserServiceDescriptor *> &get_empty_user_services_instance() { return empty_user_services; }
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
APIServer::APIServer() {
 | 
					APIServer::APIServer() {
 | 
				
			||||||
  global_api_server = this;
 | 
					  global_api_server = this;
 | 
				
			||||||
  // Pre-allocate shared write buffer
 | 
					  // Pre-allocate shared write buffer
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,6 +25,11 @@ struct SavedNoisePsk {
 | 
				
			|||||||
} PACKED;  // NOLINT
 | 
					} PACKED;  // NOLINT
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef USE_API_YAML_SERVICES
 | 
				
			||||||
 | 
					// Forward declaration of helper function
 | 
				
			||||||
 | 
					const std::vector<UserServiceDescriptor *> &get_empty_user_services_instance();
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class APIServer : public Component, public Controller {
 | 
					class APIServer : public Component, public Controller {
 | 
				
			||||||
 public:
 | 
					 public:
 | 
				
			||||||
  APIServer();
 | 
					  APIServer();
 | 
				
			||||||
@@ -151,8 +156,11 @@ class APIServer : public Component, public Controller {
 | 
				
			|||||||
#ifdef USE_API_YAML_SERVICES
 | 
					#ifdef USE_API_YAML_SERVICES
 | 
				
			||||||
    return this->user_services_;
 | 
					    return this->user_services_;
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
    static const std::vector<UserServiceDescriptor *> EMPTY;
 | 
					    if (this->user_services_) {
 | 
				
			||||||
    return this->user_services_ ? *this->user_services_ : EMPTY;
 | 
					      return *this->user_services_;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    // Return reference to global empty instance (no guard needed)
 | 
				
			||||||
 | 
					    return get_empty_user_services_instance();
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user