mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[api] Eliminate intermediate buffers in protobuf dump helpers (#13742)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -23,15 +23,8 @@ static inline void append_field_prefix(DumpBuffer &out, const char *field_name,
|
|||||||
out.append(indent, ' ').append(field_name).append(": ");
|
out.append(indent, ' ').append(field_name).append(": ");
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void append_with_newline(DumpBuffer &out, const char *str) {
|
|
||||||
out.append(str);
|
|
||||||
out.append("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void append_uint(DumpBuffer &out, uint32_t value) {
|
static inline void append_uint(DumpBuffer &out, uint32_t value) {
|
||||||
char buf[16];
|
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRIu32, value));
|
||||||
snprintf(buf, sizeof(buf), "%" PRIu32, value);
|
|
||||||
out.append(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RAII helper for message dump formatting
|
// RAII helper for message dump formatting
|
||||||
@@ -49,31 +42,23 @@ class MessageDumpHelper {
|
|||||||
|
|
||||||
// Helper functions to reduce code duplication in dump methods
|
// Helper functions to reduce code duplication in dump methods
|
||||||
static void dump_field(DumpBuffer &out, const char *field_name, int32_t value, int indent = 2) {
|
static void dump_field(DumpBuffer &out, const char *field_name, int32_t value, int indent = 2) {
|
||||||
char buffer[64];
|
|
||||||
append_field_prefix(out, field_name, indent);
|
append_field_prefix(out, field_name, indent);
|
||||||
snprintf(buffer, 64, "%" PRId32, value);
|
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRId32 "\n", value));
|
||||||
append_with_newline(out, buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump_field(DumpBuffer &out, const char *field_name, uint32_t value, int indent = 2) {
|
static void dump_field(DumpBuffer &out, const char *field_name, uint32_t value, int indent = 2) {
|
||||||
char buffer[64];
|
|
||||||
append_field_prefix(out, field_name, indent);
|
append_field_prefix(out, field_name, indent);
|
||||||
snprintf(buffer, 64, "%" PRIu32, value);
|
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRIu32 "\n", value));
|
||||||
append_with_newline(out, buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump_field(DumpBuffer &out, const char *field_name, float value, int indent = 2) {
|
static void dump_field(DumpBuffer &out, const char *field_name, float value, int indent = 2) {
|
||||||
char buffer[64];
|
|
||||||
append_field_prefix(out, field_name, indent);
|
append_field_prefix(out, field_name, indent);
|
||||||
snprintf(buffer, 64, "%g", value);
|
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%g\n", value));
|
||||||
append_with_newline(out, buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump_field(DumpBuffer &out, const char *field_name, uint64_t value, int indent = 2) {
|
static void dump_field(DumpBuffer &out, const char *field_name, uint64_t value, int indent = 2) {
|
||||||
char buffer[64];
|
|
||||||
append_field_prefix(out, field_name, indent);
|
append_field_prefix(out, field_name, indent);
|
||||||
snprintf(buffer, 64, "%" PRIu64, value);
|
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRIu64 "\n", value));
|
||||||
append_with_newline(out, buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump_field(DumpBuffer &out, const char *field_name, bool value, int indent = 2) {
|
static void dump_field(DumpBuffer &out, const char *field_name, bool value, int indent = 2) {
|
||||||
@@ -112,7 +97,7 @@ static void dump_bytes_field(DumpBuffer &out, const char *field_name, const uint
|
|||||||
char hex_buf[format_hex_pretty_size(160)];
|
char hex_buf[format_hex_pretty_size(160)];
|
||||||
append_field_prefix(out, field_name, indent);
|
append_field_prefix(out, field_name, indent);
|
||||||
format_hex_pretty_to(hex_buf, data, len);
|
format_hex_pretty_to(hex_buf, data, len);
|
||||||
append_with_newline(out, hex_buf);
|
out.append(hex_buf).append("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> const char *proto_enum_to_string<enums::EntityCategory>(enums::EntityCategory value) {
|
template<> const char *proto_enum_to_string<enums::EntityCategory>(enums::EntityCategory value) {
|
||||||
|
|||||||
@@ -402,6 +402,20 @@ class DumpBuffer {
|
|||||||
const char *c_str() const { return buf_; }
|
const char *c_str() const { return buf_; }
|
||||||
size_t size() const { return pos_; }
|
size_t size() const { return pos_; }
|
||||||
|
|
||||||
|
/// Get writable buffer pointer for use with buf_append_printf
|
||||||
|
char *data() { return buf_; }
|
||||||
|
/// Get current position for use with buf_append_printf
|
||||||
|
size_t pos() const { return pos_; }
|
||||||
|
/// Update position after buf_append_printf call
|
||||||
|
void set_pos(size_t pos) {
|
||||||
|
if (pos >= CAPACITY) {
|
||||||
|
pos_ = CAPACITY - 1;
|
||||||
|
} else {
|
||||||
|
pos_ = pos;
|
||||||
|
}
|
||||||
|
buf_[pos_] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void append_impl_(const char *str, size_t len) {
|
void append_impl_(const char *str, size_t len) {
|
||||||
size_t space = CAPACITY - 1 - pos_;
|
size_t space = CAPACITY - 1 - pos_;
|
||||||
|
|||||||
@@ -2599,15 +2599,8 @@ static inline void append_field_prefix(DumpBuffer &out, const char *field_name,
|
|||||||
out.append(indent, ' ').append(field_name).append(": ");
|
out.append(indent, ' ').append(field_name).append(": ");
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void append_with_newline(DumpBuffer &out, const char *str) {
|
|
||||||
out.append(str);
|
|
||||||
out.append("\\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void append_uint(DumpBuffer &out, uint32_t value) {
|
static inline void append_uint(DumpBuffer &out, uint32_t value) {
|
||||||
char buf[16];
|
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRIu32, value));
|
||||||
snprintf(buf, sizeof(buf), "%" PRIu32, value);
|
|
||||||
out.append(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RAII helper for message dump formatting
|
// RAII helper for message dump formatting
|
||||||
@@ -2625,31 +2618,23 @@ class MessageDumpHelper {
|
|||||||
|
|
||||||
// Helper functions to reduce code duplication in dump methods
|
// Helper functions to reduce code duplication in dump methods
|
||||||
static void dump_field(DumpBuffer &out, const char *field_name, int32_t value, int indent = 2) {
|
static void dump_field(DumpBuffer &out, const char *field_name, int32_t value, int indent = 2) {
|
||||||
char buffer[64];
|
|
||||||
append_field_prefix(out, field_name, indent);
|
append_field_prefix(out, field_name, indent);
|
||||||
snprintf(buffer, 64, "%" PRId32, value);
|
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRId32 "\\n", value));
|
||||||
append_with_newline(out, buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump_field(DumpBuffer &out, const char *field_name, uint32_t value, int indent = 2) {
|
static void dump_field(DumpBuffer &out, const char *field_name, uint32_t value, int indent = 2) {
|
||||||
char buffer[64];
|
|
||||||
append_field_prefix(out, field_name, indent);
|
append_field_prefix(out, field_name, indent);
|
||||||
snprintf(buffer, 64, "%" PRIu32, value);
|
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRIu32 "\\n", value));
|
||||||
append_with_newline(out, buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump_field(DumpBuffer &out, const char *field_name, float value, int indent = 2) {
|
static void dump_field(DumpBuffer &out, const char *field_name, float value, int indent = 2) {
|
||||||
char buffer[64];
|
|
||||||
append_field_prefix(out, field_name, indent);
|
append_field_prefix(out, field_name, indent);
|
||||||
snprintf(buffer, 64, "%g", value);
|
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%g\\n", value));
|
||||||
append_with_newline(out, buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump_field(DumpBuffer &out, const char *field_name, uint64_t value, int indent = 2) {
|
static void dump_field(DumpBuffer &out, const char *field_name, uint64_t value, int indent = 2) {
|
||||||
char buffer[64];
|
|
||||||
append_field_prefix(out, field_name, indent);
|
append_field_prefix(out, field_name, indent);
|
||||||
snprintf(buffer, 64, "%" PRIu64, value);
|
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRIu64 "\\n", value));
|
||||||
append_with_newline(out, buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dump_field(DumpBuffer &out, const char *field_name, bool value, int indent = 2) {
|
static void dump_field(DumpBuffer &out, const char *field_name, bool value, int indent = 2) {
|
||||||
@@ -2689,7 +2674,7 @@ static void dump_bytes_field(DumpBuffer &out, const char *field_name, const uint
|
|||||||
char hex_buf[format_hex_pretty_size(160)];
|
char hex_buf[format_hex_pretty_size(160)];
|
||||||
append_field_prefix(out, field_name, indent);
|
append_field_prefix(out, field_name, indent);
|
||||||
format_hex_pretty_to(hex_buf, data, len);
|
format_hex_pretty_to(hex_buf, data, len);
|
||||||
append_with_newline(out, hex_buf);
|
out.append(hex_buf).append("\\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user