Skip to content

Commit 4a99c14

Browse files
authored
feat: avoid protobuf repeated container overhead (#90)
1 parent 86a0b27 commit 4a99c14

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/bleak_esphome/backend/scanner.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@ def async_on_raw_advertisements(
3838
) -> None:
3939
"""Call the registered callback."""
4040
now = MONOTONIC_TIME()
41-
for adv in raw.advertisements:
41+
advertisements = raw.advertisements
42+
# We avoid __iter__ on the protobuf object because
43+
# the the protobuf library has an expensive internal
44+
# debug logging when it reaches the end of a repeated field.
45+
# https://github.com/Bluetooth-Devices/bleak-esphome/pull/90
46+
# To work around this we use a for loop to iterate over
47+
# the repeated field since `PyUpb_RepeatedContainer_Subscript`
48+
# does not trigger the debug logging.
49+
for i in range(len(advertisements)):
50+
adv = advertisements[i]
4251
self._async_on_advertisement(
4352
int_to_bluetooth_address(adv.address),
4453
adv.rssi,

0 commit comments

Comments
 (0)