mirror of
https://github.com/esphome/esphome.git
synced 2026-09-02 02:56:01 +00:00
Move the api_is_connected() definition from util.cpp to util.h and mark it ESPHOME_ALWAYS_INLINE. The body is trivial — a nullptr check on global_api_server plus APIServer::is_connected() (which is !clients_.empty()) — so the out-of-line call8 was pure overhead for hot paths that check connectivity every loop tick (e.g. zwave_proxy::loop, serial_proxy::loop). With USE_API disabled the function collapses to "return false" at compile time and folds away entirely. util.h now pulls in api_server.h under USE_API. Only 25 .cpp files include util.h and all of them are on USE_API builds in practice (wifi, safe_mode, nextion, web_server, etc.), so the additional transitive include is essentially free on the platforms that matter. Measured on ESP32-S3: the call8 in ZWaveProxy::loop() is replaced by 5 inline Xtensa instructions (load global_api_server, deref, null check, clients_ start/finish compare) — no call overhead, no stack frame.
25 lines
539 B
C++
25 lines
539 B
C++
#include "esphome/core/util.h"
|
|
#include "esphome/core/defines.h"
|
|
#include "esphome/core/application.h"
|
|
#include "esphome/core/version.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
#ifdef USE_MQTT
|
|
#include "esphome/components/mqtt/mqtt_client.h"
|
|
#endif
|
|
|
|
namespace esphome {
|
|
|
|
bool mqtt_is_connected() {
|
|
#ifdef USE_MQTT
|
|
if (mqtt::global_mqtt_client != nullptr) {
|
|
return mqtt::global_mqtt_client->is_connected();
|
|
}
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
bool remote_is_connected() { return api_is_connected() || mqtt_is_connected(); }
|
|
|
|
} // namespace esphome
|