From 866574ca14dbfdcaaa205be27cdaa9852ac4aadd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Sep 2026 12:23:09 +0200 Subject: [PATCH] [noise] Keep only the slot flag test inline in the api loop The per tick check is a byte load and branch now; the network check, client scan and refill live in prepare_spare_ephemeral_slow_(), called only while the slot is empty. --- esphome/components/api/api_server.cpp | 4 ++-- esphome/components/api/api_server.h | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index ddcccc0b6e..438283e826 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -199,8 +199,8 @@ void APIServer::loop() { #ifdef USE_API_NOISE // Refill only while no api client is still connecting; an OTA handshake is // not visible here and just pays the refill it triggered. -void APIServer::prepare_spare_ephemeral_() { - if (noise::has_spare_ephemeral() || !network::is_connected()) { +void APIServer::prepare_spare_ephemeral_slow_() { + if (!network::is_connected()) { return; } const uint32_t now = App.get_loop_component_start_time(); diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index 673def615a..5e5f472c1f 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -364,7 +364,13 @@ class APIServer final : public Component, #endif #ifdef USE_API_NOISE - void prepare_spare_ephemeral_(); + // Polled every loop tick: only the flag test stays inline + void prepare_spare_ephemeral_() { + if (!noise::has_spare_ephemeral()) { + this->prepare_spare_ephemeral_slow_(); + } + } + void prepare_spare_ephemeral_slow_(); noise::NoiseContext noise_ctx_; #ifndef USE_API_NOISE_PSK_FROM_YAML SavedNoisePsk saved_psk_{}; // backs noise_ctx_ for a runtime provisioned key