Optimize API connection batch priority message handling to reduce flash usage - #9510
Merged
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## dev #9510 +/- ##
=======================================
Coverage 59.67% 59.67%
=======================================
Files 50 50
Lines 10268 10268
Branches 1379 1379
=======================================
Hits 6127 6127
Misses 3782 3782
Partials 359 359 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Hey there @OttoWinter, mind taking a look at this pull request as it has been labeled with an integration ( |
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes flash usage in APIConnection by avoiding costly vector shifts and preventing redundant pings during disconnect.
- Adds a check to skip keepalive pings when a disconnect is in progress.
- Replaces
vector::insertinDeferredBatch::add_item_frontwithemplace_back+swapto reduce flash usage.
Comments suppressed due to low confidence (2)
esphome/components/api/api_connection.cpp:189
- [nitpick] The flag name 'remove' is ambiguous; consider renaming it to something like 'disconnecting' or 'is_removing' to clearly indicate that it represents an ongoing disconnection process.
} else if (now - this->last_traffic_ > KEEPALIVE_TIMEOUT_MS && !this->flags_.remove) {
esphome/components/api/api_connection.cpp:1670
- Add unit tests for
DeferredBatch::add_item_frontto verify that inserting into an empty batch, a single-item batch, and multi-item batch correctly moves the high-priority message to the front.
items.emplace_back(entity, std::move(creator), message_type, estimated_size);
jesserockz
approved these changes
Jul 16, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this implement/fix?
This PR optimizes the flash memory usage of the API connection's
add_item_frontmethod by replacing the expensivevector::insert()operation with a more efficientemplace_back()+swap()approach.The change reduces flash usage by approximately 300 bytes by avoiding the need to shift all vector elements when inserting at the front (add_item_front stl vector ops generated a lot of hidden code). This is safe because:
Types of changes
Related issue or feature (if applicable): N/A
Pull request in esphome-docs with documentation (if applicable): N/A
Test Environment
Example entry for
config.yaml:# No configuration changes needed - this is an internal optimizationChecklist:
tests/folder).Additional Information
Flash usage measurements:
Technical details:
The
add_item_frontmethod is only used for high-priority messages (ping and disconnect) when the transmit buffer is full. Since we only have one high-priority message at a time and pings are blocked during disconnect, the simple swap approach is sufficient and much more efficient than shifting all vector elements.