diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 33263bbdfe..514aabc249 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -161,6 +161,9 @@ struct EAPAuth { using bssid_t = std::array; +/// Initial reserve size for filtered scan results (typical: 1-3 matching networks per SSID) +static constexpr size_t WIFI_SCAN_RESULT_FILTERED_RESERVE = 8; + // Use std::vector for RP2040 (callback-based) and ESP32 (destructive scan API) // Use FixedVector for ESP8266 and LibreTiny where two-pass exact allocation is possible #if defined(USE_RP2040) || defined(USE_ESP32) diff --git a/esphome/components/wifi/wifi_component_esp_idf.cpp b/esphome/components/wifi/wifi_component_esp_idf.cpp index 5a7fbd35f0..cdc101b87c 100644 --- a/esphome/components/wifi/wifi_component_esp_idf.cpp +++ b/esphome/components/wifi/wifi_component_esp_idf.cpp @@ -833,7 +833,7 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) { if (needs_full) { this->scan_result_.reserve(number); } else { - this->scan_result_.reserve(8); // Typical: 1-3 matching networks + this->scan_result_.reserve(WIFI_SCAN_RESULT_FILTERED_RESERVE); } // Process one record at a time to avoid large buffer allocation