8000
Skip to content

[bluetooth_proxy] Optimize memory usage with fixed-size array and const string references - #10015

Merged
jesserockz merged 2 commits into
devfrom
bluetooth_connection_churn_array
Aug 5, 2025
Merged

[bluetooth_proxy] Optimize memory usage with fixed-size array and const string references#10015
jesserockz merged 2 commits into
devfrom
bluetooth_connection_churn_array

Conversation

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

What does this implement/fix?

This PR optimizes the bluetooth_proxy component to reduce both flash and RAM usage:

  1. Optimize string handling in logging: Changed address_str() method in BLEClientBase to return a const reference instead of by value, eliminating unnecessary string copies during frequent logging operations.

  2. Replace dynamic vector with fixed array: Converted the connections_ vector to a fixed-size std::array since all connection objects are created at compile time and the count never changes. This eliminates:

    • 12 bytes of RAM on 32-bit systems (vector's internal pointer, size, and capacity members)
    • Dynamic memory allocation for the vector's buffer
    • Heap fragmentation
    • Code for dynamic memory management

These optimizations result in:

  • 576 bytes of flash savings
  • 12+ bytes of RAM savings (12 bytes for vector overhead plus heap allocation overhead)

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):

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

  • N/A (no user-visible changes)

Test Environment

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

Example entry for config.yaml:

# No configuration changes required - internal optimization only
bluetooth_proxy:
  active: true

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
github-actions Bot commented Aug 2, 2025
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#10015
    components: [bluetooth_proxy, esp32_ble_client]
    refresh: 1h

(Added by the PR bot)

@codecov-commenter
codecov-commenter commented Aug 2, 2025
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.19%. Comparing base (4f58e1c) to head (a8f4b5c).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev   #10015   +/-   ##
=======================================
  Coverage   60.19%   60.19%           
=======================================
  Files          51       51           
  Lines       10305    10305           
  Branches     1368     1368           
=======================================
  Hits         6203     6203           
  Misses       3745     3745           
  Partials      357      357           

☔ View full re 8000 port 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 force-pushed the bluetooth_connection_churn_array branch from 68ab351 to 20959c2 Compare August 2, 2025 06:32
@bdraco
bdraco marked this pull request as ready for review August 2, 2025 06:32
Copilot AI review requested due to automatic review settings August 2, 2025 06:33
@bdraco
bdraco requested a review from jesserockz as a code owner August 2, 2025 06:33
@github-actions
github-actions Bot commented Aug 2, 2025
Copy link
Copy Markdown
Contributor

👋 Hi there! I've automatically requested reviews from codeowners based on the files changed in this PR.

@jesserockz - You've been requested to review this PR as codeowner(s) of 3 file(s) that were modified. Thanks for your time! 🙏

This comment was marked as outdated.

@bdraco
bdraco requested a review from Copilot August 2, 2025 07:28
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 memory usage in the bluetooth_proxy component by implementing two key changes to reduce both flash and RAM consumption. The optimization converts dynamic memory structures to more efficient alternatives while maintaining the same functionality.

  • Changed address_str() method to return const string reference instead of by value to eliminate unnecessary string copies
  • Replaced dynamic std::vector with fixed-size std::array for connection storage since the connection count is determined at compile time

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
esphome/components/esp32_ble_client/ble_client_base.h Modified address_str() to return const reference for optimized string handling
esphome/components/bluetooth_proxy/bluetooth_proxy.h Replaced std::vector with std::array and added connection counter for fixed-size storage
esphome/components/bluetooth_proxy/bluetooth_proxy.cpp Updated connection iteration logic to use array indexing with connection counter

Comment thread esphome/components/bluetooth_proxy/bluetooth_proxy.h
@jesserockz
jesserockz merged commit 93b2844 into dev Aug 5, 2025
35 checks passed
@jesserockz
jesserockz deleted the bluetooth_connection_churn_array branch August 5, 2025 01:13
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 7, 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