From 1d49013fc6e4315ea74c37bf61df6ab231609afd Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 19 Dec 2023 15:07:53 +0100 Subject: [PATCH] Reduce heap allocations by getting rid of temporary std::list and std::string --- components/esp_modem/CMakeLists.txt | 2 +- .../cxx_include/esp_modem_command_library.hpp | 6 ++-- .../command/src/esp_modem_modules.cpp | 4 +-- .../cxx_include/esp_modem_command_library.hpp | 6 ++-- .../esp_modem_command_library_17.hpp | 6 ++-- .../esp_modem_command_library_utils.hpp | 13 +++++--- .../include/cxx_include/esp_modem_dte.hpp | 4 +-- .../include/cxx_include/esp_modem_types.hpp | 4 +-- .../src/esp_modem_command_library.cpp | 33 ++++++++++--------- components/esp_modem/src/esp_modem_dte.cpp | 6 ++-- 10 files changed, 44 insertions(+), 40 deletions(-) diff --git a/components/esp_modem/CMakeLists.txt b/components/esp_modem/CMakeLists.txt index c0e37154f..efff94400 100644 --- a/components/esp_modem/CMakeLists.txt +++ b/components/esp_modem/CMakeLists.txt @@ -42,7 +42,7 @@ idf_component_register(SRCS "${srcs}" set_target_properties(${COMPONENT_LIB} PROPERTIES - CXX_STANDARD 17 + CXX_STANDARD 23 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS ON ) diff --git a/components/esp_modem/command/include/cxx_include/esp_modem_command_library.hpp b/components/esp_modem/command/include/cxx_include/esp_modem_command_library.hpp index 28d922d68..d40d106f9 100644 --- a/components/esp_modem/command/include/cxx_include/esp_modem_command_library.hpp +++ b/components/esp_modem/command/include/cxx_include/esp_modem_command_library.hpp @@ -28,9 +28,9 @@ namespace dce_commands { * @param fail_phrase String to be present in the reply to fail this command * @param timeout_ms Timeout in ms */ -command_result generic_command(CommandableIf *t, const std::string &command, - const std::string &pass_phrase, - const std::string &fail_phrase, uint32_t timeout_ms); +command_result generic_command(CommandableIf *t, std::string_view command, + std::string_view pass_phrase, + std::string_view fail_phrase, uint32_t timeout_ms); /** * @brief Declaration of all commands is generated from esp_modem_command_declare.inc */ diff --git a/components/esp_modem/command/src/esp_modem_modules.cpp b/components/esp_modem/command/src/esp_modem_modules.cpp index 2bc39f9b2..eaf7db0e8 100644 --- a/components/esp_modem/command/src/esp_modem_modules.cpp +++ b/components/esp_modem/command/src/esp_modem_modules.cpp @@ -496,8 +496,8 @@ command_result SQNGM02S::connect(PdpContext &pdp) return res; } //wait for +CEREG: 5 or +CEREG: 1. - const auto pass = std::list({"+CEREG: 1", "+CEREG: 5"}); - const auto fail = std::list({"ERROR"}); + const std::string_view pass[] {"+CEREG: 1", "+CEREG: 5"}; + const std::string_view fail[] {"ERROR"}; res = esp_modem::dce_commands::generic_command(dte.get(), "", pass, fail, 1200000); if (res != command_result::OK) { config_network_registration_urc(0); diff --git a/components/esp_modem/generate/include/cxx_include/esp_modem_command_library.hpp b/components/esp_modem/generate/include/cxx_include/esp_modem_command_library.hpp index 423af8c5f..1bf6275fd 100644 --- a/components/esp_modem/generate/include/cxx_include/esp_modem_command_library.hpp +++ b/components/esp_modem/generate/include/cxx_include/esp_modem_command_library.hpp @@ -31,9 +31,9 @@ namespace dce_commands { * @param fail_phrase String to be present in the reply to fail this command * @param timeout_ms Timeout in ms */ -command_result generic_command(CommandableIf *t, const std::string &command, - const std::string &pass_phrase, - const std::string &fail_phrase, uint32_t timeout_ms); +command_result generic_command(CommandableIf *t, std::string_view command, + std::string_view pass_phrase, + std::string_view fail_phrase, uint32_t timeout_ms); /** * @brief Declaration of all commands is generated from esp_modem_command_declare.inc diff --git a/components/esp_modem/include/cxx17_include/esp_modem_command_library_17.hpp b/components/esp_modem/include/cxx17_include/esp_modem_command_library_17.hpp index efabb8af0..59ec1e3e4 100644 --- a/components/esp_modem/include/cxx17_include/esp_modem_command_library_17.hpp +++ b/components/esp_modem/include/cxx17_include/esp_modem_command_library_17.hpp @@ -10,8 +10,8 @@ #include "cxx_include/esp_modem_dce_module.hpp" namespace esp_modem::dce_commands { -command_result generic_command(CommandableIf *t, const std::string &command, - const std::list &pass_phrase, - const std::list &fail_phrase, +command_result generic_command(CommandableIf *t, std::string_view command, + std::span pass_phrase, + std::span fail_phrase, uint32_t timeout_ms); } diff --git a/components/esp_modem/include/cxx_include/esp_modem_command_library_utils.hpp b/components/esp_modem/include/cxx_include/esp_modem_command_library_utils.hpp index 9cae9cdf9..79f9f4a1e 100644 --- a/components/esp_modem/include/cxx_include/esp_modem_command_library_utils.hpp +++ b/components/esp_modem/include/cxx_include/esp_modem_command_library_utils.hpp @@ -6,6 +6,9 @@ #pragma once +#include +#include + namespace esp_modem::dce_commands { /** @@ -17,9 +20,9 @@ namespace esp_modem::dce_commands { * @param timeout_ms Command timeout in ms * @return Generic command return type (OK, FAIL, TIMEOUT) */ -command_result generic_command(CommandableIf *t, const std::string &command, - const std::string &pass_phrase, - const std::string &fail_phrase, uint32_t timeout_ms); +command_result generic_command(CommandableIf *t, std::string_view command, + std::string_view pass_phrase, + std::string_view fail_phrase, uint32_t timeout_ms); /** * @brief Utility command to send command and return reply (after DCE says OK) @@ -29,7 +32,7 @@ command_result generic_command(CommandableIf *t, const std::string &command, * @param timeout_ms Command timeout in ms * @return Generic command return type (OK, FAIL, TIMEOUT) */ -template command_result generic_get_string(CommandableIf *t, const std::string &command, T &output, uint32_t timeout_ms = 500); +template command_result generic_get_string(CommandableIf *t, std::string_view command, T &output, uint32_t timeout_ms = 500); /** * @brief Generic command that passes on "OK" and fails on "ERROR" @@ -38,6 +41,6 @@ template command_result generic_get_string(CommandableIf *t, const * @param timeout_ms Command timeout in ms * @return Generic command return type (OK, FAIL, TIMEOUT) */ -command_result generic_command_common(CommandableIf *t, const std::string &command, uint32_t timeout_ms = 500); +command_result generic_command_common(CommandableIf *t, std::string_view command, uint32_t timeout_ms = 500); } // esp_modem::dce_commands diff --git a/components/esp_modem/include/cxx_include/esp_modem_dte.hpp b/components/esp_modem/include/cxx_include/esp_modem_dte.hpp index 4e11515ff..ddaa9cd0c 100644 --- a/components/esp_modem/include/cxx_include/esp_modem_dte.hpp +++ b/components/esp_modem/include/cxx_include/esp_modem_dte.hpp @@ -131,12 +131,12 @@ public: * @param time_ms Time in ms to wait for the answer * @return OK, FAIL, TIMEOUT */ - command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms) override; + command_result command(std::string_view command, got_line_cb got_line, uint32_t time_ms) override; /** * @brief Sends the command (same as above) but with a specific separator */ - command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms, char separator) override; + command_result command(std::string_view command, got_line_cb got_line, uint32_t time_ms, char separator) override; /** * @brief Allows this DTE to recover from a generic connection issue diff --git a/components/esp_modem/include/cxx_include/esp_modem_types.hpp b/components/esp_modem/include/cxx_include/esp_modem_types.hpp index 1387ee33b..608bd482b 100644 --- a/components/esp_modem/include/cxx_include/esp_modem_types.hpp +++ b/components/esp_modem/include/cxx_include/esp_modem_types.hpp @@ -90,8 +90,8 @@ public: * @param separator Character treated as a line separator, typically '\n' * @return OK, FAIL or TIMEOUT */ - virtual command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms, const char separator) = 0; - virtual command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms) = 0; + virtual command_result command(std::string_view command, got_line_cb got_line, uint32_t time_ms, const char separator) = 0; + virtual command_result command(std::string_view command, got_line_cb got_line, uint32_t time_ms) = 0; virtual int write(uint8_t *data, size_t len) = 0; virtual void on_read(got_line_cb on_data) = 0; diff --git a/components/esp_modem/src/esp_modem_command_library.cpp b/components/esp_modem/src/esp_modem_command_library.cpp index 024008fcf..73f230e14 100644 --- a/components/esp_modem/src/esp_modem_command_library.cpp +++ b/components/esp_modem/src/esp_modem_command_library.cpp @@ -5,7 +5,8 @@ */ #include -#include +#include + #include "esp_log.h" #include "cxx_include/esp_modem_dte.hpp" #include "cxx_include/esp_modem_dce_module.hpp" @@ -18,12 +19,12 @@ namespace esp_modem::dce_commands { static const char *TAG = "command_lib"; -command_result generic_command(CommandableIf *t, const std::string &command, - const std::list &pass_phrase, - const std::list &fail_phrase, +command_result generic_command(CommandableIf *t, std::string_view command, + std::span pass_phrase, + std::span fail_phrase, uint32_t timeout_ms) { - ESP_LOGD(TAG, "%s command %s\n", __func__, command.c_str()); + ESP_LOGD(TAG, "%s command %.*s\n", __func__, command.size(), command.data()); return t->command(command, [&](uint8_t *data, size_t len) { std::string_view response((char *)data, len); if (data == nullptr || len == 0 || response.empty()) { @@ -43,13 +44,13 @@ command_result generic_command(CommandableIf *t, const std::string &command, } -command_result generic_command(CommandableIf *t, const std::string &command, - const std::string &pass_phrase, - const std::string &fail_phrase, uint32_t timeout_ms) +command_result generic_command(CommandableIf *t, std::string_view command, + std::string_view pass_phrase, + std::string_view fail_phrase, uint32_t timeout_ms) { - ESP_LOGV(TAG, "%s", __func__); - const auto pass = std::list({pass_phrase}); - const auto fail = std::list({fail_phrase}); + ESP_LOGV(TAG, "%s", __func__ ); + const std::string_view pass[] {pass_phrase}; + const std::string_view fail[] {fail_phrase}; return generic_command(t, command, pass, fail, timeout_ms); } @@ -81,7 +82,7 @@ bool set(std::span &dest, std::string_view &src) } // str_copy -template command_result generic_get_string(CommandableIf *t, const std::string &command, T &output, uint32_t timeout_ms) +template command_result generic_get_string(CommandableIf *t, std::string_view command, T &output, uint32_t timeout_ms) { ESP_LOGV(TAG, "%s", __func__); return t->command(command, [&](uint8_t *data, size_t len) { @@ -110,7 +111,7 @@ template command_result generic_get_string(CommandableIf *t, const }, timeout_ms); } -command_result generic_command_common(CommandableIf *t, const std::string &command, uint32_t timeout_ms) +command_result generic_command_common(CommandableIf *t, std::string_view command, uint32_t timeout_ms) { ESP_LOGV(TAG, "%s", __func__); return generic_command(t, command, "OK", "ERROR", timeout_ms); @@ -314,9 +315,9 @@ command_result resume_data_mode(CommandableIf *t) command_result set_command_mode(CommandableIf *t) { - ESP_LOGV(TAG, "%s", __func__); - const auto pass = std::list({"NO CARRIER", "OK"}); - const auto fail = std::list({"ERROR"}); + ESP_LOGV(TAG, "%s", __func__ ); + const std::string_view pass[] {"NO CARRIER", "OK"}; + const std::string_view fail[] {"ERROR"}; return generic_command(t, "+++", pass, fail, 5000); } diff --git a/components/esp_modem/src/esp_modem_dte.cpp b/components/esp_modem/src/esp_modem_dte.cpp index 0ca8604ba..5adea5bc5 100644 --- a/components/esp_modem/src/esp_modem_dte.cpp +++ b/components/esp_modem/src/esp_modem_dte.cpp @@ -147,11 +147,11 @@ void DTE::set_command_callbacks() } -command_result DTE::command(const std::string &command, got_line_cb got_line, uint32_t time_ms, const char separator) +command_result DTE::command(std::string_view command, got_line_cb got_line, uint32_t time_ms, const char separator) { Scoped l1(internal_lock); command_cb.set(got_line, separator); - primary_term->write((uint8_t *)command.c_str(), command.length()); + primary_term->write((uint8_t *)command.data(), command.length()); command_cb.wait_for_line(time_ms); command_cb.set(nullptr); buffer.consumed = 0; @@ -161,7 +161,7 @@ command_result DTE::command(const std::string &command, got_line_cb got_line, ui return command_cb.result; } -command_result DTE::command(const std::string &cmd, got_line_cb got_line, uint32_t time_ms) +command_result DTE::command(std::string_view cmd, got_line_cb got_line, uint32_t time_ms) { return command(cmd, got_line, time_ms, '\n'); }