Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index BlueZ advertisement_callbacks by adapter #1632

Merged
merged 1 commit into from
Aug 22, 2024

Conversation

bdraco
Copy link
Contributor

@bdraco bdraco commented Aug 21, 2024

  • Instead of doing a linear search of all the callbacks to find the one for the adapter, store them in a dict so the adapter path can be looked up

  • Remove unused arg from _run_advertisement_callbacks

  • If the pull request adds functionality, the docs should be updated.

  • Modify the CHANGELOG.rst, describing your changes as is specified by the
    guidelines in that document.

  • The pull request should work for Python 3.8+ on the following platforms:

    • Windows 10, version 16299 (Fall Creators Update) and greater
    • Linux distributions with BlueZ >= 5.43
    • OS X / macOS >= 10.11
  • Squash all your commits on your PR branch, if the commits are not solving
    different problems and you are committing them in the same PR. In that case,
    consider making several PRs instead.

  • Feel free to add your name as a contributor to the AUTHORS.rst file!

@bdraco bdraco force-pushed the cleanup_manager branch 4 times, most recently from cbe6f09 to 3902d34 Compare August 21, 2024 03:59
continue

adapter_path = device["Adapter"]
for callback in self._advertisement_callbacks[adapter_path]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Average time complexity should now be: O(callbacks) instead of O(adapters * callbacks)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have before and after flamegraphs or something like that showing the difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bdraco bdraco marked this pull request as ready for review August 22, 2024 00:44
bleak/backends/bluezdbus/manager.py Outdated Show resolved Hide resolved
continue

adapter_path = device["Adapter"]
for callback in self._advertisement_callbacks[adapter_path]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have before and after flamegraphs or something like that showing the difference?

@bdraco
Copy link
Contributor Author

bdraco commented Aug 22, 2024

#1632 (comment)

Running with 6 Bluetooth adapters its ~28% less time spent in _run_advertisement_callbacks processing ~8000 messages per minute as measured with cProfile.

It can be roughly benchmarked with

import timeit
from collections import defaultdict


def callback():
    pass


advertisement_callbacks: defaultdict[str, list] = defaultdict(list)

test_adapter = "/org/bluez/hci9"

for i in range(10):
    advertisement_callbacks[f"/org/bluez/hci{i}"] = [callback]

advertisement_callbacks_orig = [
    (callback, adapter_path)
    for callback, adapter_path in advertisement_callbacks.items()
]


def new():
    for callback in advertisement_callbacks[test_adapter]:
        pass


def orig():
    for callback, adapter_path in advertisement_callbacks_orig:
        if adapter_path != test_adapter:
            continue


print(timeit.timeit(orig, number=1000000))
print(timeit.timeit(new, number=1000000))

orig

0.1742814159952104

new

0.042215333989588544

- Instead of doing a linear search of all the callbacks
  to find the one for the adapter, store them in a dict
  so the adapter path can be looked up
- Remove unused arg from _run_advertisement_callbacks

Co-authored-by: David Lechner <david@lechnology.com>
@dlech dlech merged commit 7130c40 into hbldh:develop Aug 22, 2024
13 checks passed
@dlech
Copy link
Collaborator

dlech commented Aug 22, 2024

Thanks!

@bdraco bdraco deleted the cleanup_manager branch August 22, 2024 13:28
@bdraco
Copy link
Contributor Author

bdraco commented Aug 22, 2024

Thanks

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.

3 participants