perf(gap): inline 1-byte signed decode for TX Power Level#249
Conversation
The TX_POWER branch is gated on `end - start == 1`, so the cached `int.from_bytes(..., signed=True)` indirection only ever decoded a single byte. Replace the slice + lru_cache + partial.from_bytes call chain with a direct unsigned-char read and signed-fold (`b - 256 if b >= 128 else b`), typed as `unsigned char` for Cython. Drops `_cached_from_bytes_signed` and the `from_bytes_signed` partial, along with their .pxd declarations. Behaviour is unchanged — the spec-compliance gate added in Bluetooth-Devices#226 already enforces a 1-byte payload. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #249 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 6 6
Lines 254 255 +1
Branches 40 40
=========================================
+ Hits 254 255 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Merging this PR will improve performance by 4.91%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | test_parse_advertisement_data_tuple_bytes |
73.4 µs | 70 µs | +4.92% |
| ⚡ | test_parse_advertisement_data_tuple_uncached |
75.2 µs | 71.7 µs | +4.89% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing bluetoothbot:koan/inline-tx-power-decode (cf9a85a) with main (3521434)
PR Review — perf(gap): inline 1-byte signed decode for TX Power LevelClean micro-optimization. The inline signed-fold ( Checklist
SummaryClean micro-optimization. The inline signed-fold ( Automated review by Kōan111974f |
Rebase with requested adjustmentsBranch StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
Rebase with requested adjustmentsBranch Changes applied
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
What
Drop the
lru_cache(int.from_bytes(..., signed=True))indirection for the TX Power Level AD branch. Decode the single signed byte inline with a typedunsigned charread and a signed-fold.Why
The TX_POWER branch is already gated on
end - start == 1(added in #226 for spec compliance, Core Spec Vol 3 Part C §11). With that gate in place,_cached_from_bytes_signed(gap_data[start:end])only ever decodes a one-byte slice — the slice allocation, hash, and cache lookup are pure overhead. A two-op signed-fold compiles to tight C in Cython.How
from_bytes_signedpartial and the_cached_from_bytes_signedlru_cache wrapper fromgap.py.tx_power_byte = gap_data[start]; tx_power = tx_power_byte - 256 if tx_power_byte >= 128 else tx_power_byte.tx_power_byteasunsigned charingap.pxdso Cython folds the branch into native arithmetic.cdef object from_bytes_signed/cdef object _cached_from_bytes_signedlines fromgap.pxd.Behaviour is unchanged: a one-byte
0xCEpayload still decodes to-50, a multi-byte payload is still rejected (tx_power is None).Testing
pytest tests/ --ignore=tests/benchmarks— 87 passed.ruff check src/— clean.gap.py— no errors.test_parse_advertisement_data_tx_power_single_byte(-50) andtest_parse_advertisement_data_tx_power_multibyte_rejected(None) cover both branches.Quality Report
Changes: 2 files changed, 5 insertions(+), 7 deletions(-)
Code scan: clean
Tests: failed (FAILED)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline