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

feat: add optimized parser for InterfacesRemoved #123

Merged
merged 3 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions bench/unmarshall_interfaces_removed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import io
import timeit

from dbus_fast._private.unmarshaller import Unmarshaller

# cythonize -X language_level=3 -a -i src/dbus_fast/_private/unmarshaller.py


bluez_interfaces_removed_message = (
b'l\4\1\1\222\0\0\0\377@-\0~\0\0\0\1\1o\0\1\0\0\0/\0\0\0\0\0\0\0\2\1s\0"\0\0\0'
b"org.freedesktop.DBus.ObjectManager\0\0\0\0\0\0\3\1s\0\21\0\0\0InterfacesRemoved"
b"\0\0\0\0\0\0\0\10\1g\0\3oas\0\0\0\0\0\0\0\0\7\1s\0\5\0\0\0:1.12\0\0\0%\0\0\0"
b"/org/bluez/hci0/dev_5F_13_47_38_26_55\0\0\0b\0\0\0\37\0\0\0org.freedesktop.DBus"
b".Properties\0#\0\0\0org.freedesktop.DBus.Introspectable\0\21\0\0\0org.bluez.Dev"
b"ice1\0"
)


stream = io.BytesIO(bluez_interfaces_removed_message)
unmarshaller = Unmarshaller(stream)


def unmarshall_interfaces_removed_message():
stream.seek(0)
unmarshaller.reset()
unmarshaller.unmarshall()


count = 3000000
time = timeit.Timer(unmarshall_interfaces_removed_message).timeit(count)
print(f"Unmarshalling {count} bluetooth InterfacesRemoved messages took {time} seconds")
2 changes: 2 additions & 0 deletions src/dbus_fast/_private/unmarshaller.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ cdef object SIGNATURE_TREE_A_SV_TYPES_0
cdef object SIGNATURE_TREE_SA_SV_AS
cdef object SIGNATURE_TREE_SA_SV_AS_TYPES_1
cdef object SIGNATURE_TREE_SA_SV_AS_TYPES_2
cdef object SIGNATURE_TREE_OAS
cdef object SIGNATURE_TREE_OAS_TYPES_1
cdef object SIGNATURE_TREE_OA_SA_SV
cdef object SIGNATURE_TREE_OA_SA_SV_TYPES_1
cdef object SIGNATURE_TREE_AY
Expand Down
21 changes: 17 additions & 4 deletions src/dbus_fast/_private/unmarshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
SIGNATURE_TREE_A_SV = get_signature_tree("a{sv}")
SIGNATURE_TREE_A_SV_TYPES_0 = SIGNATURE_TREE_A_SV.types[0]

SIGNATURE_TREE_OAS = get_signature_tree("oas")
SIGNATURE_TREE_OAS_TYPES_1 = SIGNATURE_TREE_OAS.types[1]

SIGNATURE_TREE_AY_TYPES_0 = SIGNATURE_TREE_AY.types[0]
SIGNATURE_TREE_A_QV = get_signature_tree("a{qv}")
SIGNATURE_TREE_A_QV_TYPES_0 = SIGNATURE_TREE_A_QV.types[0]
Expand Down Expand Up @@ -417,8 +420,9 @@ def _read_array(self, type_: SignatureType) -> Iterable[Any]:
if token == "{":
result_dict = {}
beginning_pos = self._pos
child_0 = child_type.children[0]
child_1 = child_type.children[1]
children = child_type.children
child_0 = children[0]
child_1 = children[1]
child_0_token = child_0.token
child_1_token = child_1.token
# Strings with variant values are the most common case
Expand All @@ -442,7 +446,6 @@ def _read_array(self, type_: SignatureType) -> Iterable[Any]:
else:
reader_1 = self._readers[child_1_token]
reader_0 = self._readers[child_0_token]

while self._pos - beginning_pos < array_length:
self._pos += -self._pos & 7 # align 8
key = reader_0(self, child_0)
Expand All @@ -455,7 +458,11 @@ def _read_array(self, type_: SignatureType) -> Iterable[Any]:

result_list = []
beginning_pos = self._pos
reader = self._readers[child_type.token]
if token in "os":
while self._pos - beginning_pos < array_length:
result_list.append(self._read_string_unpack())
return result_list
reader = self._readers[token]
while self._pos - beginning_pos < array_length:
result_list.append(reader(self, child_type))
return result_list
Expand Down Expand Up @@ -568,6 +575,12 @@ def _read_body(self) -> None:
self._read_string_unpack(),
self._read_array(SIGNATURE_TREE_OA_SA_SV_TYPES_1),
]
elif signature == "oas":
tree = SIGNATURE_TREE_OAS
body = [
self._read_string_unpack(),
self._read_array(SIGNATURE_TREE_OAS_TYPES_1),
]
else:
tree = get_signature_tree(signature)
body = [self._readers[t.token](self, t) for t in tree.types]
Expand Down
43 changes: 43 additions & 0 deletions tests/test_marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,49 @@ def test_unmarshall_bluez_interfaces_added_message():
]


def test_unmarshall_bluez_interfaces_removed_message():
bluez_interfaces_removed_message = (
b'l\4\1\1\222\0\0\0\377@-\0~\0\0\0\1\1o\0\1\0\0\0/\0\0\0\0\0\0\0\2\1s\0"\0\0\0'
b"org.freedesktop.DBus.ObjectManager\0\0\0\0\0\0\3\1s\0\21\0\0\0InterfacesRemoved"
b"\0\0\0\0\0\0\0\10\1g\0\3oas\0\0\0\0\0\0\0\0\7\1s\0\5\0\0\0:1.12\0\0\0%\0\0\0"
b"/org/bluez/hci0/dev_5F_13_47_38_26_55\0\0\0b\0\0\0\37\0\0\0org.freedesktop.DBus"
b".Properties\0#\0\0\0org.freedesktop.DBus.Introspectable\0\21\0\0\0org.bluez.Dev"
b"ice1\0"
)

stream = io.BytesIO(bluez_interfaces_removed_message)
unmarshaller = Unmarshaller(stream)
assert unmarshaller.unmarshall()
message = unmarshaller.message
assert message is not None
assert message.body == [
"/org/bluez/hci0/dev_5F_13_47_38_26_55",
[
"org.freedesktop.DBus.Properties",
"org.freedesktop.DBus.Introspectable",
"org.bluez.Device1",
],
]
assert message.sender == ":1.12"
assert message.path == "/"
assert message.interface == "org.freedesktop.DBus.ObjectManager"
assert message.member == "InterfacesRemoved"
assert message.signature == "oas"
assert message.message_type == MessageType.SIGNAL
assert message.flags == MessageFlag.NO_REPLY_EXPECTED
assert message.serial == 2965759
assert message.destination is None
unpacked = unpack_variants(message.body)
assert unpacked == [
"/org/bluez/hci0/dev_5F_13_47_38_26_55",
[
"org.freedesktop.DBus.Properties",
"org.freedesktop.DBus.Introspectable",
"org.bluez.Device1",
],
]


def test_ay_buffer():
body = [bytes(10000)]
msg = Message(path="/test", member="test", signature="ay", body=body)
Expand Down