Reduce DeviceValue heap usage with on-demand custom name

Store the per-entity custom_fullname as a heap-allocated string that is
only created when a custom name is actually set, instead of an inline
std::string on every DeviceValue. Saves ~20 bytes per entity (hundreds to
~1000 entities), reclaiming internal heap on no-PSRAM boards.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
proddy
2026-06-13 09:19:15 +02:00
parent b8a63aeac6
commit 5aadf7234f
4 changed files with 60 additions and 23 deletions

View File

@@ -1271,9 +1271,9 @@ void EMSdevice::setCustomizationEntity(const std::string & entity_id) {
// set the custom name if it has one, or clear it
if (has_custom_name) {
dv.custom_fullname = entity_id.substr(custom_name_pos + 1);
dv.set_custom_fullname(entity_id.substr(custom_name_pos + 1));
} else {
dv.custom_fullname = "";
dv.set_custom_fullname("");
}
auto min = dv.min;
@@ -1312,11 +1312,11 @@ void EMSdevice::getCustomizationEntities(std::vector<std::string> & entity_ids)
break;
}
}
if (!is_set && (mask || !dv.custom_fullname.empty())) {
if (dv.custom_fullname.empty()) {
if (!is_set && (mask || dv.has_custom_fullname())) {
if (!dv.has_custom_fullname()) {
entity_ids.push_back(Helpers::hextoa(mask, false) + entity_name);
} else {
entity_ids.push_back(Helpers::hextoa(mask, false) + entity_name + "|" + dv.custom_fullname);
entity_ids.push_back(Helpers::hextoa(mask, false) + entity_name + "|" + dv.custom_fullname());
}
}
}

View File

@@ -47,7 +47,6 @@ DeviceValue::DeviceValue(uint8_t device_type,
, numeric_operator(numeric_operator)
, short_name(short_name)
, fullname(fullname)
, custom_fullname(custom_fullname)
, uom(uom)
, has_cmd(has_cmd)
, min(min)
@@ -60,7 +59,12 @@ DeviceValue::DeviceValue(uint8_t device_type,
options_size = Helpers::count_items(options);
}
// set the min/max
// store the custom name on the heap, but only if one was actually provided
if (!custom_fullname.empty()) {
custom_fullname_ = std::make_unique<std::string>(custom_fullname);
}
// set the min/max (reads back the custom name set above)
set_custom_minmax();
/*
@@ -343,11 +347,12 @@ bool DeviceValue::get_min_max(int16_t & dv_set_min, uint32_t & dv_set_max) {
// extract custom min from custom_fullname
bool DeviceValue::get_custom_min(int16_t & val) {
auto min_pos = custom_fullname.find('>');
bool has_min = (min_pos != std::string::npos);
const auto & cf = custom_fullname();
auto min_pos = cf.find('>');
bool has_min = (min_pos != std::string::npos);
uint8_t fahrenheit = !EMSESP::system_.fahrenheit() ? 0 : (uom == DeviceValueUOM::DEGREES) ? 2 : (uom == DeviceValueUOM::DEGREES_R) ? 1 : 0;
if (has_min) {
int32_t v = Helpers::atoint(custom_fullname.substr(min_pos + 1).c_str());
int32_t v = Helpers::atoint(cf.substr(min_pos + 1).c_str());
if (fahrenheit) {
v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C
}
@@ -361,11 +366,12 @@ bool DeviceValue::get_custom_min(int16_t & val) {
// extract custom max from custom_fullname
bool DeviceValue::get_custom_max(uint32_t & val) {
auto max_pos = custom_fullname.find('<');
bool has_max = (max_pos != std::string::npos);
const auto & cf = custom_fullname();
auto max_pos = cf.find('<');
bool has_max = (max_pos != std::string::npos);
uint8_t fahrenheit = !EMSESP::system_.fahrenheit() ? 0 : (uom == DeviceValueUOM::DEGREES) ? 2 : (uom == DeviceValueUOM::DEGREES_R) ? 1 : 0;
if (has_max) {
int32_t v = Helpers::atoint(custom_fullname.substr(max_pos + 1).c_str());
int32_t v = Helpers::atoint(cf.substr(max_pos + 1).c_str());
if (fahrenheit) {
v = (v - (32 * (fahrenheit - 1))) / 1.8; // reset to °C
}
@@ -383,14 +389,32 @@ void DeviceValue::set_custom_minmax() {
get_custom_max(max);
}
std::string DeviceValue::get_custom_fullname() const {
auto min_pos = custom_fullname.find('>');
auto max_pos = custom_fullname.find('<');
auto minmax_pos = min_pos < max_pos ? min_pos : max_pos;
if (minmax_pos != std::string::npos) {
return custom_fullname.substr(0, minmax_pos);
// raw stored custom name (empty string if none was set)
const std::string & DeviceValue::custom_fullname() const {
static const std::string empty_string;
return custom_fullname_ ? *custom_fullname_ : empty_string;
}
// set or clear the custom name, only allocating heap when there's actually a name
void DeviceValue::set_custom_fullname(const std::string & name) {
if (name.empty()) {
custom_fullname_.reset();
} else if (custom_fullname_) {
*custom_fullname_ = name;
} else {
custom_fullname_ = std::make_unique<std::string>(name);
}
return custom_fullname;
}
std::string DeviceValue::get_custom_fullname() const {
const auto & cf = custom_fullname();
auto min_pos = cf.find('>');
auto max_pos = cf.find('<');
auto minmax_pos = min_pos < max_pos ? min_pos : max_pos;
if (minmax_pos != std::string::npos) {
return cf.substr(0, minmax_pos);
}
return cf;
}
// returns the translated fullname or the custom fullname (if provided)

View File

@@ -23,6 +23,8 @@
#include <Arduino.h>
#include <ArduinoJson.h>
#include <memory>
#include "helpers.h" // for conversions
#include "default_settings.h" // for enum types
@@ -177,7 +179,6 @@ class DeviceValue {
int8_t numeric_operator;
const char * const short_name; // used in MQTT and API
const char * const * fullname; // used in Web and Console, is translated
std::string custom_fullname; // optional, from customization
uint8_t uom; // DeviceValueUOM::*
bool has_cmd; // true if there is a Console/MQTT command which matches the short_name
int16_t min; // min range
@@ -214,6 +215,13 @@ class DeviceValue {
std::string get_fullname() const;
static std::string get_name(const std::string & entity);
// raw stored custom name (including any >min<max suffix), empty if none. Stored on heap only when set.
const std::string & custom_fullname() const;
void set_custom_fullname(const std::string & name);
bool has_custom_fullname() const {
return (bool)custom_fullname_;
}
// dv state flags
void add_state(uint8_t s) {
state |= s;
@@ -232,6 +240,11 @@ class DeviceValue {
static const char * const * DeviceValueTAG_s[];
static const char * const DeviceValueTAG_mqtt[];
static uint8_t NUM_TAGS; // # tags
private:
// optional custom name from customization. Allocated on heap only when actually set,
// so unnamed entities (the vast majority) don't pay for an inline std::string.
std::unique_ptr<std::string> custom_fullname_;
};
}; // namespace emsesp

View File

@@ -491,8 +491,8 @@ void WebCustomizationService::load_test_data() {
// find the device value and set the mask and custom name to match the above fake data
for (auto & dv : emsdevice->devicevalues_) {
if (strcmp(dv.short_name, "heatingactive") == 0) {
dv.state = DeviceValueState::DV_FAVORITE; // set as favorite
dv.custom_fullname = "is my heating on?";
dv.state = DeviceValueState::DV_FAVORITE; // set as favorite
dv.set_custom_fullname("is my heating on?");
} else if (strcmp(dv.short_name, "tapwateractive") == 0) {
dv.state = DeviceValueState::DV_FAVORITE; // set as favorite
} else if (strcmp(dv.short_name, "selflowtemp") == 0) {