Optimize application loop speed (#860)

* Optimize application loop speed

* Also check call_loop

* Remove duplicate code

* Fixes
This commit is contained in:
Otto Winter 2019-12-04 16:03:37 +01:00 committed by GitHub
parent e86f2e993f
commit e9e92afc9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 3 deletions

View file

@ -416,8 +416,6 @@ LightColorValues LightCall::validate_() {
if (this->brightness_.has_value()) if (this->brightness_.has_value())
v.set_brightness(*this->brightness_); v.set_brightness(*this->brightness_);
if (this->brightness_.has_value())
v.set_brightness(*this->brightness_);
if (this->red_.has_value()) if (this->red_.has_value())
v.set_red(*this->red_); v.set_red(*this->red_);
if (this->green_.has_value()) if (this->green_.has_value())

View file

@ -57,13 +57,14 @@ void Application::setup() {
ESP_LOGI(TAG, "setup() finished successfully!"); ESP_LOGI(TAG, "setup() finished successfully!");
this->schedule_dump_config(); this->schedule_dump_config();
this->calculate_looping_components_();
} }
void Application::loop() { void Application::loop() {
uint32_t new_app_state = 0; uint32_t new_app_state = 0;
const uint32_t start = millis(); const uint32_t start = millis();
this->scheduler.call(); this->scheduler.call();
for (Component *component : this->components_) { for (Component *component : this->looping_components_) {
component->call(); component->call();
new_app_state |= component->get_component_state(); new_app_state |= component->get_component_state();
this->app_state_ |= new_app_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; Application App;
} // namespace esphome } // namespace esphome

View file

@ -209,7 +209,10 @@ class Application {
void register_component_(Component *comp); void register_component_(Component *comp);
void calculate_looping_components_();
std::vector<Component *> components_{}; std::vector<Component *> components_{};
std::vector<Component *> looping_components_{};
#ifdef USE_BINARY_SENSOR #ifdef USE_BINARY_SENSOR
std::vector<binary_sensor::BinarySensor *> binary_sensors_{}; std::vector<binary_sensor::BinarySensor *> binary_sensors_{};

View file

@ -138,6 +138,16 @@ float Component::get_actual_setup_priority() const {
return this->setup_priority_override_; return this->setup_priority_override_;
} }
void Component::set_setup_priority(float priority) { this->setup_priority_override_ = priority; } 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) {} PollingComponent::PollingComponent(uint32_t update_interval) : Component(), update_interval_(update_interval) {}

View file

@ -126,6 +126,8 @@ class Component {
void status_momentary_error(const std::string &name, uint32_t length = 5000); void status_momentary_error(const std::string &name, uint32_t length = 5000);
bool has_overridden_loop() const;
protected: protected:
virtual void call_loop(); virtual void call_loop();
virtual void call_setup(); virtual void call_setup();