From 06a81db88a9bc4e515a879cba4e68bfe80d20f83 Mon Sep 17 00:00:00 2001 From: proddy Date: Sat, 13 Jun 2026 16:47:14 +0200 Subject: [PATCH] minor heap optimizations --- src/core/command.h | 5 +++ src/core/emsdevice.cpp | 12 ++++--- src/core/emsdevice.h | 6 ++++ src/core/emsdevicevalue.cpp | 56 ++++++++++++++++++++--------- src/core/emsdevicevalue.h | 16 +++++++-- src/core/emsesp.cpp | 35 ++++++++++++++++++ src/core/emsesp.h | 12 +++++++ src/web/WebCustomizationService.cpp | 4 +-- 8 files changed, 121 insertions(+), 25 deletions(-) diff --git a/src/core/command.h b/src/core/command.h index 2644a29b4..f66111ecb 100644 --- a/src/core/command.h +++ b/src/core/command.h @@ -123,6 +123,11 @@ class Command { cmdfunctions_.reserve(num); } + // release any reserved-but-unused capacity once commands have settled + static void compact() { + cmdfunctions_.shrink_to_fit(); + } + static void show_all(uuid::console::Shell & shell); static Command::CmdFunction * find_command(const uint8_t device_type, const uint8_t device_id, const char * cmd, const uint8_t flag); static std::string tagged_cmd(const std::string & cmd, const uint8_t flag); diff --git a/src/core/emsdevice.cpp b/src/core/emsdevice.cpp index 41a83ecb8..45ce91ab9 100644 --- a/src/core/emsdevice.cpp +++ b/src/core/emsdevice.cpp @@ -559,6 +559,7 @@ void EMSdevice::show_mqtt_handlers(uuid::console::Shell & shell) const { // register a callback function for a specific telegram type void EMSdevice::register_telegram_type(const uint16_t telegram_type_id, const char * telegram_type_name, bool fetch, const process_function_p f, uint8_t length) { telegram_functions_.emplace_back(telegram_type_id, telegram_type_name, fetch, false, length, f); + EMSESP::mark_entities_changed(); } // add to device value library, also know now as a "device entity" @@ -675,6 +676,7 @@ void EMSdevice::add_device_value(int8_t tag, // to b // add the device entity devicevalues_.emplace_back( device_type_, tag, value_p, type, options, options_single, numeric_operator, short_name, fullname, custom_fullname, uom, has_cmd, min, max, state); + EMSESP::mark_entities_changed(); // add a new command if it has a function attached if (has_cmd) { @@ -1281,9 +1283,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; @@ -1322,11 +1324,11 @@ void EMSdevice::getCustomizationEntities(std::vector & 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()); } } } diff --git a/src/core/emsdevice.h b/src/core/emsdevice.h index 93a0595f9..ec136b938 100644 --- a/src/core/emsdevice.h +++ b/src/core/emsdevice.h @@ -550,6 +550,12 @@ class EMSdevice { telegram_functions_.reserve(elements); } + // release any reserved-but-unused capacity once the entity/telegram set has settled + void compact() { + devicevalues_.shrink_to_fit(); + telegram_functions_.shrink_to_fit(); + } + #if defined(EMSESP_STANDALONE) struct TelegramFunctionDump { uint16_t type_id_; diff --git a/src/core/emsdevicevalue.cpp b/src/core/emsdevicevalue.cpp index 8db02d339..3f2eca495 100644 --- a/src/core/emsdevicevalue.cpp +++ b/src/core/emsdevicevalue.cpp @@ -53,8 +53,7 @@ DeviceValue::DeviceValue(uint8_t device_type, , uom(uom) , has_cmd(has_cmd) , min(min) - , max(max) - , custom_fullname(custom_fullname) { + , max(max) { // calculate #options in options list if (options_single) { options_size = 1; @@ -62,7 +61,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(custom_fullname); + } + + // set the min/max (reads back the custom name set above) set_custom_minmax(); /* @@ -347,11 +351,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 } @@ -365,11 +370,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 } @@ -387,14 +393,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(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) diff --git a/src/core/emsdevicevalue.h b/src/core/emsdevicevalue.h index 0eeaf4f62..25ca1875b 100644 --- a/src/core/emsdevicevalue.h +++ b/src/core/emsdevicevalue.h @@ -23,6 +23,8 @@ #include #include +#include + #include "helpers.h" // for conversions #include "default_settings.h" // for enum types @@ -188,8 +190,6 @@ class DeviceValue { // wider numeric range fields int16_t min; // min range uint32_t max; // max range - // largest member last (cold path: only read during customization save/load and web display) - std::string custom_fullname; // optional, from customization DeviceValue(uint8_t device_type, // EMSdevice::DeviceType int8_t tag, // DeviceValueTAG::* @@ -219,6 +219,13 @@ class DeviceValue { std::string get_fullname() const; static std::string get_name(const std::string & entity); + // raw stored custom name (including any >min custom_fullname_; }; }; // namespace emsesp diff --git a/src/core/emsesp.cpp b/src/core/emsesp.cpp index 582e94fdd..feb747586 100644 --- a/src/core/emsesp.cpp +++ b/src/core/emsesp.cpp @@ -48,6 +48,9 @@ uint16_t EMSESP::wait_validate_ = 0; bool EMSESP::wait_km_ = false; uint32_t EMSESP::last_fetch_ = 0; +uint32_t EMSESP::last_entity_change_ = 0; +bool EMSESP::entity_compaction_pending_ = false; + AsyncWebServer webServer(80); #if defined(EMSESP_STANDALONE) @@ -176,6 +179,37 @@ void EMSESP::clear_all_devices() { // emsdevices.clear(); // remove entries, but doesn't delete actual devices } +// called from EMSdevice/Command whenever an entity or telegram handler is registered. +// Devices reserve their value/telegram vectors generously (to avoid realloc storms while +// heating circuits etc. are discovered incrementally), so once registration settles we +// reclaim the unused capacity - see compact_entities_if_stable(). +void EMSESP::mark_entities_changed() { + last_entity_change_ = uuid::get_uptime(); + entity_compaction_pending_ = true; +} + +// once the entity/telegram set has been stable for ENTITY_COMPACT_DELAY, shrink the +// per-device and command vectors to their actual size. Re-arms automatically if a new +// device/circuit appears later (which just costs a single realloc). +void EMSESP::compact_entities_if_stable() { + if (!entity_compaction_pending_) { + return; // nothing to do (cheap early-out on the hot path) + } + if ((uuid::get_uptime() - last_entity_change_) < ENTITY_COMPACT_DELAY) { + return; // still settling + } + + for (const auto & emsdevice : emsdevices) { + if (emsdevice) { + emsdevice->compact(); + } + } + Command::compact(); + + entity_compaction_pending_ = false; + LOG_DEBUG("Reclaimed unused entity vector capacity"); +} + // return total number of devices excluding the Controller uint8_t EMSESP::count_devices() { if (emsdevices.empty()) { @@ -1860,6 +1894,7 @@ void EMSESP::loop() { webModulesService.loop(); // loop through the external library modules webSchedulerService.loop(); // scheduler timing logic; command execution is offloaded to WebCommandService's worker task scheduled_fetch_values(); // force a query on the EMS devices to fetch latest data at a set interval (1 min) + compact_entities_if_stable(); // reclaim over-reserved entity vector capacity once device discovery settles } // check for GPIO Errors - this is called once when booting if (EMSESP::system_.systemStatus() == SYSTEM_STATUS::SYSTEM_STATUS_INVALID_GPIO) { diff --git a/src/core/emsesp.h b/src/core/emsesp.h index e59db86ae..c75c0fa12 100644 --- a/src/core/emsesp.h +++ b/src/core/emsesp.h @@ -239,6 +239,10 @@ class EMSESP { static void scan_devices(); static void clear_all_devices(); + // called whenever a device entity or telegram handler is registered, so we can + // later reclaim the (deliberately generous) reserved vector capacity once stable + static void mark_entities_changed(); + static std::vector, AllocatorPSRAM>> emsdevices; // services static Mqtt mqtt_; @@ -275,6 +279,9 @@ class EMSESP { static void publish_response(const std::shared_ptr & telegram); static void publish_all_loop(); + // one-time compaction of per-device/command vectors once registration has been stable + static void compact_entities_if_stable(); + void shell_prompt(); void start_serial_console(); @@ -303,6 +310,11 @@ class EMSESP { static bool wait_km_; static uint32_t last_fetch_; + // entity/telegram registration tracking, used to trigger a one-time vector compaction + static constexpr uint32_t ENTITY_COMPACT_DELAY = 60000; // ms of stability before compacting + static uint32_t last_entity_change_; // uptime (ms) of last registration + static bool entity_compaction_pending_; // true while a compaction is owed + // UUID stuff static constexpr auto & serial_console_ = Serial; static constexpr unsigned long SERIAL_CONSOLE_BAUD_RATE = 115200; diff --git a/src/web/WebCustomizationService.cpp b/src/web/WebCustomizationService.cpp index 0f4ad78dd..16086fa49 100644 --- a/src/web/WebCustomizationService.cpp +++ b/src/web/WebCustomizationService.cpp @@ -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) {