From 5fab869286302ec8d29cf97471922a97e652aa40 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 24 Apr 2026 02:57:21 -0500 Subject: [PATCH] [web_server_idf] Move has_pending_sessions_ to end to tighten layout Keep the single-byte std::atomic as the last member of AsyncEventSource so it consumes what would otherwise be trailing padding instead of introducing 3 bytes of interior padding between pending_mutex_ and on_connect_. --- esphome/components/web_server_idf/web_server_idf.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/esphome/components/web_server_idf/web_server_idf.h b/esphome/components/web_server_idf/web_server_idf.h index 04a856ae04..60d07534b9 100644 --- a/esphome/components/web_server_idf/web_server_idf.h +++ b/esphome/components/web_server_idf/web_server_idf.h @@ -351,6 +351,9 @@ class AsyncEventSource : public AsyncWebHandler { size_t count() const { return this->sessions_.size(); } protected: + // Members are ordered to minimize padding on 32-bit: all 4-byte-aligned members + // first, then the single-byte atomic at the end to consume what would otherwise + // be trailing padding. std::string url_; // Use vector instead of set: SSE sessions are typically 1-5 connections (browsers, dashboards). // Linear search is faster than red-black tree overhead for this small dataset. @@ -358,13 +361,14 @@ class AsyncEventSource : public AsyncWebHandler { // Mutated only from the main loop. std::vector sessions_; // Sessions constructed on the httpd task wait here until the main loop adopts them. - // All mutations are guarded by pending_mutex_. has_pending_sessions_ is a fast-path - // gate so the per-tick cost in loop() when no connect is pending is one atomic load. + // All mutations are guarded by pending_mutex_. has_pending_sessions_ (below) is a + // fast-path gate so the per-tick cost in loop() when no connect is pending is one + // atomic load. std::vector pending_sessions_; Mutex pending_mutex_; - std::atomic has_pending_sessions_{false}; connect_handler_t on_connect_{}; esphome::web_server::WebServer *web_server_; + std::atomic has_pending_sessions_{false}; }; #endif // USE_WEBSERVER