[esp32] Move heap functions to flash, saving ~6KB - #12862
Conversation
This is the culmination of months of work to reduce heap churn throughout the ESPHome codebase. By systematically eliminating unnecessary dynamic allocations (StaticVector, FixedVector, const char* instead of std::string, pre-allocated buffers, etc.), heap functions are now called so infrequently that they can safely be moved from IRAM to flash. Enable CONFIG_HEAP_PLACE_FUNCTION_INTO_FLASH by default, which moves malloc/free/realloc from IRAM to flash. This is safe because: - Heap functions should never be called from ISRs - CONFIG_SPI_MASTER_ISR_IN_IRAM is not enabled - Audio/video use pre-allocated ring buffers, not dynamic allocation Measured results: +6,124 bytes of heap freed. Add heap_in_iram advanced option as an escape hatch for users who need heap functions in IRAM for specific use cases.
|
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#12862
components: [esp32]
refresh: 1h(Added by the PR bot) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #12862 +/- ##
=======================================
Coverage 73.44% 73.44%
=======================================
Files 53 53
Lines 11303 11303
Branches 1534 1534
=======================================
Hits 8302 8302
Misses 2602 2602
Partials 399 399 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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. |
|
👋 Hi there! This PR modifies 1 file(s) with codeowners. @esphome/core - 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. |
There was a problem hiding this comment.
Pull request overview
This PR optimizes ESP32 IRAM usage by moving heap functions (malloc, free, realloc) from IRAM to flash memory, freeing up approximately 6KB of heap memory. This optimization is made possible by extensive heap churn reduction work throughout the ESPHome codebase, which has minimized the frequency of heap allocations during normal operation. The change introduces a new heap_in_iram configuration option (defaulting to false) as an escape hatch for users who need heap functions in IRAM.
Key Changes
- Adds
CONFIG_HEAP_PLACE_FUNCTION_INTO_FLASHconfiguration to move heap functions from IRAM to flash by default - Introduces
heap_in_iramadvanced configuration option to allow users to revert to the old behavior if needed - Measured heap savings: 6,124 bytes (~6KB)
|
Thanks |
What does this implement/fix?
This is the culmination of months of work to reduce heap churn throughout the ESPHome codebase. By systematically eliminating unnecessary dynamic allocations, we've reached a point where heap functions are called so infrequently that they can safely be moved from IRAM to flash - unlocking ~6 KB of additional heap memory essentially for free.
Background
Over the past several months, we've made extensive optimizations to reduce heap churn:
StaticVectorandFixedVectorto replacestd::vectorwhere sizes are knownstd::stringwithconst char*for static stringsThe result: heap functions (malloc, free, realloc) are now primarily called during setup, not during normal operation. This makes the performance difference between IRAM and flash access negligible.
Even without the heap churn reduction work, the impact would be minimal - the overhead only occurs on cache misses, and with heavy heap usage the functions would likely stay cached anyway.
The Optimization
Enable
CONFIG_HEAP_PLACE_FUNCTION_INTO_FLASHby default, moving heap functions from IRAM to flash.This is safe because:
CONFIG_SPI_MASTER_ISR_IN_IRAMis not enabledMeasured results:
An
advanced: heap_in_iramescape hatch is provided for users who need heap functions in IRAM for specific use cases.Types of changes
Related issue or feature (if applicable):
Pull request in esphome-docs with documentation (if applicable):< 8000 /p>
Test Environment
Example entry for
config.yaml:Checklist:
tests/folder).If user exposed functionality or configuration variables are added/changed: