From e9e92afc9e6857af24bd55dbce0de452c76aca07 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Wed, 4 Dec 2019 16:03:37 +0100 Subject: [PATCH] Optimize application loop speed (#860) * Optimize application loop speed * Also check call_loop * Remove duplicate code * Fixes --- esphome/components/light/light_state.cpp | 2 -- esphome/core/application.cpp | 10 +++++++++- esphome/core/application.h | 3 +++ esphome/core/component.cpp | 10 ++++++++++ esphome/core/component.h | 2 ++ 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/esphome/components/light/light_state.cpp b/esphome/components/light/light_state.cpp index e96d64ad1f..0ffb603818 100644 --- a/esphome/components/light/light_state.cpp +++ b/esphome/components/light/light_state.cpp @@ -416,8 +416,6 @@ LightColorValues LightCall::validate_() { if (this->brightness_.has_value()) v.set_brightness(*this->brightness_); - if (this->brightness_.has_value()) - v.set_brightness(*this->brightness_); if (this->red_.has_value()) v.set_red(*this->red_); if (this->green_.has_value()) diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index 2600ace218..4ecb247ec3 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -57,13 +57,14 @@ void Application::setup() { ESP_LOGI(TAG, "setup() finished successfully!"); this->schedule_dump_config(); + this->calculate_looping_components_(); } void Application::loop() { uint32_t new_app_state = 0; const uint32_t start = millis(); this->scheduler.call(); - for (Component *component : this->components_) { + for (Component *component : this->looping_components_) { component->call(); new_app_state |= component->get_component_state(); this->app_state_ |= new_app_state; @@ -146,6 +147,13 @@ void Application::safe_reboot() { } } +void Application::calculate_looping_components_() { + for (auto *obj : this->components_) { + if (obj->has_overridden_loop()) + this->looping_components_.push_back(obj); + } +} + Application App; } // namespace esphome diff --git a/esphome/core/application.h b/esphome/core/application.h index 2014b082e9..3c293e6c8f 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -209,7 +209,10 @@ class Application { void register_component_(Component *comp); + void calculate_looping_components_(); + std::vector components_{}; + std::vector looping_components_{}; #ifdef USE_BINARY_SENSOR std::vector binary_sensors_{}; diff --git a/esphome/core/component.cpp b/esphome/core/component.cpp index 0547fcbdd5..26662b0061 100644 --- a/esphome/core/component.cpp +++ b/esphome/core/component.cpp @@ -138,6 +138,16 @@ float Component::get_actual_setup_priority() const { return this->setup_priority_override_; } void Component::set_setup_priority(float priority) { this->setup_priority_override_ = priority; } +bool Component::has_overridden_loop() const { +#ifdef CLANG_TIDY + bool loop_overridden = true; + bool call_loop_overridden = true; +#else + bool loop_overridden = (void *) (this->*(&Component::loop)) != (void *) (&Component::loop); + bool call_loop_overridden = (void *) (this->*(&Component::call_loop)) != (void *) (&Component::call_loop); +#endif + return loop_overridden || call_loop_overridden; +} PollingComponent::PollingComponent(uint32_t update_interval) : Component(), update_interval_(update_interval) {} diff --git a/esphome/core/component.h b/esphome/core/component.h index c8e05cc252..e3f9a51f25 100644 --- a/esphome/core/component.h +++ b/esphome/core/component.h @@ -126,6 +126,8 @@ class Component { void status_momentary_error(const std::string &name, uint32_t length = 5000); + bool has_overridden_loop() const; + protected: virtual void call_loop(); virtual void call_setup();