[sprinkler] Fix scheduler deprecation warnings and heap churn with FixedVector - #13251
Conversation
|
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#13251
components: [sprinkler]
refresh: 1h(Added by the PR bot) |
|
👋 Hi there! I've automatically requested reviews from codeowners based on the files changed in this PR. @kbx81 - You've been requested to review this PR as codeowner(s) of 2 file(s) that were modified. Thanks for your time! 🙏 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #13251 +/- ##
=======================================
Coverage 73.79% 73.79%
=======================================
Files 53 53
Lines 11329 11329
Branches 1538 1538
=======================================
Hits 8360 8360
Misses 2570 2570
Partials 399 399 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This pull request fixes deprecation warnings in the sprinkler component by migrating from the deprecated std::string overloads of set_timeout() and cancel_timeout() to the const char* overloads, which eliminates hidden heap allocations in the scheduler.
Changes:
- Migrated
timer_fromstd::vector<SprinklerTimer>toFixedVector<SprinklerTimer>to ensure the vector capacity never changes after initialization - Updated
start_timer_()andcancel_timer_()to use.c_str()on timer names when calling scheduler methods - Added include for
esphome/core/helpers.hto accessFixedVector
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| esphome/components/sprinkler/sprinkler.h | Added helpers.h include and changed timer_ from std::vector to FixedVector with explanatory comment |
| esphome/components/sprinkler/sprinkler.cpp | Initialized FixedVector with capacity 2 in constructor, and updated scheduler calls to use .c_str() on timer names |
Memory Impact AnalysisComponents:
📊 Component Memory Breakdown
🔍 Symbol-Level Changes (click to expand)Changed Symbols
New Symbols (top 15)
Removed Symbols (top 15)
This analysis runs automatically when components change. Memory usage is measured from a representative test configuration. |
| /// Valve control timers | ||
| std::vector<SprinklerTimer> timer_{}; | ||
| /// Valve control timers - FixedVector enforces that this can never grow beyond init() size | ||
| FixedVector<SprinklerTimer> timer_; |
There was a problem hiding this comment.
Could probably do the same for other_controllers_ below, as that size should always be known at compile time
There was a problem hiding this comment.
Err I guess StaticVector might be better for that case. haha
There was a problem hiding this comment.
I thought about StaticVector first, but won't work because it can't handle moving std::string. Its intentionally missing the move support.
|
thanks |
What does this implement/fix?
Fix deprecation warnings and eliminate hidden heap churn in sprinkler component by using
const char*overload ofset_timeout()andcancel_timeout()instead of the deprecatedstd::stringoverloads.The timer names are stored in
SprinklerTimerstructs withconst std::string namemembers. Since these are initialized once in the constructor and never modified, calling.c_str()on them is safe.Key change: Migrated
timer_fromstd::vector<SprinklerTimer>toFixedVector<SprinklerTimer>.While
std::vectorwould have worked (the vector was only populated in the constructor), usingFixedVectorenforces this invariant at the type level. This prevents future refactoring from accidentally addingpush_back()calls elsewhere that could invalidate the.c_str()pointers. The type system now guarantees the safety property we're relying on.Before: Each
set_timeout()andcancel_timeout()call caused heap allocation when the scheduler copied the string name.After: Zero heap allocation - the scheduler stores pointers to the persistent member strings.
FixedVectorensures these pointers remain valid.Types of changes
Related issue or feature (if applicable):
set_timeout(const std::string&, ...)andcancel_timeout(const std::string&, ...)in sprinklerPull request in esphome-docs with documentation (if applicable):
Test Environment
Example entry for
config.yaml:Checklist:
tests/folder).If user exposed functionality or configuration variables are added/changed: