Two fixes for ESP8266 with noise encryption:
1. Cache socket ready() before the handshake loop. On ESP8266 LWIP raw
TCP, ready() returns the live state (false once rx buffer is consumed),
unlike ESP32 where it is cached until the next main loop. Re-checking
each iteration blocked handshake writes that must follow reads,
deadlocking the handshake.
2. Process client removal immediately after loop() instead of deferring
to the next server loop iteration. This closes the socket promptly
to free LWIP PCB resources and prevent retransmit crashes on ESP8266.
On ESP8266 (GCC 10.3), std::vector::push_back/emplace_back emit
separate _M_realloc_insert functions even when called from only
one site. Adding __attribute__((flatten)) inlines the realloc
path, saving the out-of-line function overhead.
Changes:
- wifi: Move set_sta_priority from header to .cpp (eliminates
duplicate instantiation at 2 call sites) and add flatten
- web_server_base: Flatten add_handler (single push_back site)
- api: Flatten accept_new_connections_ (single emplace_back site)
- api: Extract push_item_ as single flattened push_back site for
DeferredBatch (was 2 call sites in add_item/add_item_front)
Saves ~200 bytes of flash on ESP8266, ~40 bytes on ESP32.
On ESP8266 (GCC 10.3), std::vector::push_back/emplace_back emit
separate _M_realloc_insert functions even when called from only
one site. Adding __attribute__((flatten)) inlines the realloc
path, saving the out-of-line function overhead.
Changes:
- wifi: Move set_sta_priority from header to .cpp (eliminates
duplicate instantiation at 2 call sites) and add flatten
- web_server_base: Flatten add_handler (single push_back site)
- api: Flatten accept_new_connections_ (single emplace_back site)
Saves 160 bytes of flash on ESP8266.
On ESP8266 (GCC 10.3), std::vector::push_back() emits a separate
_M_realloc_insert<DeferredEvent> function (198 bytes) that is only
called from one site. Adding __attribute__((flatten)) forces the
compiler to inline it, eliminating the out-of-line function's
prologue/epilogue and call overhead.
This matches what GCC 14.2 (ESP-IDF toolchain) already does
naturally for the equivalent code in web_server_idf.cpp.
Saves 80 bytes of flash on ESP8266.
Extract the keepalive ping sending logic from APIConnection::loop()
into a separate noinline send_keepalive_ping_() method. This code
only fires once per keepalive interval (~60s) making it cold relative
to the ~111 Hz loop rate. Moving it out reduces loop() from 337 to
258 bytes, keeping the hot path smaller and more cache-friendly.
- Add #include <type_traits> to both .cpp and .h for static_asserts
- Clarify CompactString comment: explicitly note it is not trivially
copyable, and that memcpy safety relies on validated layout property
- Use memcpy_fn indirection to suppress both GCC -Wclass-memaccess
and clang-tidy bugprone-undefined-memory-manipulation without
platform-specific pragma guards
Replace copy-assignment with raw memcpy in the WiFi scan result
insertion sort. Copy assignment on WiFiScanResult calls
CompactString's destructor then placement-new for every shift,
which means delete[]/new[] per shift for heap-allocated SSIDs.
With 70+ networks visible (e.g., during captive portal transition
showing full scan results), this caused event loop blocking from
hundreds of heap allocations in a tight loop on an 80MHz ESP8266.
This optimization is safe because we're permuting elements within
the same array - each slot is overwritten exactly once, so no
ownership duplication occurs. CompactString stores either inline
data or a heap pointer, never a self-referential pointer (unlike
libstdc++ std::string SSO). This was made possible by PR#13472
which replaced std::string with CompactString.
Static asserts guard the memcpy safety assumptions at compile time.
Confirmed on real device: event loop blocking during captive portal
transition is eliminated and WiFi connection is slightly faster.
Add @warning comments to all format_hex_pretty() overloads in helpers.h
pointing to the buffer-based format_hex_pretty_to() alternatives.
Deprecate with ESPDEPRECATED (removal 2026.9.0):
- EthernetComponent::get_eth_mac_address_pretty()
-> use get_eth_mac_address_pretty_into_buffer()
- AsyncWebServerRequest::url()
-> use url_to()
Neither deprecated method has internal callers.
wifi_ssid() returns std::string which allocates on the heap.
All internal callers have already been migrated to the
buffer-based wifi_ssid_to() alternative. Mark as deprecated
with a 6-month removal window (2026.9.0).
bLength includes the 2-byte descriptor header, so the character count
is (bLength - 2) / 2, not bLength / 2. The old loop read one wData
entry past the actual string data. Also guard bLength < 2.
Extract the USB_CLIENT_OPEN state handling into handle_open_state_() to
keep loop() hot path small (~112 B vs 440 B before). Replace
get_descriptor_string() std::string return with caller-provided
fixed-size buffer via std::span to eliminate heap allocation during
device connection.