From f5865f135750ea7ffe775657ad31e1fc3d4ee285 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 14 Mar 2026 17:49:47 -1000 Subject: [PATCH] perf: replace set membership checks with chained == in GAP parser In Cython-compiled code, `x in {A, B}` creates a frozenset lookup that benchmarks ~2.4x slower than `x == A or x == B` per check. This fires on every GAP record in the parse loop. --- src/bluetooth_data_tools/gap.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/bluetooth_data_tools/gap.py b/src/bluetooth_data_tools/gap.py index a37c6d7..2a3ce27 100644 --- a/src/bluetooth_data_tools/gap.py +++ b/src/bluetooth_data_tools/gap.py @@ -221,10 +221,10 @@ def _uncached_parse_advertisement_bytes( manufacturer_data[gap_data[start] | (gap_data[start + 1] << 8)] = gap_data[ splice_pos:end ] - elif gap_type_num in { - TYPE_16BIT_SERVICE_UUID_COMPLETE, - TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE, - }: + elif ( + gap_type_num == TYPE_16BIT_SERVICE_UUID_COMPLETE + or gap_type_num == TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE + ): if service_uuids is _EMPTY_SERVICE_UUIDS: service_uuids = [] # Parse multiple 16-bit UUIDs (each is 2 bytes) @@ -233,10 +233,10 @@ def _uncached_parse_advertisement_bytes( service_uuids.append( _cached_uint16_bytes_as_uuid(gap_data[i : i + 2]) ) - elif gap_type_num in { - TYPE_32BIT_SERVICE_UUID_COMPLETE, - TYPE_32BIT_SERVICE_UUID_MORE_AVAILABLE, - }: + elif ( + gap_type_num == TYPE_32BIT_SERVICE_UUID_COMPLETE + or gap_type_num == TYPE_32BIT_SERVICE_UUID_MORE_AVAILABLE + ): if service_uuids is _EMPTY_SERVICE_UUIDS: service_uuids = [] # Parse multiple 32-bit UUIDs (each is 4 bytes) @@ -245,10 +245,10 @@ def _uncached_parse_advertisement_bytes( service_uuids.append( _cached_uint32_bytes_as_uuid(gap_data[i : i + 4]) ) - elif gap_type_num in { - TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE, - TYPE_128BIT_SERVICE_UUID_COMPLETE, - }: + elif ( + gap_type_num == TYPE_128BIT_SERVICE_UUID_COMPLETE + or gap_type_num == TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE + ): if service_uuids is _EMPTY_SERVICE_UUIDS: service_uuids = [] service_uuids.append(_cached_uint128_bytes_as_uuid(gap_data[start:end]))