Skip to content

perf(history): skip props lookup work for non-matching source_adapter#281

Draft
bluetoothbot wants to merge 1 commit into
Bluetooth-Devices:mainfrom
bluetoothbot:koan/history-filter-adapter-early
Draft

perf(history): skip props lookup work for non-matching source_adapter#281
bluetoothbot wants to merge 1 commit into
Bluetooth-Devices:mainfrom
bluetoothbot:koan/history-filter-adapter-early

Conversation

@bluetoothbot

@bluetoothbot bluetoothbot commented May 27, 2026

Copy link
Copy Markdown
Contributor

What

Move the source_adapter filter in load_history_from_managed_objects to right after the org.bluez.Device1 props existence check, before reading address/rssi and running the rssi dedup.

Why

When a caller passes source_adapter="hci0", every managed-object entry under /org/bluez/hci1/... (or any non-matching adapter) currently runs through:

  • props["Address"] lookup
  • props.get("RSSI", MIN_RSSI) lookup
  • history.get(address) lookup + rssi comparison

…only to be discarded at the source_adapter check. With one BlueZ instance commonly exposing several adapters and dozens-to-hundreds of devices each, this is per-discarded-device work on a hot path that is otherwise cheap.

Bonus: defends against KeyError on Address/Alias for malformed entries under unrelated adapters — those can't crash a single-adapter call anymore.

How

The split + filter moves up four lines. No new allocations: path_str.split("/") already happens once per accepted device, now happens once per Device1 entry instead. Non-Device1 paths still short-circuit before the split.

Behavior

Unchanged. When source_adapter is None, the filter is a no-op. When set, entries from other adapters were never inserted into history anyway, so they couldn't affect the rssi dedup — moving the filter just decides "skip" earlier.

Testing

poetry run pytest tests/ — 51 passed (including test_history_with_rssi_check which exercises both the source_adapter=None and source_adapter="hci1" paths). ruff check clean.


Quality Report

Changes: 1 file changed, 4 insertions(+), 6 deletions(-)

Code scan: clean

Tests: passed (1
PASSED)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

When source_adapter is set, move the adapter filter to right after the
Device1 props existence check, before reading address/rssi and running
the rssi dedup. Avoids per-non-matching dict lookups on the hot path and
defends against KeyError on Address/Alias for malformed entries under
unrelated adapters.

No behavior change for callers — when source_adapter is None the filter
is a no-op, and when set, entries from other adapters never affect the
rssi dedup since they were never inserted anyway.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented May 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.61%. Comparing base (ece711c) to head (69970de).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #281      +/-   ##
==========================================
- Coverage   99.61%   99.61%   -0.01%     
==========================================
  Files          14       14              
  Lines         522      521       -1     
  Branches       48       48              
==========================================
- Hits          520      519       -1     
  Partials        2        2              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant