8000
Skip to content

Optimize API connection batch priority message handling to reduce flash usage - #9510

Merged
jesserockz merged 1 commit into
devfrom
api_connection_batch_prio
Jul 16, 2025
Merged

Optimize API connection batch priority message handling to reduce flash usage#9510
jesserockz merged 1 commit into
devfrom
api_connection_batch_prio

Conversation

@bdraco
@bdraco bdraco commented Jul 15, 2025
Copy link
Copy Markdown
Member

What does this implement/fix?

This PR optimizes the flash memory usage of the API connection's add_item_front method by replacing the expensive vector::insert() operation with a more efficient emplace_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:

  • Only one high-priority message (ping or disconnect) exists at a time
  • We already prevent pings when disconnecting
  • The exact order within a batch doesn't affect delivery since we send the entire batch together

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

Example entry for config.yaml:

# No configuration changes needed - 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).

Additional Information

Flash usage measurements:

  • Before: 16,147 bytes
  • After: 15,844 bytes
  • Savings: 303 bytes

Technical details:

The add_item_front method 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.

@codecov-commenter
codecov-commenter commented Jul 15, 2025
Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 59.67%. Comparing base (84349b6) to head (9093566).
Report is 2 commits behind head on dev.

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.
📢 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 July 15, 2025 06:59
Copilot AI review requested due to automatic review settings July 15, 2025 06:59
@bdraco
bdraco requested a review from OttoWinter as a code owner July 15, 2025 06:59
@probot-esphome
Copy link
Copy Markdown

Hey there @OttoWinter, mind taking a look at this pull request as it has been labeled with an integration (api) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

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 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::insert in DeferredBatch::add_item_front with emplace_back + swap to 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_front to 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
jesserockz merged commit b648944 into dev Jul 16, 2025
31 checks passed
@jesserockz
jesserockz deleted the api_connection_batch_prio branch July 16, 2025 01:46
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 18, 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.

5 participants

0