From 901192cca10d0796a19f8716d2ba154f7b9508fd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 6 Feb 2026 23:33:21 +0100 Subject: [PATCH] [ld2450] Batch UART reads to reduce loop overhead Read all available bytes in batches via read_array() instead of byte-at-a-time read() calls. Each read() internally chains through read_byte -> read_array(1) -> check_read_timeout_ -> available(), resulting in 3 UART calls per byte. At 256000 baud with ~235 bytes per loop iteration, this was ~706 UART operations per loop call. Batching reduces this to ~12. Measured 33% reduction in loop time (2348ms -> 1577ms per 60s). --- esphome/components/ld2450/ld2450.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/esphome/components/ld2450/ld2450.cpp b/esphome/components/ld2450/ld2450.cpp index ca8d918441..5ee6dd6e3d 100644 --- a/esphome/components/ld2450/ld2450.cpp +++ b/esphome/components/ld2450/ld2450.cpp @@ -276,8 +276,23 @@ void LD2450Component::dump_config() { } void LD2450Component::loop() { - while (this->available()) { - this->readline_(this->read()); + int avail = this->available(); + if (avail == 0) { + return; + } + + // Read all available bytes in batches to reduce UART call overhead. + uint8_t buf[MAX_LINE_LENGTH]; + while (avail > 0) { + size_t to_read = std::min(static_cast(avail), sizeof(buf)); + if (!this->read_array(buf, to_read)) { + break; + } + avail -= to_read; + + for (size_t i = 0; i < to_read; i++) { + this->readline_(buf[i]); + } } }