Skip to content

Commit

Permalink
feat: avoid protobuf repeated container overhead (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Feb 2, 2025
1 parent 86a0b27 commit 4a99c14
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/bleak_esphome/backend/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4a99c14

Please sign in to comment.