[core] Restore COMPONENT_STATE_LOOP_DONE check in calculate_looping_components - #9832
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a performance regression where the logger component consumes excessive CPU cycles by restoring the COMPONENT_STATE_LOOP_DONE check that was removed in PR #9820. The issue occurs when components call disable_loop() during initialization, but are still added to the active looping components list, causing unnecessary loop execution.
- Restores state checking in
calculate_looping_components_()to respect components inLOOP_DONEstate - Adds separate handling for components that are already
LOOP_DONEduring initialization - Maintains the early initialization benefits from the original PR while fixing the CPU performance issue
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## dev #9832 +/- ##
==========================================
- Coverage 59.96% 59.92% -0.04%
==========================================
Files 51 51
Lines 10333 10333
Branches 1387 1387
==========================================
- Hits 6196 6192 -4
- Misses 3774 3777 +3
- Partials 363 364 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
15 tasks
jesserockz
approved these changes
Jul 23, 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 partially reverts PR #9820 to fix a performance regression where the logger component consumes excessive CPU cycles by running its loop continuously even when the buffer is empty.
The original PR removed the
COMPONENT_STATE_LOOP_DONEcheck when building the looping components list, which broke components that calldisable_loop()during initialization (before setup runs). The logger component specifically callsdisable_loop()ininit_log_buffer()to save CPU cycles when its buffer is empty, but this optimization was inadvertently broken.This fix restores the state check while maintaining the early initialization of
looping_components_that was the main goal of PR #9820. While this reverts the 76-byte flash savings from the original PR, the performance impact of excessive CPU usage far outweighs the minimal flash benefit.Types of changes
Related issue or feature (if applicable):
looping_components_before setup blocking phase #9820Pull 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:
Additional context
The runtime statistics before this fix show the logger consuming excessive resources:
This represents 6920 calls in 60 seconds (115 calls/second) totaling 4.4 seconds of CPU time out of 60 seconds.
The issue occurs because:
init_log_buffer()callsdisable_loop()to set state toCOMPONENT_STATE_LOOP_DONEcalculate_looping_components_()ignores this state and adds the logger to active componentsloop()runs continuously even with an empty bufferThis fix restores the original behavior where components in
COMPONENT_STATE_LOOP_DONEare properly placed in the inactive section of the looping components list.Trade-off: This restores the second loop that was removed for a 76-byte flash optimization, but the excessive CPU usage makes that optimization counterproductive.