[api] Avoid object_id string allocations for all entity info messages - #10260
Conversation
|
To use the changes in this PR: # Clone the repository:
git clone https://github.com/esphome/esphome
cd esphome
# Checkout the PR branch:
git fetch origin pull/10260/head:object_id_alloc
git checkout object_id_alloc
# Install the development version:
script/setup
# Activate the development version:
source venv/bin/activateNow you can run (Added by the PR bot) |
|
needs to be tested on a real esp32 since host platform doesn't support this path as it doesn't get the mac so I can't make an integration test |
|
tested on esp32 |
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes the API by eliminating temporary string allocations for object IDs when sending entity information to Home Assistant. The optimization adds a new method that returns a StringRef for static object IDs (the common case) while only falling back to allocation for dynamic object IDs when MAC suffix is enabled without own name.
- Adds a
get_object_id_ref_for_api_()method to EntityBase that returns StringRef for static object_ids - Updates API connection to use the new method to avoid allocations when possible
- Trades 80 bytes of flash for eliminating 10s-100s of heap allocations per Home Assistant reconnect
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| esphome/core/entity_base.h | Adds forward declaration for APIConnection friend access and declares the new optimization method |
| esphome/core/entity_base.cpp | Implements the new method and includes StringRef header, refactors existing logic |
| esphome/components/api/api_connection.h | Updates API connection to use the new method for avoiding string allocations |
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
What does this implement/fix?
This PR optimizes the API to avoid allocating temporary strings for object_id when sending entity information. Since object_id is sent for every entity on each Home Assistant reconnect (which can be 10s-100s of entities), eliminating these allocations is worth the 80 bytes of flash overhead.
The optimization adds a
get_object_id_ref_for_api_()method that returns a StringRef for static object_ids (the common case), only falling back to allocation for dynamic object_ids (when MAC suffix is enabled without own name).Types of changes
Related issue or feature (if applicable): N/A
Pull request in esphome-docs with documentation (if applicable): N/A (internal optimization, no user-facing changes)
Test Environment
Example entry for
config.yaml:# No configuration changes needed - this is an internal optimizationChecklist:
tests/folder).If user exposed functionality or configuration variables are added/changed:
Additional Context:
Performance Impact:
Why This Trade-off Makes Sense:
Trading 80 bytes of flash for avoiding 10s-100s of heap allocations per Home Assistant reconnect is a clear win because:
Implementation:
get_object_id_ref_for_api_()method to EntityBase_for_api_()pattern used elsewhere in the codebase