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: speed up gap parser with a memory view #35

Merged
merged 2 commits into from
Nov 5, 2023
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
1 change: 1 addition & 0 deletions src/bluetooth_data_tools/gap.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ cpdef parse_advertisement_data_tuple(cython.tuple data)
@cython.locals(
gap_data=cython.bytes,
gap_value=cython.bytes,
gap_view="const unsigned char [:]",
gap_type_num=cython.uint,
total_length=cython.uint,
length=cython.uint,
Expand Down
7 changes: 4 additions & 3 deletions src/bluetooth_data_tools/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,19 @@ def _uncached_parse_advertisement_data(
tx_power: int | None = None

for gap_data in data:
gap_view = gap_data
offset = 0
total_length = len(gap_data)
while offset < total_length:
try:
length = gap_data[offset]
length = gap_view[offset]
if not length:
if offset + 2 < total_length:
# Maybe zero padding
offset += 1
continue
break
gap_type_num = gap_data[offset + 1]
gap_type_num = gap_view[offset + 1]
if not gap_type_num:
break
start = offset + 2
Expand All @@ -236,7 +237,7 @@ def _uncached_parse_advertisement_data(
continue

offset += 1 + length
if len(gap_value) == 0:
if end - start == 0:
continue
if gap_type_num == TYPE_SHORT_LOCAL_NAME and local_name is None:
local_name = gap_value.decode("utf-8", "replace")
Expand Down