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

Bluez: improve get services performance #966

Merged
merged 2 commits into from
Aug 29, 2022

Conversation

dlech
Copy link
Collaborator

@dlech dlech commented Aug 26, 2022

Fixes #927.

See commit message for details.

@bdraco can you test and see if this actually makes a measurable difference in performance?

dlech added 2 commits August 26, 2022 16:56
This moves the D-Bus interface property dict types to the defs module.
This will help avoid circular imports of the manager module.
This adds maps to improve performance when calling get_services().

Previously we had to enumerate all BlueZ D-Bus objects for each service,
characteristic and descriptor. When there were many devices, this was
a performance bottleneck.

Instead, we can create a map of the D-Bus object tree as new interfaces
appear, then use this map as an efficient way to find the child D-Bus
object paths in the GATT tree.

Fixes #927.
self._descriptor_map.setdefault(
desc_props["Characteristic"], set()
).add(obj_path)

Copy link
Contributor

Choose a reason for hiding this comment

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

Could make the if below an elif

@bdraco
Copy link
Contributor

bdraco commented Aug 26, 2022

This will definitely solve the performance issue.

@bdraco
Copy link
Contributor

bdraco commented Aug 26, 2022

I did some testing with this and it seems the scanner is killed off when ever an active connection is being made. Not sure if its a regression on develop

@bdraco
Copy link
Contributor

bdraco commented Aug 26, 2022

I double checking that on a different system, it could be a hardware issue

@bdraco
Copy link
Contributor

bdraco commented Aug 26, 2022

Switched to a known good system and testing now

@bdraco
Copy link
Contributor

bdraco commented Aug 26, 2022

Going to take a while to put batteries in enough bluetooth devices to get to the test level I had before

@bdraco
Copy link
Contributor

bdraco commented Aug 26, 2022

2022-08-26 18:45:51.811 DEBUG (MainThread) [bleak_retry_connector] 60-55-F9-3C-34-DA (60:55:F9:3C:34:DA) - /org/bluez/hci1/dev_60_55_F9_3C_34_DA: Connecting (attempt: 1, last rssi: -47)
2022-08-26 18:45:53.498 DEBUG (MainThread) [bleak_retry_connector] 60-55-F9-3C-34-DA (60:55:F9:3C:34:DA) - /org/bluez/hci1/dev_60_55_F9_3C_34_DA: Connected (attempt: 1, last rssi: -47)

connecting to a switchbot seems faster. digging though the callgrind now

@bdraco
Copy link
Contributor

bdraco commented Aug 26, 2022

Connect time with services cached -- its a layered one top version (that overloads get_services https://github.com/Bluetooth-Devices/bleak-retry-connector/blob/acbee3824e4200f4acb43851ff90ccfd60ecb575/src/bleak_retry_connector/__init__.py#L146) of #923

2022-08-26 18:47:08.666 DEBUG (MainThread) [bleak_retry_connector] WoPlug (60:55:F9:3C:34:DA) - /org/bluez/hci1/dev_60_55_F9_3C_34_DA: Connecting (attempt: 1, last rssi: -43)
2022-08-26 18:47:08.860 DEBUG (MainThread) [bleak_retry_connector] Cached services found: <bleak.backends.service.BleakGATTServiceCollection object at 0xffff9b3e9000>
2022-08-26 18:47:08.860 DEBUG (MainThread) [bleak_retry_connector] WoPlug (60:55:F9:3C:34:DA) - /org/bluez/hci1/dev_60_55_F9_3C_34_DA: Connected (attempt: 1, last rssi: -43)

@bdraco
Copy link
Contributor

bdraco commented Aug 26, 2022

get_services

The overhead just disappears with this change

@bdraco
Copy link
Contributor

bdraco commented Aug 28, 2022

Been testing with this in production and the performance improvement is noticeable

@bdraco
Copy link
Contributor

bdraco commented Aug 28, 2022

This has been a really nice change. I added support for some switchbot lights in home-assistant/core#77430 and the performance improvement was quite noticeable on first connect

@dlech dlech merged commit f343136 into develop Aug 29, 2022
@dlech
Copy link
Collaborator Author

dlech commented Aug 29, 2022

Thanks for testing.

@dlech dlech deleted the bluez-get-services-performance branch August 29, 2022 16:10
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.

Linear searching is a performance bottleneck in BlueZManager.get_services with many devices
2 participants