From c89bc0bfd7559196b7b8e0042636b695c35cf3b9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 2 Aug 2025 12:02:30 -1000 Subject: [PATCH] [core] Replace std::stable_sort with insertion sort to save 1.3KB flash --- esphome/core/application.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index 3ac17849dd..05fa85c112 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -69,8 +69,20 @@ void Application::setup() { if (component->can_proceed()) continue; - std::stable_sort(this->components_.begin(), this->components_.begin() + i + 1, - [](Component *a, Component *b) { return a->get_loop_priority() > b->get_loop_priority(); }); + // Using insertion sort instead of std::stable_sort saves ~1.3KB of flash + // by avoiding std::rotate, std::stable_sort, and lambda template instantiations. + // Insertion sort is efficient for small arrays and maintains stability + for (int32_t j = 1; j <= static_cast(i); j++) { + Component *key = this->components_[j]; + float key_priority = key->get_loop_priority(); + int32_t k = j - 1; + + while (k >= 0 && this->components_[k]->get_loop_priority() < key_priority) { + this->components_[k + 1] = this->components_[k]; + k--; + } + this->components_[k + 1] = key; + } do { uint8_t new_app_state = STATUS_LED_WARNING;