From 4a99c14613c3243716e237d53816d821b12de2f4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 2 Feb 2025 14:24:23 -0600 Subject: [PATCH] feat: avoid protobuf repeated container overhead (#90) --- src/bleak_esphome/backend/scanner.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/bleak_esphome/backend/scanner.py b/src/bleak_esphome/backend/scanner.py index a47a78d..b8d39b4 100644 --- a/src/bleak_esphome/backend/scanner.py +++ b/src/bleak_esphome/backend/scanner.py @@ -38,7 +38,16 @@ def async_on_raw_advertisements( ) -> None: """Call the registered callback.""" now = MONOTONIC_TIME() - for adv in raw.advertisements: + advertisements = raw.advertisements + # We avoid __iter__ on the protobuf object because + # the the protobuf library has an expensive internal + # debug logging when it reaches the end of a repeated field. + # https://github.com/Bluetooth-Devices/bleak-esphome/pull/90 + # To work around this we use a for loop to iterate over + # the repeated field since `PyUpb_RepeatedContainer_Subscript` + # does not trigger the debug logging. + for i in range(len(advertisements)): + adv = advertisements[i] self._async_on_advertisement( int_to_bluetooth_address(adv.address), adv.rssi,