From d670bc2b0768a8034a37545db675df3c19d7a577 Mon Sep 17 00:00:00 2001 From: proddy Date: Tue, 19 May 2026 17:45:48 +0200 Subject: [PATCH] optimize enum to avoid per-call adding enums to heap --- src/core/emsdevice.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/core/emsdevice.h b/src/core/emsdevice.h index 3f6b7c7c3..d7fed3cb5 100644 --- a/src/core/emsdevice.h +++ b/src/core/emsdevice.h @@ -26,6 +26,7 @@ #include "emsdevicevalue.h" #include +#include #include namespace emsesp { @@ -223,16 +224,23 @@ class EMSdevice { } } - void has_enumupdate(const std::shared_ptr & telegram, uint8_t & value, const uint8_t index, const std::vector & maskIn) { - uint8_t val = value < maskIn.size() ? maskIn[value] : EMS_VALUE_UINT8_NOTSET; + // maskIn is taken as a std::initializer_list so brace-list call sites + // like has_enumupdate(t, v, idx, {0,5,1,2,4}) avoid the per-call + // heap allocation of a temporary std::vector. The backing + // array of an initializer_list of integral constants is placed in + // static storage or on the stack — never on the heap. + void has_enumupdate(const std::shared_ptr & telegram, uint8_t & value, const uint8_t index, std::initializer_list maskIn) { + uint8_t val = value < maskIn.size() ? *(maskIn.begin() + value) : EMS_VALUE_UINT8_NOTSET; if (telegram->read_value(val, index)) { - for (uint8_t i = 0; i < maskIn.size(); i++) { - if (val == maskIn[i]) { + uint8_t i = 0; + for (auto m : maskIn) { + if (val == m) { value = i; has_update_ = true; publish_value((void *)&value); return; } + ++i; } } }