Skip to content

Commit

Permalink
Improve unmarshall performance
Browse files Browse the repository at this point in the history
This change reduces the overhead of unmarshalling with the
goal of having bleak scanners not overwhelming the system.

See related issue:
hbldh/bleak#236 (comment)

```
(speed_up_unmarsh) % python3 bench/unmarshall.py
Unmarshalling 1000000 bluetooth rssi messages took 14.397348250000505 seconds
(master) % python3 bench/unmarshall.py
Unmarshalling 1000000 bluetooth rssi messages took 47.7756206660124 seconds
```
  • Loading branch information
bdraco committed Sep 11, 2022
1 parent 0eb0b7f commit 3282eed
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 316 deletions.
22 changes: 22 additions & 0 deletions bench/unmarshall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import io
import timeit

from dbus_next._private.unmarshaller import Unmarshaller

bluez_rssi_message = (
"6c04010134000000e25389019500000001016f00250000002f6f72672f626c75657a2f686369302f6465"
"765f30385f33415f46325f31455f32425f3631000000020173001f0000006f72672e667265656465736b"
"746f702e444275732e50726f7065727469657300030173001100000050726f706572746965734368616e"
"67656400000000000000080167000873617b73767d617300000007017300040000003a312e3400000000"
"110000006f72672e626c75657a2e446576696365310000000e0000000000000004000000525353490001"
"6e00a7ff000000000000"
)


def unmarhsall_bluez_rssi_message():
Unmarshaller(io.BytesIO(bytes.fromhex(bluez_rssi_message))).unmarshall()


count = 1000000
time = timeit.Timer(unmarhsall_bluez_rssi_message).timeit(count)
print(f"Unmarshalling {count} bluetooth rssi messages took {time} seconds")
5 changes: 5 additions & 0 deletions dbus_next/_private/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ class HeaderField(Enum):
SENDER = 7
SIGNATURE = 8
UNIX_FDS = 9


HEADER_NAME_MAP = {
field.value: field.name for field in HeaderField
}
Loading

0 comments on commit 3282eed

Please sign in to comment.