Skip to content

Commit

Permalink
feat: speed up gap parser with a memory view (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Nov 5, 2023
1 parent 14a5302 commit 35e132f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
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

0 comments on commit 35e132f

Please sign in to comment.