mirror of
https://github.com/esphome/esphome.git
synced 2026-01-20 09:59:11 -07:00
Compare commits
1 Commits
lazy_callb
...
component_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4293f8fe89 |
@@ -47,18 +47,21 @@ struct ComponentPriorityOverride {
|
||||
};
|
||||
|
||||
// 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::unique_ptr<std::vector<ComponentErrorMessage>> component_error_messages;
|
||||
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::unique_ptr<std::vector<ComponentPriorityOverride>> setup_priority_overrides;
|
||||
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
|
||||
void store_component_error_message(const Component *component, const char *message, bool is_flash_ptr) {
|
||||
// Lazy allocate the error messages vector if needed
|
||||
if (!component_error_messages) {
|
||||
component_error_messages = std::make_unique<std::vector<ComponentErrorMessage>>();
|
||||
component_error_messages = new std::vector<ComponentErrorMessage>();
|
||||
}
|
||||
// Check if this component already has an error message
|
||||
for (auto &entry : *component_error_messages) {
|
||||
@@ -467,7 +470,7 @@ float Component::get_actual_setup_priority() const {
|
||||
void Component::set_setup_priority(float priority) {
|
||||
// Lazy allocate the vector if needed
|
||||
if (!setup_priority_overrides) {
|
||||
setup_priority_overrides = std::make_unique<std::vector<ComponentPriorityOverride>>();
|
||||
setup_priority_overrides = new std::vector<ComponentPriorityOverride>();
|
||||
// Reserve some space to avoid reallocations (most configs have < 10 overrides)
|
||||
setup_priority_overrides->reserve(10);
|
||||
}
|
||||
@@ -553,7 +556,8 @@ WarnIfComponentBlockingGuard::~WarnIfComponentBlockingGuard() {}
|
||||
|
||||
void clear_setup_priority_overrides() {
|
||||
// Free the setup priority map completely
|
||||
setup_priority_overrides.reset();
|
||||
delete setup_priority_overrides;
|
||||
setup_priority_overrides = nullptr;
|
||||
}
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
@@ -1343,30 +1343,16 @@ template<typename... X> class LazyCallbackManager;
|
||||
*
|
||||
* Memory overhead comparison (32-bit systems):
|
||||
* - CallbackManager: 12 bytes (empty std::vector)
|
||||
* - LazyCallbackManager: 4 bytes (nullptr pointer)
|
||||
*
|
||||
* Uses plain pointer instead of unique_ptr to avoid template instantiation overhead.
|
||||
* The class is explicitly non-copyable/non-movable for Rule of Five compliance.
|
||||
* - LazyCallbackManager: 4 bytes (nullptr unique_ptr)
|
||||
*
|
||||
* @tparam Ts The arguments for the callbacks, wrapped in void().
|
||||
*/
|
||||
template<typename... Ts> class LazyCallbackManager<void(Ts...)> {
|
||||
public:
|
||||
LazyCallbackManager() = default;
|
||||
/// Destructor - clean up allocated CallbackManager if any.
|
||||
/// In practice this never runs (entities live for device lifetime) but included for correctness.
|
||||
~LazyCallbackManager() { delete this->callbacks_; }
|
||||
|
||||
// Non-copyable and non-movable (entities are never copied or moved)
|
||||
LazyCallbackManager(const LazyCallbackManager &) = delete;
|
||||
LazyCallbackManager &operator=(const LazyCallbackManager &) = delete;
|
||||
LazyCallbackManager(LazyCallbackManager &&) = delete;
|
||||
LazyCallbackManager &operator=(LazyCallbackManager &&) = delete;
|
||||
|
||||
/// Add a callback to the list. Allocates the underlying CallbackManager on first use.
|
||||
void add(std::function<void(Ts...)> &&callback) {
|
||||
if (!this->callbacks_) {
|
||||
this->callbacks_ = new CallbackManager<void(Ts...)>();
|
||||
this->callbacks_ = make_unique<CallbackManager<void(Ts...)>>();
|
||||
}
|
||||
this->callbacks_->add(std::move(callback));
|
||||
}
|
||||
@@ -1388,7 +1374,7 @@ template<typename... Ts> class LazyCallbackManager<void(Ts...)> {
|
||||
void operator()(Ts... args) { this->call(args...); }
|
||||
|
||||
protected:
|
||||
CallbackManager<void(Ts...)> *callbacks_{nullptr};
|
||||
std::unique_ptr<CallbackManager<void(Ts...)>> callbacks_;
|
||||
};
|
||||
|
||||
/// Helper class to deduplicate items in a series of values.
|
||||
|
||||
Reference in New Issue
Block a user