Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve cache performance #99

Merged
merged 6 commits into from
Feb 2, 2025
Merged
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
2 changes: 0 additions & 2 deletions src/bluetooth_data_tools/gap.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ cdef cython.uint TYPE_TX_POWER_LEVEL

cpdef parse_advertisement_data(object data)

cpdef parse_advertisement_data_tuple(cython.tuple data)

@cython.locals(
gap_data=cython.bytes,
gap_value=cython.bytes,
Expand Down
76 changes: 30 additions & 46 deletions src/bluetooth_data_tools/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Iterable
from enum import IntEnum
from functools import lru_cache, partial
from typing import TYPE_CHECKING

BLE_UUID = "0000-1000-8000-00805f9b34fb"
_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -101,13 +102,7 @@ class BLEGAPType(IntEnum):
]


@lru_cache(maxsize=256)
def _from_bytes_signed(bytes_: bytes_) -> int:
"""Convert bytes to a signed integer."""
return from_bytes_signed(bytes_)


_cached_from_bytes_signed = _from_bytes_signed
_cached_from_bytes_signed = lru_cache(maxsize=256)(from_bytes_signed)


@lru_cache(maxsize=256)
Expand Down Expand Up @@ -138,14 +133,7 @@ def _uint32_bytes_as_uuid(uuid32_bytes: bytes_) -> str:

_cached_uint32_bytes_as_uuid = _uint32_bytes_as_uuid


@lru_cache(maxsize=256)
def _manufacturer_id_bytes_to_int(manufacturer_id_bytes: bytes_) -> int:
"""Convert manufacturer ID bytes to an int."""
return from_bytes_little(manufacturer_id_bytes)


_cached_manufacturer_id_bytes_to_int = _manufacturer_id_bytes_to_int
_cached_manufacturer_id_bytes_to_int = lru_cache(maxsize=256)(from_bytes_little)


@lru_cache(maxsize=256)
Expand All @@ -168,37 +156,6 @@ def parse_advertisement_data(
return _cached_parse_advertisement_data(tuple(data))


@lru_cache(maxsize=256)
def _parse_advertisement_data_tuple(
data: tuple[bytes, ...],
) -> BLEGAPAdvertisementTupleType:
"""Parse a tuple of raw advertisement data and return a tuple of BLEGAPAdvertisementTupleType.

The format of the tuple is:
(local_name, service_uuids, service_data, manufacturer_data, tx_power)

This is tightly coupled to bleak. If you are not using bleak
it is recommended to use parse_advertisement_data instead.

local_name: str | None
service_uuids: list[str]
service_data: dict[str, bytes]
manufacturer_data: dict[int, bytes]
tx_power: int | None
"""
return _uncached_parse_advertisement_data(data)


_cached_parse_advertisement_data_tuple = _parse_advertisement_data_tuple


def parse_advertisement_data_tuple(
data: tuple[bytes, ...],
) -> BLEGAPAdvertisementTupleType:
"""Parse a tuple of raw advertisement data and return a tuple of BLEGAPAdvertisementTupleType."""
return _cached_parse_advertisement_data_tuple(data)


def _uncached_parse_advertisement_data(
data: tuple[bytes, ...],
) -> BLEGAPAdvertisementTupleType:
Expand Down Expand Up @@ -273,3 +230,30 @@ def _uncached_parse_advertisement_data(
tx_power = _cached_from_bytes_signed(gap_data[start:end])

return (local_name, service_uuids, service_data, manufacturer_data, tx_power)


if TYPE_CHECKING:

@lru_cache(maxsize=256)
def parse_advertisement_data_tuple(
data: tuple[bytes, ...],
) -> BLEGAPAdvertisementTupleType:
"""Parse a tuple of raw advertisement data and return a tuple of BLEGAPAdvertisementTupleType.

The format of the tuple is:
(local_name, service_uuids, service_data, manufacturer_data, tx_power)

This is tightly coupled to bleak. If you are not using bleak
it is recommended to use parse_advertisement_data instead.

local_name: str | None
service_uuids: list[str]
service_data: dict[str, bytes]
manufacturer_data: dict[int, bytes]
tx_power: int | None
"""
return _uncached_parse_advertisement_data(data)
else:
parse_advertisement_data_tuple = lru_cache(maxsize=256)(
_uncached_parse_advertisement_data
)