Require reserve_size in create_buffer to reduce realloc overhead (#8715)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
J. Nick Koston 2025-05-08 00:50:20 -05:00 committed by GitHub
parent ef2621aa54
commit 9e64e71cdf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 8 deletions

View File

@ -212,8 +212,7 @@ void APIConnection::loop() {
msg_size += 1 + ProtoSize::varint(to_send) + to_send;
ProtoSize::add_bool_field(msg_size, 1, done);
auto buffer = this->create_buffer();
buffer.get_buffer()->reserve(msg_size);
auto buffer = this->create_buffer(msg_size);
// fixed32 key = 1;
buffer.encode_fixed32(1, esp32_camera::global_esp32_camera->get_object_id_hash());
// bytes data = 2;
@ -1807,8 +1806,7 @@ bool APIConnection::try_send_log_message(int level, const char *tag, const char
msg_size += 1 + api::ProtoSize::varint(static_cast<uint32_t>(line_length)) + line_length;
// Create a pre-sized buffer
auto buffer = this->create_buffer();
buffer.get_buffer()->reserve(msg_size);
auto buffer = this->create_buffer(msg_size);
// Encode the message (SubscribeLogsResponse)
buffer.encode_uint32(1, static_cast<uint32_t>(level)); // LogLevel level = 1

View File

@ -312,9 +312,10 @@ class APIConnection : public APIServerConnection {
void on_fatal_error() override;
void on_unauthenticated_access() override;
void on_no_setup_connection() override;
ProtoWriteBuffer create_buffer() override {
ProtoWriteBuffer create_buffer(uint32_t reserve_size) override {
// FIXME: ensure no recursive writes can happen
this->proto_write_buffer_.clear();
this->proto_write_buffer_.reserve(reserve_size);
return {&this->proto_write_buffer_};
}
bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override;

View File

@ -311,7 +311,14 @@ class ProtoService {
virtual void on_fatal_error() = 0;
virtual void on_unauthenticated_access() = 0;
virtual void on_no_setup_connection() = 0;
virtual ProtoWriteBuffer create_buffer() = 0;
/**
* Create a buffer with a reserved size.
* @param reserve_size The number of bytes to pre-allocate in the buffer. This is a hint
* to optimize memory usage and avoid reallocations during encoding.
* Implementations should aim to allocate at least this size.
* @return A ProtoWriteBuffer object with the reserved size.
*/
virtual ProtoWriteBuffer create_buffer(uint32_t reserve_size) = 0;
virtual bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) = 0;
virtual bool read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) = 0;
@ -321,8 +328,7 @@ class ProtoService {
msg.calculate_size(msg_size);
// Create a pre-sized buffer
auto buffer = this->create_buffer();
buffer.get_buffer()->reserve(msg_size);
auto buffer = this->create_buffer(msg_size);
// Encode message into the buffer
msg.encode(buffer);