8000
Skip to content

[api] Optimize single vector writes to use write() instead of writev() - #10193

Merged
kbx81 merged 1 commit into
devfrom
api_writev
Aug 11, 2025
Merged

[api] Optimize single vector writes to use write() instead of writev()#10193
kbx81 merged 1 commit into
devfrom
api_writev

Conversation

@bdraco
@bdraco bdraco commented Aug 11, 2025
Copy link
Copy Markdown
Member

What does this implement/fix?

This PR optimizes the API frame helper to use write() instead of writev() 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 via lwip_sendmsg(). This change detects single-vector writes and calls write() directly, reducing CPU overhead for every API message.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable): N/A

Pull request in esphome-docs with documentation (if applicable): N/A

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • nRF52840

Example entry for config.yaml:

# No configuration changes required - this is an internal optimization

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@github-actions
Copy link
Copy Markdown
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-commenter
codecov-commenter commented Aug 11, 2025
Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 60.21%. Comparing base (45b88f2) to head (0441521).

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.
πŸ“’ Have feedback on the report? Share it here.

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco
bdraco marked this pull request as ready for review August 11, 2025 20:08
Copilot AI review requested due to automatic review settings August 11, 2025 20:08
@bdraco
bdraco requested a review from a team as a code owner August 11, 2025 20:08
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread esphome/components/api/api_frame_helper.cpp
@kbx81 kbx81 left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! πŸ‘πŸ»

@kbx81
kbx81 merged commit 9aa2195 into dev Aug 11, 2025
36 checks passed
@kbx81
kbx81 deleted the api_writev branch August 11, 2025 20:41
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0