mirror of
https://github.com/esphome/esphome.git
synced 2025-11-19 16:25:50 +00:00
[web_server_idf] Reduce flash by eliminating temporary string allocations in event formatting (#11658)
Co-authored-by: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com>
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
#include <cinttypes>
|
||||||
|
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
@@ -245,8 +246,8 @@ void AsyncWebServerRequest::redirect(const std::string &url) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AsyncWebServerRequest::init_response_(AsyncWebServerResponse *rsp, int code, const char *content_type) {
|
void AsyncWebServerRequest::init_response_(AsyncWebServerResponse *rsp, int code, const char *content_type) {
|
||||||
// Set status code - use constants for common codes to avoid string allocation
|
// Set status code - use constants for common codes, default to 500 for unknown codes
|
||||||
const char *status = nullptr;
|
const char *status;
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case 200:
|
case 200:
|
||||||
status = HTTPD_200;
|
status = HTTPD_200;
|
||||||
@@ -258,9 +259,10 @@ void AsyncWebServerRequest::init_response_(AsyncWebServerResponse *rsp, int code
|
|||||||
status = HTTPD_409;
|
status = HTTPD_409;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
status = HTTPD_500;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
httpd_resp_set_status(*this, status == nullptr ? to_string(code).c_str() : status);
|
httpd_resp_set_status(*this, status);
|
||||||
|
|
||||||
if (content_type && *content_type) {
|
if (content_type && *content_type) {
|
||||||
httpd_resp_set_type(*this, content_type);
|
httpd_resp_set_type(*this, content_type);
|
||||||
@@ -348,7 +350,13 @@ void AsyncWebServerResponse::addHeader(const char *name, const char *value) {
|
|||||||
httpd_resp_set_hdr(*this->req_, name, value);
|
httpd_resp_set_hdr(*this->req_, name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AsyncResponseStream::print(float value) { this->print(to_string(value)); }
|
void AsyncResponseStream::print(float value) {
|
||||||
|
// Use stack buffer to avoid temporary string allocation
|
||||||
|
// Size: sign (1) + digits (10) + decimal (1) + precision (6) + exponent (5) + null (1) = 24, use 32 for safety
|
||||||
|
char buf[32];
|
||||||
|
int len = snprintf(buf, sizeof(buf), "%f", value);
|
||||||
|
this->content_.append(buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
void AsyncResponseStream::printf(const char *fmt, ...) {
|
void AsyncResponseStream::printf(const char *fmt, ...) {
|
||||||
va_list args;
|
va_list args;
|
||||||
@@ -593,16 +601,19 @@ bool AsyncEventSourceResponse::try_send_nodefer(const char *message, const char
|
|||||||
|
|
||||||
event_buffer_.append(chunk_len_header);
|
event_buffer_.append(chunk_len_header);
|
||||||
|
|
||||||
|
// Use stack buffer for formatting numeric fields to avoid temporary string allocations
|
||||||
|
// Size: "retry: " (7) + max uint32 (10 digits) + CRLF (2) + null (1) = 20 bytes, use 32 for safety
|
||||||
|
constexpr size_t num_buf_size = 32;
|
||||||
|
char num_buf[num_buf_size];
|
||||||
|
|
||||||
if (reconnect) {
|
if (reconnect) {
|
||||||
event_buffer_.append("retry: ", sizeof("retry: ") - 1);
|
int len = snprintf(num_buf, num_buf_size, "retry: %" PRIu32 CRLF_STR, reconnect);
|
||||||
event_buffer_.append(to_string(reconnect));
|
event_buffer_.append(num_buf, len);
|
||||||
event_buffer_.append(CRLF_STR, CRLF_LEN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
event_buffer_.append("id: ", sizeof("id: ") - 1);
|
int len = snprintf(num_buf, num_buf_size, "id: %" PRIu32 CRLF_STR, id);
|
||||||
event_buffer_.append(to_string(id));
|
event_buffer_.append(num_buf, len);
|
||||||
event_buffer_.append(CRLF_STR, CRLF_LEN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event && *event) {
|
if (event && *event) {
|
||||||
|
|||||||
Reference in New Issue
Block a user