fix: Reduce heap allocations by getting rid of temporary std::list and std::string

This commit is contained in:
2023-05-31 16:13:55 +02:00
parent 331935d7ec
commit 2ded13f195
7 changed files with 36 additions and 32 deletions
@@ -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
@@ -6,6 +6,9 @@
#pragma once
#include <string_view>
#include <span>
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)
@@ -30,7 +33,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)
*/
command_result generic_get_string(CommandableIf *t, const std::string &command, std::string &output, uint32_t timeout_ms = 500);
command_result generic_get_string(CommandableIf *t, std::string_view command, std::string &output, uint32_t timeout_ms = 500);
/**
* @brief Generic command that passes on "OK" and fails on "ERROR"
@@ -40,6 +43,6 @@ command_result generic_get_string(CommandableIf *t, const std::string &command,
* @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
@@ -74,7 +74,7 @@ public:
return device.get();
}
command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms)
command_result command(std::string_view command, got_line_cb got_line, uint32_t time_ms)
{
return dte->command(command, std::move(got_line), time_ms);
}
@@ -101,12 +101,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;
protected:
/**
@@ -78,8 +78,8 @@ public:
* @param time_ms timeout in milliseconds
* @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;