1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-14 09:12:19 +01:00

Optimize OTA loop to avoid unnecessary stack allocations

This commit is contained in:
J. Nick Koston
2025-06-18 13:44:07 +02:00
parent 5ffe50381a
commit 7f1d0eef98

View File

@@ -82,7 +82,13 @@ void ESPHomeOTAComponent::dump_config() {
#endif #endif
} }
void ESPHomeOTAComponent::loop() { this->handle_(); } void ESPHomeOTAComponent::loop() {
// Skip handle_() call if no client connected and no incoming connections
// This optimization reduces idle loop overhead when OTA is not active
if (client_ != nullptr || (server_ && server_->ready())) {
this->handle_();
}
}
static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01; static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01;
@@ -102,13 +108,11 @@ void ESPHomeOTAComponent::handle_() {
#endif #endif
if (client_ == nullptr) { if (client_ == nullptr) {
// Check if the server socket is ready before accepting // We already checked server_->ready() in loop(), so we can accept directly
if (this->server_->ready()) {
struct sockaddr_storage source_addr; struct sockaddr_storage source_addr;
socklen_t addr_len = sizeof(source_addr); socklen_t addr_len = sizeof(source_addr);
client_ = server_->accept((struct sockaddr *) &source_addr, &addr_len); client_ = server_->accept((struct sockaddr *) &source_addr, &addr_len);
} }
}
if (client_ == nullptr) if (client_ == nullptr)
return; return;