[api] Optimize single vector writes to use write() instead of writev() - #10193
Merged
Conversation
Contributor
|
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#10193
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 #10193 +/- ##
=======================================
Coverage 60.21% 60.21%
=======================================
Files 51 51
Lines 10378 10378
Branches 1382 1382
=======================================
Hits 6249 6249
Misses 3773 3773
Partials 356 356 β View full report in Codecov by Sentry. π New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes the API frame helper to use write() instead of writev() when sending single I/O vectors, reducing CPU overhead in the lwIP networking stack. The optimization specifically targets plaintext API connections which typically send single iovecs but were unnecessarily using the more complex writev() path.
- Adds conditional logic to detect single-vector writes and use
write()directly - Maintains backward compatibility by falling back to
writev()for multi-vector operations - Targets performance improvement for Bluetooth proxy and general API message sending
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 API frame helper to use
write()instead ofwritev()when sending a single I/O vector, avoiding unnecessary overhead in the lwIP networking stack.When analyzing the ESPHome API send path for Bluetooth proxy performance, I found that plaintext API connections always use a single iovec, but still call
writev(). The lwIP implementation has no fast path for single vectors - it always goes through full vector processing vialwip_sendmsg(). This change detects single-vector writes and callswrite()directly, reducing CPU overhead for every API message.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 required - this is an internal optimizationChecklist:
tests/folder).If user exposed functionality or configuration variables are added/changed: