mirror of
https://github.com/esphome/esphome.git
synced 2026-09-02 02:56:01 +00:00
250d28f69c20120b89e44112598076ca6fd862e7
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)
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%
