[text] Avoid string copies in callbacks by passing const ref - #12504
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #12504 +/- ##
=======================================
Coverage 72.66% 72.66%
=======================================
Files 53 53
Lines 11193 11193
Branches 1517 1517
=======================================
Hits 8133 8133
Misses 2667 2667
Partials 393 393 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
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#12504
components: [text]
refresh: 1h(Added by the PR bot) |
Memory Impact AnalysisComponents:
📊 Component Memory Breakdown
🔍 Symbol-Level Changes (click to expand)Changed Symbols
This analysis runs automatically when components change. Memory usage is measured from a representative test configuration. |
There was a problem hiding this comment.
Pull request overview
This PR optimizes memory usage in the text entity component by changing callback signatures to pass strings by const reference instead of by value, eliminating unnecessary heap allocations on each callback invocation.
Key changes:
- Modified callback signature from
std::function<void(std::string)>tostd::function<void(const std::string &)> - Updated both the method signature and internal
CallbackManagertemplate parameter - This is a minor API breaking change, though lambda usage patterns (the common case) remain compatible
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| esphome/components/text/text.h | Updated callback signature in public API method and internal callback manager type |
| esphome/components/text/text.cpp | Updated callback method implementation signature to match header |
|
👋 Hi there! This PR modifies 2 file(s) with codeowners. @mauritskorse - As codeowner(s) of the affected files, your review would be appreciated! 🙏 Note: Automatic review request may have failed, but you're still welcome to review. |
|
Thanks |
What does this implement/fix?
Reduces heap churn in text entity by changing callback signatures from
std::function<void(std::string)>tostd::function<void(const std::string &)>.Previously, every callback invocation caused a string copy because the callback signature passed by value. With this change, strings are passed by const reference, eliminating unnecessary heap allocations.
Before: Each
publish_state()call copied the string once per registered callback.After: Strings are passed by reference - no copies for callbacks.
No lifetime issues: The string is always valid during callback execution because:
this->statebefore callbacks runpublish_state()returnsThis is a minor API change. Lambdas (the common pattern) continue to work unchanged:
The only breaking case is explicitly-typed
std::functionvariables:This pattern is extremely rare in practice.
Follow-up to #12503 which applied the same optimization to text_sensor.
Types of changes
Related issue or feature (if applicable):
N/A
Pull request in esphome-docs with documentation (if applicable):
N/A - internal API change only
Test Environment
Example entry for
config.yaml:Checklist:
tests/folder). Existing integration tests intests/integration/cover text callbacks.If user exposed functionality or configuration variables are added/changed: