[core] Convert components, devices, and areas vectors to static allocation - #10020
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #10020 +/- ##
=======================================
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 report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
bdraco
commented
Aug 2, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR converts the components_, devices_, and areas_ vectors in the Application class from std::vector to StaticVector for compile-time memory allocation. This optimization eliminates heap allocations and fragmentation by determining the required sizes from YAML configuration at compile time.
- Replaces dynamic vectors with static allocation based on YAML configuration counts
- Adds compile-time defines for component, device, and area counts in config generation
- Enhances StaticVector with additional STL-compatible methods (empty, reverse iterators)
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| esphome/core/helpers.h | Adds missing STL methods to StaticVector (empty(), reverse iterators) |
| esphome/core/defines.h | Adds default count definitions for static analysis tools |
| esphome/core/config.py | Replaces reserve calls with compile-time count definitions |
| esphome/core/application.h | Converts vectors to StaticVector and reorganizes member layout |
This was referenced Aug 2, 2025
jesserockz
approved these changes
Aug 4, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this implement/fix?
This PR extends the static allocation optimization from #10018 to the
components_,devices_, andareas_vectors in the Application class. These vectors are now allocated with a fixed size at compile time based on the actual counts from the YAML configuration, eliminating heap allocations and fragmentation.Key improvements:
std::vectortoStaticVectorfor deterministic memory usageMemory impact:
Types of changes
Related issue or feature (if applicable):
Pull 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:
Implementation Details
Changes Made:
Added compile-time defines in
core/config.py:ESPHOME_COMPONENT_COUNT- based onlen(CORE.component_ids)ESPHOME_DEVICE_COUNT- based on devices in YAMLESPHOME_AREA_COUNT- based on areas in YAMLEnhanced StaticVector in
core/helpers.h:empty()methodrbegin(),rend())<iterator>include forstd::reverse_iteratorConverted vectors in
core/application.h:std::vector<Component *>→StaticVector<Component *, ESPHOME_COMPONENT_COUNT>std::vector<Device *>→StaticVector<Device *, ESPHOME_DEVICE_COUNT>std::vector<Area *>→StaticVector<Area *, ESPHOME_AREA_COUNT>Updated member ordering in
application.h:Added default counts in
core/defines.hfor static analysis:ESPHOME_COMPONENT_COUNT 50ESPHOME_DEVICE_COUNT 10ESPHOME_AREA_COUNT 10Testing Results:
Small Device (ESP32-IDF):
Large Device (ESP32-IDF):
The optimization successfully reduces flash usage and eliminates heap fragmentation while maintaining compatibility with all existing configurations.