J. Nick Koston 250d28f69c [api] Replace clients_ std::vector with compile-time std::array + uint8_t count
Convert APIServer's client container from
`std::vector<std::unique_ptr<APIConnection>>` to a compile-time sized
`std::array<..., MAX_API_CONNECTIONS>` with an inline `uint8_t
api_connection_count_` tracking the active slice.

Why:
- Eliminates a persistent heap-held buffer (one of APIServer's
  fragmentation sources). The vector grows via doubling reallocations
  (0 -> 1 -> 2 -> 4 -> 8) and keeps its capacity for the life of the
  process.
- Kills ~100-150 bytes of `_M_realloc_insert` template instantiation
  per build (measured: -92 B on ESP32-S3, -152 B on ESP8266).
- is_connected() becomes `api_connection_count_ != 0` — a single-byte
  load, down from two pointer loads + compare.
- The count lives in what was already 1 byte of padding after
  shutting_down_, replacing the removed max_connections_ runtime
  setter. Zero size overhead in that slot.
- Max connections is now a compile-time constant via
  cg.add_define("MAX_API_CONNECTIONS", ...), so the accept cap check
  and the array size derive from the same value.

Also lower default max_connections on mid/high-RAM platforms from 8 to
6. With a compile-time array, every slot costs 4 bytes of static RAM
whether a connection ever uses it or not — 6 covers HA + dashboard +
spares without paying the full 8-slot static cost. esp8266 stays at 4,
rp2040 stays at 4.

Mechanical churn:
- 13 `for (auto &c : this->clients_)` -> `for (auto &c : this->active_clients())`,
  where active_clients() returns a pointer-range view over the active
  slice.
- `.empty()` / `.size()` -> comparisons against api_connection_count_.
- `.emplace_back()` / `.pop_back()` / `.back()` rewritten in the
  swap-and-pop path to index into the array and reset() the freed slot
  (maintains the invariant that slots [count, N) are always nullptr).

Measured on ESP32-S3 (zwave-proxy-seeedw5500, N=6):
  RAM: 32464 -> 32480 B  (+16 B static, but no heap buffer)
  Flash: 469011 -> 468919 B  (-92 B)

Measured on ESP8266 (basic8266, N=4):
  RAM: 29268 -> 29268 B  (unchanged at this granularity)
  Flash: 323847 -> 323695 B  (-152 B)
2026-04-21 11:54:13 +02:00
2023-06-12 17:00:34 +12:00
2022-09-06 15:48:01 +12:00
2025-12-21 09:26:03 -05:00
2024-03-28 10:20:51 +13:00
2025-07-17 22:40:28 +12:00
2026-04-07 22:29:55 +00:00
2025-12-08 14:37:45 -05:00
2026-04-09 11:28:48 +12:00
2025-07-17 22:40:28 +12:00
S
Description
ESPHome is a system to control your ESP8266/ESP32 by simple yet powerful configuration files and control them remotely through Home Automation systems.
Readme Multiple Licenses
585 MiB
Languages
C++ 56.1%
Python 43.3%
C 0.3%
JavaScript 0.2%