From edf19b8dd444b7a678bfcd407b0fab9ce13dc493 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 30 Nov 2025 22:49:23 -0600 Subject: [PATCH] [core] Use StringRef for get_comment and get_compilation_time to avoid allocations --- esphome/components/wifi/wifi_component.cpp | 2 +- esphome/core/helpers.h | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 2a19ba45f1..69d7cb489c 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -360,7 +360,7 @@ void WiFiComponent::start() { get_mac_address_pretty_into_buffer(mac_s)); this->last_connected_ = millis(); - uint32_t hash = this->has_sta() ? fnv1_hash(App.get_compilation_time_ref()) : 88491487UL; + uint32_t hash = this->has_sta() ? fnv1_hash(App.get_compilation_time_ref().c_str()) : 88491487UL; this->pref_ = global_preferences->make_preference(hash, true); #ifdef USE_WIFI_FAST_CONNECT diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 9bbc771fb8..83a12b9bf0 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -15,7 +15,6 @@ #include #include "esphome/core/optional.h" -#include "esphome/core/string_ref.h" #ifdef USE_ESP8266 #include @@ -48,6 +47,9 @@ namespace esphome { +// Forward declaration to avoid circular dependency with string_ref.h +class StringRef; + /// @name STL backports ///@{ @@ -378,7 +380,6 @@ uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc = 0, uint16_t p /// Calculate a FNV-1 hash of \p str. uint32_t fnv1_hash(const char *str); inline uint32_t fnv1_hash(const std::string &str) { return fnv1_hash(str.c_str()); } -inline uint32_t fnv1_hash(const StringRef &str) { return fnv1_hash(str.c_str()); } /// Return a random 32-bit unsigned integer. uint32_t random_uint32();