Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bench/test_parse_gap_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
parse_advertisement_data_bytes,
parse_advertisement_data_tuple,
)
from bluetooth_data_tools.gap import _uncached_parse_advertisement_data
from bluetooth_data_tools.gap import _parse_advertisement_data_miss_via_bytes

# cythonize -X language_level=3 -a -i src/bluetooth_data_tools/gap.py

Expand Down Expand Up @@ -91,7 +91,7 @@ def test_parse_advertisement_data_tuple(benchmark):

def test_parse_advertisement_data_tuple_uncached(benchmark):
joined_advs = b"".join(advs)
benchmark(lambda: _uncached_parse_advertisement_data(joined_advs))
benchmark(lambda: _parse_advertisement_data_miss_via_bytes(joined_advs))


Comment on lines +94 to 96
def test_parse_advertisement_data_tuple_bytes_cache_fallthrough(benchmark):
Expand Down
4 changes: 2 additions & 2 deletions src/bluetooth_data_tools/gap.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ cpdef parse_advertisement_data(object data)
)
cpdef _uncached_parse_advertisement_bytes(bytes gap_bytes)

cpdef _uncached_parse_advertisement_data(bytes data)
cpdef _parse_advertisement_data_miss_via_bytes(bytes data)

cpdef _uncached_parse_advertisement_tuple(tuple data)
cpdef _parse_advertisement_tuple_miss_via_bytes(tuple data)
18 changes: 12 additions & 6 deletions src/bluetooth_data_tools/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _parse_advertisement_data(
data: bytes,
) -> BLEGAPAdvertisement:
"""Parse advertisement data and return a BLEGAPAdvertisement."""
return _uncached_parse_advertisement_data(data)
return _parse_advertisement_data_miss_via_bytes(data)


_cached_parse_advertisement_data = _parse_advertisement_data
Expand Down Expand Up @@ -182,11 +182,17 @@ def parse_advertisement_data(
return _cached_parse_advertisement_data(b"".join(data))


def _uncached_parse_advertisement_data(data: bytes) -> BLEGAPAdvertisement:
return BLEGAPAdvertisement(*_uncached_parse_advertisement_bytes(data))
def _parse_advertisement_data_miss_via_bytes(data: bytes) -> BLEGAPAdvertisement:
# Route BLEGAPAdvertisement-cache misses through the bytes-keyed cache
# (symmetric with _parse_advertisement_tuple_miss_via_bytes, see #261).
# Identical payloads reaching this miss path skip the full parse when the
# bytes-tuple cache already holds the result, and a true miss populates
# that cache so subsequent parse_advertisement_data_bytes /
# parse_advertisement_data_tuple calls on the same payload also win.
return BLEGAPAdvertisement(*parse_advertisement_data_bytes(data))


def _uncached_parse_advertisement_tuple(
def _parse_advertisement_tuple_miss_via_bytes(
data: tuple[bytes, ...],
) -> BLEGAPAdvertisementTupleType:
# Route tuple-cache misses through the bytes-keyed cache so identical
Expand Down Expand Up @@ -379,11 +385,11 @@ def parse_advertisement_data_tuple(
manufacturer_data: dict[int, bytes]
tx_power: int | None
"""
return _uncached_parse_advertisement_tuple(data)
return _parse_advertisement_tuple_miss_via_bytes(data)
else:
parse_advertisement_data_bytes = lru_cache(maxsize=1024)(
_uncached_parse_advertisement_bytes
)
parse_advertisement_data_tuple = lru_cache(maxsize=256)(
_uncached_parse_advertisement_tuple
_parse_advertisement_tuple_miss_via_bytes
)
10 changes: 5 additions & 5 deletions tests/benchmarks/test_parse_gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ def test_parse_advertisement_data(benchmark: BenchmarkFixture) -> None:
def test_parse_advertisement_data_bytes_cache_fallthrough(
benchmark: BenchmarkFixture,
) -> None:
"""BLEGAPAdvertisement-cache misses should fall through to the bytes cache.
"""BLEGAPAdvertisement-cache misses must fall through to the bytes cache.

Mirrors the parse_advertisement_data_tuple fallthrough bench in
test_parse_gap_tuple.py: with the bytes-tuple cache hot, evicting the
BLEGAPAdvertisement entry should not pay the full parse cost again once
the wrapper rehydrates from the bytes cache.
Mirrors the parse_advertisement_data_tuple fallthrough bench: with the
bytes-tuple cache hot, evicting the BLEGAPAdvertisement entry should not
pay the full parse cost again — the wrapper rehydrates from the bytes
cache and only re-constructs the BLEGAPAdvertisement.
"""
advs_as_single_tuple = (b"".join(advs),)
parse_advertisement_data(advs_as_single_tuple)
Expand Down
4 changes: 2 additions & 2 deletions tests/benchmarks/test_parse_gap_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
parse_advertisement_data_bytes,
parse_advertisement_data_tuple,
)
from bluetooth_data_tools.gap import _uncached_parse_advertisement_data
from bluetooth_data_tools.gap import _parse_advertisement_data_miss_via_bytes

advs = (
b"\x02\x01\x06\x03\x03\x12\x18\x10\tLOOKin_98F33163\x03\x19\xc1\x03",
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_parse_advertisement_data_tuple(benchmark: BenchmarkFixture) -> None:

def test_parse_advertisement_data_tuple_uncached(benchmark: BenchmarkFixture) -> None:
joined_advs = b"".join(advs)
benchmark(lambda: _uncached_parse_advertisement_data(joined_advs))
benchmark(lambda: _parse_advertisement_data_miss_via_bytes(joined_advs))


Comment on lines +95 to 97
def test_parse_advertisement_data_tuple_bytes_cache_fallthrough(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_gap_fuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import random

from bluetooth_data_tools.gap import (
_uncached_parse_advertisement_data,
_parse_advertisement_data_miss_via_bytes,
parse_advertisement_data,
)

Expand All @@ -32,7 +32,7 @@ def test_gap_fuzzer_random_bytes_do_not_crash() -> None:
bytes(rng.randint(0, 255) for _ in range(rng.randint(1, 31))),
bytes(rng.randint(0, 255) for _ in range(rng.randint(1, 31))),
)
_uncached_parse_advertisement_data(b"".join(adv))
_parse_advertisement_data_miss_via_bytes(b"".join(adv))
Comment on lines 17 to +35


def test_gap_fuzzer_truncated_length_does_not_crash() -> None:
Expand Down
Loading