From 2067053adbede82d0553a25cd3fb03ce56b6c321 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 20 Mar 2026 21:14:45 -1000 Subject: [PATCH] [api] Remove ProtoService base class ProtoService was an abstract interface with 6 pure virtual methods, but APIConnection was the only concrete implementation. Move all functionality directly into APIConnection and remove the unnecessary virtual dispatch and vtable overhead. --- esphome/components/api/api_connection.h | 12 ++++++------ esphome/components/api/api_pb2_service.h | 2 +- esphome/components/api/proto.h | 12 ++---------- script/api_protobuf/api_protobuf.py | 2 +- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index e44fbab603..58e0addf3f 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -57,7 +57,7 @@ class APIConnection final : public APIServerConnectionBase { protected: // Override read_message here (instead of in APIServerConnectionBase) so the // compiler can devirtualize and inline on_* handler calls within this class. - void read_message(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) override; + void read_message(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data); // Auth helpers defined here (not in ProtoService) so the compiler can // devirtualize is_connection_setup()/on_no_setup_connection() calls @@ -286,10 +286,10 @@ class APIConnection final : public APIServerConnectionBase { void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &msg); #endif - bool is_authenticated() override { + bool is_authenticated() { return static_cast(this->flags_.connection_state) == ConnectionState::AUTHENTICATED; } - bool is_connection_setup() override { + bool is_connection_setup() { return static_cast(this->flags_.connection_state) == ConnectionState::CONNECTED || this->is_authenticated(); } @@ -302,8 +302,8 @@ class APIConnection final : public APIServerConnectionBase { (this->client_api_version_major_ == major && this->client_api_version_minor_ >= minor); } - void on_fatal_error() override; - void on_no_setup_connection() override; + void on_fatal_error(); + void on_no_setup_connection(); // Function pointer type for type-erased message encoding using MessageEncodeFn = void (*)(const void *, ProtoWriteBuffer &); @@ -342,7 +342,7 @@ class APIConnection final : public APIServerConnectionBase { return true; return this->try_to_clear_buffer_slow_(log_out_of_space); } - bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) override; + bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type); const char *get_name() const { return this->helper_->get_client_name(); } /// Get peer name (IP address) into caller-provided buffer, returns buf for convenience diff --git a/esphome/components/api/api_pb2_service.h b/esphome/components/api/api_pb2_service.h index 471fb5c0b0..4925a6497a 100644 --- a/esphome/components/api/api_pb2_service.h +++ b/esphome/components/api/api_pb2_service.h @@ -8,7 +8,7 @@ namespace esphome::api { -class APIServerConnectionBase : public ProtoService { +class APIServerConnectionBase { public: #ifdef HAS_PROTO_MESSAGE_DUMP protected: diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index a1826742a5..845714c833 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -697,15 +697,7 @@ inline void ProtoLengthDelimited::decode_to_message(ProtoDecodableMessage &msg) template const char *proto_enum_to_string(T value); -class ProtoService { - public: - protected: - virtual bool is_authenticated() = 0; - virtual bool is_connection_setup() = 0; - virtual void on_fatal_error() = 0; - virtual void on_no_setup_connection() = 0; - virtual bool send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) = 0; - virtual void read_message(uint32_t msg_size, uint32_t msg_type, const uint8_t *msg_data) = 0; -}; +// ProtoService removed — all methods moved to APIServerConnectionBase. +// APIConnection is the only concrete class; virtual dispatch was unnecessary. } // namespace esphome::api diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 7d8ad5c98a..ff7ef48a84 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -2958,7 +2958,7 @@ static const char *const TAG = "api.service"; class_name = "APIServerConnectionBase" - hpp += f"class {class_name} : public ProtoService {{\n" + hpp += f"class {class_name} {{\n" hpp += " public:\n" # Add logging helper method declarations