mirror of
https://github.com/esphome/esphome.git
synced 2026-09-11 23:37:34 +00:00
[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).
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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
|
||||
|
||||
+1
-1
@@ -45,7 +45,7 @@ void setup() {
|
||||
App.setup();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
void __attribute__((optimize("O2"))) loop() {
|
||||
App.loop();
|
||||
}
|
||||
""",
|
||||
|
||||
Reference in New Issue
Block a user