[api] Extract cold code from APIServer::loop() hot path#13902
Conversation
# Conflicts: # esphome/components/api/api_connection.cpp
|
👋 Hi there! This PR modifies 2 file(s) with codeowners. @esphome/core - As codeowner(s) of the affected files, your review would be appreciated! 🙏 Note: Automatic review request may have failed, but you're still welcome to review. |
|
To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file: external_components:
- source: github://pr#13902
components: [api]
refresh: 1h(Added by the PR bot) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #13902 +/- ##
==========================================
- Coverage 74.10% 74.07% -0.03%
==========================================
Files 55 55
Lines 11587 11587
Branches 1577 1577
==========================================
- Hits 8586 8583 -3
- Misses 2599 2601 +2
- Partials 402 403 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Memory Impact AnalysisComponents:
📊 Component Memory Breakdown
🔍 Symbol-Level Changes (click to expand)Changed Symbols
New Symbols (top 15)
This analysis runs automatically when components change. Memory usage is measured from a representative test configuration. |
…ccept # Conflicts: # esphome/components/api/api_server.cpp
There was a problem hiding this comment.
Pull request overview
This PR refactors APIServer::loop() to move rarely-executed connection-management logic out of the steady-state hot path, reducing instruction-cache pressure during frequent loop iterations in the API server component.
Changes:
- Extracted the incoming-connection accept loop into
APIServer::accept_new_connections_()(markednoinline). - Extracted disconnected-client cleanup into
APIServer::remove_client_()(markednoinline) while preserving swap-and-pop behavior. - Simplified the main
loop()flow to focus on readiness checks and per-clientloop()calls.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| esphome/components/api/api_server.h | Declares two new noinline helper methods for accept/remove paths. |
| esphome/components/api/api_server.cpp | Moves cold accept/removal code out of APIServer::loop() into the new helpers. |
|
thanks |
What does this implement/fix?
Extract cold code from
APIServer::loop()into separate noinline helpers to reduce icache pressure in the hot path.1. Extract
accept_new_connections_():The socket accept loop (accept, getpeername,
new APIConnection, emplace_back, status_clear_warning) only runs when new clients connect, which is rare compared to ~7000 loop iterations per minute.2. Extract
remove_client_():The client removal path (unregister actions, close socket, swap-and-pop, status_set_warning, disconnect trigger) only runs when a client disconnects.
After extraction,
loop()hot path is: socket ready check → client empty check → is_connected check → iterate clients callingloop().Requires #13540
Types of changes
Related issue or feature (if applicable):
Pull request in esphome-docs with documentation (if applicable):
Test Environment
Example entry for
config.yaml:# No configuration changes - internal optimization onlyChecklist:
tests/folder).