perf(utils): cache human_readable_name#266
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #266 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 6 6
Lines 266 267 +1
Branches 42 42
=========================================
+ Hits 266 267 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Merging this PR will improve performance by 53.84%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | test_human_readable_name_local_only |
22.5 µs | 14.8 µs | +51.77% |
| ⚡ | test_human_readable_name_with_name |
22.7 µs | 14.6 µs | +55.94% |
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/cache-human-readable-name (9edf6c8) with main (967cc9f)
bdraco
left a comment
There was a problem hiding this comment.
Benchmarks must be moved to a separate pr ahead of this pr
PR Review — perf(utils): cache human_readable_nameThe change itself is a minimal, correct lru_cache wrap around a pure function — behavior is preserved, key is hashable, maxsize=512 is reasonable for HA-style peer counts, and codecov shows 100% coverage. However, @bdraco has explicitly requested CHANGES_REQUESTED with the direction 'Benchmarks must be moved to a separate PR ahead of this PR.' The PR currently bundles benchmark additions ( Checklist
SummaryThe change itself is a minimal, correct lru_cache wrap around a pure function — behavior is preserved, key is hashable, maxsize=512 is reasonable for HA-style peer counts, and codecov shows 100% coverage. However, @bdraco has explicitly requested CHANGES_REQUESTED with the direction 'Benchmarks must be moved to a separate PR ahead of this PR.' The PR currently bundles benchmark additions ( |
Acknowledged — splitting this into two PRs. Will open a benchmarks-only PR first (both |
Rebase with requested adjustmentsBranch Changes applied
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
adb3a16 to
3ae23a7
Compare
Per-device name formatting in advertisement callbacks repeats the same triplet many times: HA-style scanners build a label per advert and the arguments are stable per device. Adding lru_cache(512) skips both the short_address `replace().split()` work and the surrounding f-string on hit. Local microbench (CPython 3.13, 1M calls, repeated input): - baseline: 1143 ns/call - cached: 260 ns/call (~4.4x faster on hit) Cache size 512 covers the realistic peer count for a HA scanner without unbounded growth; signature stays str-equality keyed so misses pay only the regular hash cost.
Rebase with requested adjustmentsBranch Changes applied
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
b0d9a54 to
9edf6c8
Compare
What
Wrap
human_readable_namewithlru_cache(maxsize=512).Why
HA-style scanners format the same
(name, local_name, address)triplet repeatedly per device. Each uncached call pays for the surrounding f-string plus the underlyingshort_addressreplace().split()work, even though the inputs are stable per peer.How
human_readable_namewithlru_cache(maxsize=512)— enough headroom for realistic peer counts on a HA scanner without unbounded growth.short_addressentirely, so the rejection of a nativeshort_addresshelper (perf(utils): add native short_address with hyphen and unseparated forms #241 — "this path isn't hot") is not contradicted: this PR adds no native code and only short-circuits the wrapper that is called per advert.Testing
Existing
tests/test_utils.py::test_human_readable_namestill passes. Added codspeed + pytest-benchmark coverage intests/benchmarks/test_human_readable_name.pyandbench/test_human_readable_name.py.Local microbench (CPython 3.13, 1M calls, steady-state input):
🤖 Generated with Claude Code
Quality Report
Changes: 3 files changed, 33 insertions(+)
Code scan: clean
Tests: failed (FAILED)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline