This commit is contained in:
proddy
2026-06-08 20:54:43 +02:00
parent 7d94dc7cf0
commit 8fb69826f9
3 changed files with 8 additions and 14 deletions

View File

@@ -919,7 +919,6 @@ std::string EMSESP::pretty_telegram(const std::shared_ptr<const Telegram> & tele
} }
} }
// Optimized: Use stack buffer and build string once to avoid multiple temporary allocations
char buf[250]; char buf[250];
if (telegram->operation == Telegram::Operation::RX_READ) { if (telegram->operation == Telegram::Operation::RX_READ) {
auto pos = snprintf(buf, auto pos = snprintf(buf,

View File

@@ -34,7 +34,6 @@ char * Helpers::hextoa(char * result, const uint8_t value) {
} }
// same as hextoa but uses to a hex std::string // same as hextoa but uses to a hex std::string
// Optimized: Avoid string concatenation to reduce temporary allocations
std::string Helpers::hextoa(const uint8_t value, bool prefix) { std::string Helpers::hextoa(const uint8_t value, bool prefix) {
if (prefix) { if (prefix) {
char buf[5]; // "0x" + 2 hex chars + null char buf[5]; // "0x" + 2 hex chars + null
@@ -60,7 +59,6 @@ char * Helpers::hextoa(char * result, const uint16_t value) {
} }
// same as above but to a hex string // same as above but to a hex string
// Optimized: Avoid string concatenation to reduce temporary allocations
std::string Helpers::hextoa(const uint16_t value, bool prefix) { std::string Helpers::hextoa(const uint16_t value, bool prefix) {
if (prefix) { if (prefix) {
char buf[7]; // "0x" + 4 hex chars + null char buf[7]; // "0x" + 4 hex chars + null
@@ -114,7 +112,6 @@ char * Helpers::ultostr(char * ptr, uint32_t value, const uint8_t base) {
// fast itoa returning a std::string // fast itoa returning a std::string
// http://www.strudel.org.uk/itoa/ // http://www.strudel.org.uk/itoa/
// Optimized: Use stack buffer to avoid heap allocation, then create string once
std::string Helpers::itoa(int16_t value) { std::string Helpers::itoa(int16_t value) {
// int16_t max: -32768 to 32767 = max 6 chars + null // int16_t max: -32768 to 32767 = max 6 chars + null
char buf[8]; char buf[8];
@@ -140,7 +137,7 @@ std::string Helpers::itoa(int16_t value) {
/* /*
* fast itoa * fast itoa
* written by Lukás Chmela, Released under GPLv3. http://www.strudel.org.uk/itoa/ version 0.4 * written by Lukás Chmela, Released under GPLv3. http://www.strudel.org.uk/itoa/ version 0.4
* optimized for ESP32 * optimized for ESP32 for EMS-ESP
*/ */
char * Helpers::itoa(int32_t value, char * result, const uint8_t base) { char * Helpers::itoa(int32_t value, char * result, const uint8_t base) {
// check that the base if valid // check that the base if valid

View File

@@ -27,7 +27,7 @@
namespace emsesp { namespace emsesp {
// find tokens - optimized to reduce string allocations // find tokens
std::deque<Token> exprToTokens(const std::string & expr) { std::deque<Token> exprToTokens(const std::string & expr) {
std::deque<Token> tokens; std::deque<Token> tokens;
@@ -231,7 +231,7 @@ std::deque<Token> exprToTokens(const std::string & expr) {
return tokens; return tokens;
} }
// sort tokens to RPN form - optimized for memory usage // sort tokens to RPN form
std::deque<Token> shuntingYard(const std::deque<Token> & tokens) { std::deque<Token> shuntingYard(const std::deque<Token> & tokens) {
std::deque<Token> queue; std::deque<Token> queue;
std::vector<Token> stack; std::vector<Token> stack;
@@ -347,7 +347,6 @@ bool isnum(const std::string & s) {
std::string commands(std::string & expr, bool quotes) { std::string commands(std::string & expr, bool quotes) {
auto expr_new = Helpers::toLower(expr); auto expr_new = Helpers::toLower(expr);
for (uint8_t device = 0; device < EMSdevice::DeviceType::UNKNOWN; device++) { for (uint8_t device = 0; device < EMSdevice::DeviceType::UNKNOWN; device++) {
// Optimized: build string with reserve to avoid temporary allocations
std::string d; std::string d;
d.reserve(32); // typical device name length + "/" d.reserve(32); // typical device name length + "/"
d = EMSdevice::device_type_2_device_name(device); d = EMSdevice::device_type_2_device_name(device);
@@ -374,7 +373,6 @@ std::string commands(std::string & expr, bool quotes) {
JsonDocument doc_in; JsonDocument doc_in;
JsonObject output = doc_out.to<JsonObject>(); JsonObject output = doc_out.to<JsonObject>();
JsonObject input = doc_in.to<JsonObject>(); JsonObject input = doc_in.to<JsonObject>();
// Optimized: use stack buffer for small strings to avoid heap allocation
char cmd_s[COMMAND_MAX_LENGTH + 5]; // "api/" prefix + cmd char cmd_s[COMMAND_MAX_LENGTH + 5]; // "api/" prefix + cmd
snprintf(cmd_s, sizeof(cmd_s), "api/%s", cmd); snprintf(cmd_s, sizeof(cmd_s), "api/%s", cmd);