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

Optimize BlueZ device watchers and condition callbacks to avoid linear searches #1400

Merged
merged 25 commits into from
Aug 28, 2023

Conversation

bdraco
Copy link
Contributor

@bdraco bdraco commented Aug 25, 2023

needs #1399

@bdraco
Copy link
Contributor Author

bdraco commented Aug 28, 2023

rebasing..

@bdraco bdraco marked this pull request as ready for review August 28, 2023 15:19
Copy link
Collaborator

@dlech dlech left a comment

Choose a reason for hiding this comment

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

Thanks for PR. Did you measure the performance difference?

bleak/backends/bluezdbus/manager.py Outdated Show resolved Hide resolved
bleak/backends/bluezdbus/manager.py Outdated Show resolved Hide resolved
bleak/backends/bluezdbus/manager.py Show resolved Hide resolved
bleak/backends/bluezdbus/manager.py Outdated Show resolved Hide resolved
bleak/backends/bluezdbus/manager.py Outdated Show resolved Hide resolved
):
def _wait_condition_callback(changed: Dict[str, Any]) -> None:
"""Callback for when a property changes."""
if changed.get(property_name) == property_value:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there could be some potential issues here with None == False. As mentioned in another comment, might be simpler to just leave it the way it was without passing the changed arg.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the incoming to pull from self_interface.get so None will still work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left it passing the value because I always worry about race conditions when we have to look it up again. Not a problem right now but could be in the future if something gets refactored

bleak/backends/bluezdbus/manager.py Outdated Show resolved Hide resolved
@bdraco
Copy link
Contributor Author

bdraco commented Aug 28, 2023

Thanks for PR. Did you measure the performance difference?

It was ~18% faster with my locks when I have a lot of devices but its going to vary widely depending on how many devices since its a Time complexity issue

@bdraco bdraco marked this pull request as draft August 28, 2023 16:41
@bdraco
Copy link
Contributor Author

bdraco commented Aug 28, 2023

marking as draft while I clean up and retest

@bdraco
Copy link
Contributor Author

bdraco commented Aug 28, 2023

profile here
_parse_msg
Screenshot 2023-08-28 at 12 17 43 PM

Comment on lines 976 to 978
self._run_advertisement_callbacks(
message.path, cast(Device1, self_interface), changed.keys()
device_path, cast(Device1, self_interface), changed.keys()
)
Copy link
Contributor Author

@bdraco bdraco Aug 28, 2023

Choose a reason for hiding this comment

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

This could be indexed by adapter when the user has multiple bluetooth adapters it has to reject callbacks for the wrong adapter. But that should be another PR in the future (maybe not needed though since not many people will have multiple adapters)

@bdraco bdraco marked this pull request as ready for review August 28, 2023 17:25
bleak/backends/bluezdbus/manager.py Show resolved Hide resolved
bleak/backends/bluezdbus/manager.py Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
poetry.lock Outdated Show resolved Hide resolved
@bdraco
Copy link
Contributor Author

bdraco commented Aug 28, 2023

retesting on production one more time just to be sure

@bdraco
Copy link
Contributor Author

bdraco commented Aug 28, 2023

all good 👍

@bdraco
Copy link
Contributor Author

bdraco commented Aug 28, 2023

Thanks for the review. Will be very happy to get a new release so we can get esphome/aioesphomeapi#528 sorted out

@dlech dlech merged commit 256a5be into hbldh:develop Aug 28, 2023
@dlech
Copy link
Collaborator

dlech commented Aug 28, 2023

Hopefully we can get a release by the end of the week. There is still a Windows issue I would like to address and also prepare for Python 3.12.

@bdraco bdraco deleted the opt branch August 28, 2023 20:16
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.

2 participants