From 348d5f583ade800be48c3364713ced678967a702 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 29 Mar 2026 10:47:07 -1000 Subject: [PATCH] [api] Restructure write_raw_ for single tail call to slow path --- esphome/components/api/api_frame_helper.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/esphome/components/api/api_frame_helper.h b/esphome/components/api/api_frame_helper.h index e8e27f8d98..0bf04735a2 100644 --- a/esphome/components/api/api_frame_helper.h +++ b/esphome/components/api/api_frame_helper.h @@ -193,27 +193,25 @@ class APIFrameHelper { // Write a single contiguous buffer to the socket (inlined fast path) inline APIError ESPHOME_ALWAYS_INLINE write_raw_(const void *data, uint16_t len) { // Fast path: no overflow backlog and full write succeeds + ssize_t sent = -1; if (this->overflow_buf_.empty()) [[likely]] { - ssize_t sent = this->socket_->write(data, len); + sent = this->socket_->write(data, len); if (sent == static_cast(len)) [[likely]] return APIError::OK; - // Slow path: wrap in iovec and handle error/overflow - struct iovec iov = {const_cast(data), len}; - return this->write_raw_slow_(&iov, 1, len, sent); } struct iovec iov = {const_cast(data), len}; - return this->write_raw_slow_(&iov, 1, len, -1); + return this->write_raw_slow_(&iov, 1, len, sent); } // Write multiple iovec buffers to the socket (inlined fast path) inline APIError ESPHOME_ALWAYS_INLINE write_raw_(const struct iovec *iov, int iovcnt, uint16_t total_write_len) { // Fast path: no overflow backlog and full writev succeeds + ssize_t sent = -1; if (this->overflow_buf_.empty()) [[likely]] { - ssize_t sent = this->socket_->writev(iov, iovcnt); + sent = this->socket_->writev(iov, iovcnt); if (sent == static_cast(total_write_len)) [[likely]] return APIError::OK; - return this->write_raw_slow_(iov, iovcnt, total_write_len, sent); } - return this->write_raw_slow_(iov, iovcnt, total_write_len, -1); + return this->write_raw_slow_(iov, iovcnt, total_write_len, sent); } // Slow path (out-of-line): handle partial writes, errors, overflow buffering APIError write_raw_slow_(const struct iovec *iov, int iovcnt, uint16_t total_write_len, ssize_t sent);