mirror of
https://github.com/esphome/esphome.git
synced 2026-06-01 17:57:37 +08:00
[api] Skip state_action_() call in noise data path (#14629)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -232,6 +232,17 @@ class APIFrameHelper {
|
|||||||
EXPLICIT_REJECT = 8, // Noise only
|
EXPLICIT_REJECT = 8, // Noise only
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Fast inline state check for read_packet/write_protobuf_messages hot path.
|
||||||
|
// Returns OK only in DATA state; maps CLOSED/FAILED to BAD_STATE and any
|
||||||
|
// other intermediate state to WOULD_BLOCK.
|
||||||
|
inline APIError ESPHOME_ALWAYS_INLINE check_data_state_() const {
|
||||||
|
if (this->state_ == State::DATA)
|
||||||
|
return APIError::OK;
|
||||||
|
if (this->state_ == State::CLOSED || this->state_ == State::FAILED)
|
||||||
|
return APIError::BAD_STATE;
|
||||||
|
return APIError::WOULD_BLOCK;
|
||||||
|
}
|
||||||
|
|
||||||
// Containers (size varies, but typically 12+ bytes on 32-bit)
|
// Containers (size varies, but typically 12+ bytes on 32-bit)
|
||||||
std::array<std::unique_ptr<SendBuffer>, API_MAX_SEND_QUEUE> tx_buf_;
|
std::array<std::unique_ptr<SendBuffer>, API_MAX_SEND_QUEUE> tx_buf_;
|
||||||
std::vector<uint8_t> rx_buf_;
|
std::vector<uint8_t> rx_buf_;
|
||||||
|
|||||||
@@ -397,14 +397,9 @@ void APINoiseFrameHelper::send_explicit_handshake_reject_(const LogString *reaso
|
|||||||
state_ = orig_state;
|
state_ = orig_state;
|
||||||
}
|
}
|
||||||
APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
||||||
APIError aerr = this->state_action_();
|
APIError aerr = this->check_data_state_();
|
||||||
if (aerr != APIError::OK) {
|
if (aerr != APIError::OK)
|
||||||
return aerr;
|
return aerr;
|
||||||
}
|
|
||||||
|
|
||||||
if (this->state_ != State::DATA) {
|
|
||||||
return APIError::WOULD_BLOCK;
|
|
||||||
}
|
|
||||||
|
|
||||||
aerr = this->try_read_frame_();
|
aerr = this->try_read_frame_();
|
||||||
if (aerr != APIError::OK)
|
if (aerr != APIError::OK)
|
||||||
@@ -461,14 +456,9 @@ APIError APINoiseFrameHelper::write_protobuf_packet(uint8_t type, ProtoWriteBuff
|
|||||||
}
|
}
|
||||||
|
|
||||||
APIError APINoiseFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffer, std::span<const MessageInfo> messages) {
|
APIError APINoiseFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffer, std::span<const MessageInfo> messages) {
|
||||||
APIError aerr = state_action_();
|
APIError aerr = this->check_data_state_();
|
||||||
if (aerr != APIError::OK) {
|
if (aerr != APIError::OK)
|
||||||
return aerr;
|
return aerr;
|
||||||
}
|
|
||||||
|
|
||||||
if (state_ != State::DATA) {
|
|
||||||
return APIError::WOULD_BLOCK;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (messages.empty()) {
|
if (messages.empty()) {
|
||||||
return APIError::OK;
|
return APIError::OK;
|
||||||
|
|||||||
@@ -195,11 +195,11 @@ APIError APIPlaintextFrameHelper::try_read_frame_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
||||||
if (this->state_ != State::DATA) {
|
APIError aerr = this->check_data_state_();
|
||||||
return APIError::WOULD_BLOCK;
|
if (aerr != APIError::OK)
|
||||||
}
|
return aerr;
|
||||||
|
|
||||||
APIError aerr = this->try_read_frame_();
|
aerr = this->try_read_frame_();
|
||||||
if (aerr != APIError::OK) {
|
if (aerr != APIError::OK) {
|
||||||
if (aerr == APIError::BAD_INDICATOR) {
|
if (aerr == APIError::BAD_INDICATOR) {
|
||||||
// Make sure to tell the remote that we don't
|
// Make sure to tell the remote that we don't
|
||||||
@@ -244,9 +244,9 @@ APIError APIPlaintextFrameHelper::write_protobuf_packet(uint8_t type, ProtoWrite
|
|||||||
|
|
||||||
APIError APIPlaintextFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffer,
|
APIError APIPlaintextFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffer,
|
||||||
std::span<const MessageInfo> messages) {
|
std::span<const MessageInfo> messages) {
|
||||||
if (state_ != State::DATA) {
|
APIError aerr = this->check_data_state_();
|
||||||
return APIError::BAD_STATE;
|
if (aerr != APIError::OK)
|
||||||
}
|
return aerr;
|
||||||
|
|
||||||
if (messages.empty()) {
|
if (messages.empty()) {
|
||||||
return APIError::OK;
|
return APIError::OK;
|
||||||
|
|||||||
Reference in New Issue
Block a user