Merge branch 'conditionally-compile-setup-priority-override' into integration

This commit is contained in:
J. Nick Koston
2026-02-18 13:29:56 -06:00
5 changed files with 19 additions and 7 deletions

View File

@@ -137,8 +137,10 @@ void Application::setup() {
ESP_LOGI(TAG, "setup() finished successfully!");
#ifdef USE_SETUP_PRIORITY_OVERRIDE
// Clear setup priority overrides to free memory
clear_setup_priority_overrides();
#endif
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
// Set up wake socket for waking main loop from tasks

View File

@@ -41,20 +41,23 @@ struct ComponentErrorMessage {
bool is_flash_ptr;
};
#ifdef USE_SETUP_PRIORITY_OVERRIDE
struct ComponentPriorityOverride {
const Component *component;
float priority;
};
// Setup priority overrides - freed after setup completes
// Using raw pointer instead of unique_ptr to avoid global constructor/destructor overhead
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::vector<ComponentPriorityOverride> *setup_priority_overrides = nullptr;
#endif
// Error messages for failed components
// Using raw pointer instead of unique_ptr to avoid global constructor/destructor overhead
// This is never freed as error messages persist for the lifetime of the device
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::vector<ComponentErrorMessage> *component_error_messages = nullptr;
// Setup priority overrides - freed after setup completes
// Using raw pointer instead of unique_ptr to avoid global constructor/destructor overhead
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::vector<ComponentPriorityOverride> *setup_priority_overrides = nullptr;
// Helper to store error messages - reduces duplication between deprecated and new API
// Remove before 2026.6.0 when deprecated const char* API is removed
@@ -461,6 +464,7 @@ void log_update_interval(const char *tag, PollingComponent *component) {
}
}
float Component::get_actual_setup_priority() const {
#ifdef USE_SETUP_PRIORITY_OVERRIDE
// Check if there's an override in the global vector
if (setup_priority_overrides) {
// Linear search is fine for small n (typically < 5 overrides)
@@ -470,14 +474,14 @@ float Component::get_actual_setup_priority() const {
}
}
}
#endif
return this->get_setup_priority();
}
#ifdef USE_SETUP_PRIORITY_OVERRIDE
void Component::set_setup_priority(float priority) {
// Lazy allocate the vector if needed
if (!setup_priority_overrides) {
setup_priority_overrides = new std::vector<ComponentPriorityOverride>();
// Reserve some space to avoid reallocations (most configs have < 10 overrides)
setup_priority_overrides->reserve(10);
}
// Check if this component already has an override
@@ -491,6 +495,7 @@ void Component::set_setup_priority(float priority) {
// Add new override
setup_priority_overrides->emplace_back(ComponentPriorityOverride{this, priority});
}
#endif
bool Component::has_overridden_loop() const {
#if defined(USE_HOST) || defined(CLANG_TIDY)
@@ -557,10 +562,12 @@ uint32_t WarnIfComponentBlockingGuard::finish() {
return curr_time;
}
#ifdef USE_SETUP_PRIORITY_OVERRIDE
void clear_setup_priority_overrides() {
// Free the setup priority map completely
delete setup_priority_overrides;
setup_priority_overrides = nullptr;
}
#endif
} // namespace esphome

View File

@@ -564,6 +564,7 @@ class WarnIfComponentBlockingGuard {
};
// Function to clear setup priority overrides after all components are set up
// Only has an implementation when USE_SETUP_PRIORITY_OVERRIDE is defined
void clear_setup_priority_overrides();
} // namespace esphome

View File

@@ -109,6 +109,7 @@
#define USE_SAFE_MODE_CALLBACK
#define USE_SELECT
#define USE_SENSOR
#define USE_SETUP_PRIORITY_OVERRIDE
#define USE_STATUS_LED
#define USE_STATUS_SENSOR
#define USE_SWITCH

View File

@@ -9,7 +9,7 @@ from esphome.const import (
)
from esphome.core import CORE, ID, coroutine
from esphome.coroutine import FakeAwaitable
from esphome.cpp_generator import LogStringLiteral, add, get_variable
from esphome.cpp_generator import LogStringLiteral, add, add_define, get_variable
from esphome.cpp_types import App
from esphome.types import ConfigFragmentType, ConfigType
from esphome.util import Registry, RegistryEntry
@@ -49,6 +49,7 @@ async def register_component(var, config):
)
CORE.component_ids.remove(id_)
if CONF_SETUP_PRIORITY in config:
add_define("USE_SETUP_PRIORITY_OVERRIDE")
add(var.set_setup_priority(config[CONF_SETUP_PRIORITY]))
if CONF_UPDATE_INTERVAL in config:
add(var.set_update_interval(config[CONF_UPDATE_INTERVAL]))