[core] Use StringRef for get_comment and get_compilation_time to avoid allocations

This commit is contained in:
J. Nick Koston
2025-11-30 22:42:20 -06:00
parent 4335fcdb72
commit 4dbe0dab51
9 changed files with 10 additions and 7 deletions
+1 -1
View File
@@ -154,7 +154,7 @@ bool MQTTComponent::send_discovery_() {
device_info[MQTT_DEVICE_MANUFACTURER] =
model == nullptr ? ESPHOME_PROJECT_NAME : std::string(ESPHOME_PROJECT_NAME, model - ESPHOME_PROJECT_NAME);
#else
device_info[MQTT_DEVICE_SW_VERSION] = ESPHOME_VERSION " (" + App.get_compilation_time() + ")";
device_info[MQTT_DEVICE_SW_VERSION] = ESPHOME_VERSION " (" + App.get_compilation_time_ref() + ")";
device_info[MQTT_DEVICE_MODEL] = ESPHOME_BOARD;
#if defined(USE_ESP8266) || defined(USE_ESP32)
device_info[MQTT_DEVICE_MANUFACTURER] = "Espressif";
+1 -1
View File
@@ -157,7 +157,7 @@ void SEN5XComponent::setup() {
// Hash with compilation time and serial number
// This ensures the baseline storage is cleared after OTA
// Serial numbers are unique to each sensor, so mulitple sensors can be used without conflict
uint32_t hash = fnv1_hash(App.get_compilation_time() + std::to_string(combined_serial));
uint32_t hash = fnv1_hash(App.get_compilation_time_ref() + std::to_string(combined_serial).c_str());
this->pref_ = global_preferences->make_preference<Sen5xBaselines>(hash, true);
if (this->pref_.load(&this->voc_baselines_storage_)) {
+1 -1
View File
@@ -75,7 +75,7 @@ void SGP30Component::setup() {
// Hash with compilation time and serial number
// This ensures the baseline storage is cleared after OTA
// Serial numbers are unique to each sensor, so mulitple sensors can be used without conflict
uint32_t hash = fnv1_hash(App.get_compilation_time() + std::to_string(this->serial_number_));
uint32_t hash = fnv1_hash(App.get_compilation_time_ref() + std::to_string(this->serial_number_).c_str());
this->pref_ = global_preferences->make_preference<SGP30Baselines>(hash, true);
if (this->store_baseline_ && this->pref_.load(&this->baselines_storage_)) {
+1 -1
View File
@@ -59,7 +59,7 @@ void SGP4xComponent::setup() {
// Hash with compilation time and serial number
// This ensures the baseline storage is cleared after OTA
// Serial numbers are unique to each sensor, so mulitple sensors can be used without conflict
uint32_t hash = fnv1_hash(App.get_compilation_time() + std::to_string(this->serial_number_));
uint32_t hash = fnv1_hash(App.get_compilation_time_ref() + std::to_string(this->serial_number_).c_str());
this->pref_ = global_preferences->make_preference<SGP4xBaselines>(hash, true);
if (this->pref_.load(&this->voc_baselines_storage_)) {
@@ -13,7 +13,7 @@ void VersionTextSensor::setup() {
if (this->hide_timestamp_) {
this->publish_state(ESPHOME_VERSION);
} else {
this->publish_state(str_sprintf(ESPHOME_VERSION " %s", App.get_compilation_time().c_str()));
this->publish_state(str_sprintf(ESPHOME_VERSION " %s", App.get_compilation_time_ref().c_str()));
}
}
float VersionTextSensor::get_setup_priority() const { return setup_priority::DATA; }
+1 -1
View File
@@ -283,7 +283,7 @@ std::string WebServer::get_config_json() {
JsonObject root = builder.root();
root["title"] = App.get_friendly_name().empty() ? App.get_name() : App.get_friendly_name();
root["comment"] = App.get_comment();
root["comment"] = App.get_comment_ref();
#if defined(USE_WEBSERVER_OTA_DISABLED) || !defined(USE_WEBSERVER_OTA)
root["ota"] = false; // Note: USE_WEBSERVER_OTA_DISABLED only affects web_server, not captive_portal
#else
+1 -1
View File
@@ -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()) : 88491487UL;
uint32_t hash = this->has_sta() ? fnv1_hash(App.get_compilation_time_ref()) : 88491487UL;
this->pref_ = global_preferences->make_preference<wifi::SavedWifiSettings>(hash, true);
#ifdef USE_WIFI_FAST_CONNECT
+2
View File
@@ -256,6 +256,8 @@ class Application {
/// Get the comment of this Application set by pre_setup().
std::string get_comment() const { return this->comment_; }
/// Get the comment as StringRef (avoids allocation)
StringRef get_comment_ref() const { return StringRef::from_maybe_nullptr(this->comment_); }
bool is_name_add_mac_suffix_enabled() const { return this->name_add_mac_suffix_; }
+1
View File
@@ -380,6 +380,7 @@ 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();