From 9a022baa06615441ff2a036a9bcfaae223bdf373 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 12 Apr 2026 19:55:30 -1000 Subject: [PATCH] [core] Optimize main loop with -O2 Add __attribute__((optimize("O2"))) to the main loop functions (loop_task on ESP32, codegen loop() on other platforms) so GCC inlines scheduler helpers and loop bookkeeping more aggressively. Under -Os, GCC outlines small functions (Scheduler::call helpers, millis conversions, etc.) that are called every loop iteration. With -O2, these get inlined into the loop body, reducing call overhead on the hottest code path in the firmware. ESP32: loop_task grows from 303 to 416 bytes (+113 bytes). ESP8266: no change (already fully inlined via ESPHOME_ALWAYS_INLINE). --- esphome/components/esp32/core.cpp | 2 +- esphome/components/host/core.cpp | 2 +- esphome/components/zephyr/core.cpp | 2 +- esphome/core/application.h | 2 +- esphome/writer.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/esphome/components/esp32/core.cpp b/esphome/components/esp32/core.cpp index add50dcf4d..1fa51f1d9e 100644 --- a/esphome/components/esp32/core.cpp +++ b/esphome/components/esp32/core.cpp @@ -65,7 +65,7 @@ static StaticTask_t loop_task_tcb; // NOLINT(cppcoreguidelines-avoid-non- static StackType_t loop_task_stack[ESPHOME_LOOP_TASK_STACK_SIZE]; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) -void loop_task(void *pv_params) { +void __attribute__((optimize("O2"))) loop_task(void *pv_params) { setup(); while (true) { App.loop(); diff --git a/esphome/components/host/core.cpp b/esphome/components/host/core.cpp index 0ade4274fe..596b46305f 100644 --- a/esphome/components/host/core.cpp +++ b/esphome/components/host/core.cpp @@ -77,7 +77,7 @@ uint32_t arch_get_cpu_freq_hz() { return 1000000000U; } void setup(); void loop(); -int main() { +int __attribute__((optimize("O2"))) main() { // Install signal handlers for graceful shutdown (flushes preferences to disk) std::signal(SIGINT, signal_handler); std::signal(SIGTERM, signal_handler); diff --git a/esphome/components/zephyr/core.cpp b/esphome/components/zephyr/core.cpp index a3b0471ebc..84dee374f5 100644 --- a/esphome/components/zephyr/core.cpp +++ b/esphome/components/zephyr/core.cpp @@ -95,7 +95,7 @@ void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parame void setup(); void loop(); -int main() { +int __attribute__((optimize("O2"))) main() { setup(); while (true) { loop(); diff --git a/esphome/core/application.h b/esphome/core/application.h index 6b2969b490..5b68146b2d 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -837,7 +837,7 @@ inline void ESPHOME_ALWAYS_INLINE Application::before_loop_tasks_(uint32_t loop_ this->in_loop_ = true; } -inline void ESPHOME_ALWAYS_INLINE Application::loop() { +inline void ESPHOME_ALWAYS_INLINE __attribute__((optimize("O2"))) Application::loop() { uint8_t new_app_state = 0; // Get the initial loop time at the start diff --git a/esphome/writer.py b/esphome/writer.py index 06a2230118..c2e0d08fa4 100644 --- a/esphome/writer.py +++ b/esphome/writer.py @@ -45,7 +45,7 @@ void setup() { App.setup(); } -void loop() { +void __attribute__((optimize("O2"))) loop() { App.loop(); } """,