Optimize logger performance by eliminating redundant strlen calls (#9369)

This commit is contained in:
J. Nick Koston 2025-07-07 15:36:36 -05:00 committed by GitHub
parent 832a787271
commit 90fb3680d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 2 deletions

View File

@ -355,7 +355,7 @@ class Logger : public Component {
}
inline void HOT write_footer_to_buffer_(char *buffer, uint16_t *buffer_at, uint16_t buffer_size) {
static const uint16_t RESET_COLOR_LEN = strlen(ESPHOME_LOG_RESET_COLOR);
static constexpr uint16_t RESET_COLOR_LEN = sizeof(ESPHOME_LOG_RESET_COLOR) - 1;
this->write_body_to_buffer_(ESPHOME_LOG_RESET_COLOR, RESET_COLOR_LEN, buffer, buffer_at, buffer_size);
}

View File

@ -184,7 +184,9 @@ void HOT Logger::write_msg_(const char *msg) {
) {
puts(msg);
} else {
uart_write_bytes(this->uart_num_, msg, strlen(msg));
// Use tx_buffer_at_ if msg points to tx_buffer_, otherwise fall back to strlen
size_t len = (msg == this->tx_buffer_) ? this->tx_buffer_at_ : strlen(msg);
uart_write_bytes(this->uart_num_, msg, len);
uart_write_bytes(this->uart_num_, "\n", 1);
}
}