Skip to content

Commit

Permalink
feat: speed up Message creation and callbacks (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Aug 2, 2023
1 parent 3e6560d commit 04d6451
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/dbus_fast/_private/unmarshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def build_simple_parsers(
except ImportError:
from ._cython_compat import FAKE_CYTHON as cython


#
# Alignment padding is handled with the following formula below
#
Expand Down
4 changes: 2 additions & 2 deletions src/dbus_fast/aio/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def _make_method_handler(self, interface, method):
if not asyncio.iscoroutinefunction(method.fn):
return super()._make_method_handler(interface, method)

def handler(msg, send_reply):
def _coro_method_handler(msg, send_reply):
def done(fut):
with send_reply:
result = fut.result()
Expand All @@ -430,7 +430,7 @@ def done(fut):
fut = asyncio.ensure_future(method.fn(interface, *args))
fut.add_done_callback(done)

return handler
return _coro_method_handler

async def _auth_readline(self) -> str:
buf = b""
Expand Down
10 changes: 5 additions & 5 deletions src/dbus_fast/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
)

REQUIRED_FIELDS = {
MessageType.METHOD_CALL: ("path", "member"),
MessageType.SIGNAL: ("path", "member", "interface"),
MessageType.ERROR: ("error_name", "reply_serial"),
MessageType.METHOD_RETURN: ("reply_serial",),
MessageType.METHOD_CALL.value: ("path", "member"),
MessageType.SIGNAL.value: ("path", "member", "interface"),
MessageType.ERROR.value: ("error_name", "reply_serial"),
MessageType.METHOD_RETURN.value: ("reply_serial",),
}

HEADER_PATH = HeaderField.PATH.value
Expand Down Expand Up @@ -146,7 +146,7 @@ def __init__(
if self.error_name is not None:
assert_interface_name_valid(self.error_name) # type: ignore[arg-type]

required_fields = REQUIRED_FIELDS.get(self.message_type)
required_fields = REQUIRED_FIELDS.get(self.message_type.value)
if not required_fields:
raise InvalidMessageError(f"got unknown message type: {self.message_type}")
for field in required_fields:
Expand Down
60 changes: 33 additions & 27 deletions src/dbus_fast/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,69 +881,75 @@ def _process_message(self, msg: _Message) -> None:
def _make_method_handler(
self, interface: ServiceInterface, method: _Method
) -> Callable[[Message, Callable[[Message], None]], None]:
def handler(msg: Message, send_reply: Callable[[Message], None]) -> None:
args = ServiceInterface._msg_body_to_args(msg)
result = method.fn(interface, *args)
body, fds = ServiceInterface._fn_result_to_body(
result,
signature_tree=method.out_signature_tree,
replace_fds=self._negotiate_unix_fd,
method_fn = method.fn
out_signature_tree = method.out_signature_tree
negotiate_unix_fd = self._negotiate_unix_fd
out_signature = method.out_signature
message_type_method_return = MessageType.METHOD_RETURN
msg_body_to_args = ServiceInterface._msg_body_to_args
fn_result_to_body = ServiceInterface._fn_result_to_body

def _callback_method_handler(
msg: Message, send_reply: Callable[[Message], None]
) -> None:
body, fds = fn_result_to_body(
method_fn(interface, *msg_body_to_args(msg)),
signature_tree=out_signature_tree,
replace_fds=negotiate_unix_fd,
)
send_reply(
Message(
message_type=MessageType.METHOD_RETURN,
message_type=message_type_method_return,
reply_serial=msg.serial,
destination=msg.sender,
signature=method.out_signature,
signature=out_signature,
body=body,
unix_fds=fds,
)
)

return handler
return _callback_method_handler

def _find_message_handler(
self, msg
) -> Optional[Callable[[Message, Callable], None]]:
handler: Optional[Callable[[Message, Callable], None]] = None

if (
msg.interface == "org.freedesktop.DBus.Introspectable"
and msg.member == "Introspect"
and msg.signature == ""
):
handler = self._default_introspect_handler
return self._default_introspect_handler

elif msg.interface == "org.freedesktop.DBus.Properties":
handler = self._default_properties_handler
if msg.interface == "org.freedesktop.DBus.Properties":
return self._default_properties_handler

elif msg.interface == "org.freedesktop.DBus.Peer":
if msg.interface == "org.freedesktop.DBus.Peer":
if msg.member == "Ping" and msg.signature == "":
handler = self._default_ping_handler
return self._default_ping_handler
elif msg.member == "GetMachineId" and msg.signature == "":
handler = self._default_get_machine_id_handler
elif (
return self._default_get_machine_id_handler

if (
msg.interface == "org.freedesktop.DBus.ObjectManager"
and msg.member == "GetManagedObjects"
):
handler = self._default_get_managed_objects_handler
return self._default_get_managed_objects_handler

elif msg.path:
for interface in self._path_exports.get(msg.path, []):
msg_path = msg.path
if msg_path:
for interface in self._path_exports.get(msg_path, []):
for method in ServiceInterface._get_methods(interface):
if method.disabled:
continue

if (
msg.interface == interface.name
and msg.member == method.name
and msg.signature == method.in_signature
):
handler = ServiceInterface._get_handler(interface, method, self)
break
if handler:
break
return ServiceInterface._get_handler(interface, method, self)

return handler
return None

def _default_introspect_handler(
self, msg: Message, send_reply: Callable[[Message], None]
Expand Down
1 change: 0 additions & 1 deletion src/dbus_fast/proxy_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def __init__(
introspection: intr.Interface,
bus: "message_bus.BaseMessageBus",
) -> None:

self.bus_name = bus_name
self.path = path
self.introspection = introspection
Expand Down
4 changes: 4 additions & 0 deletions src/dbus_fast/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def is_member_name_valid(member: str) -> bool:
return True


@lru_cache(maxsize=32)
def assert_bus_name_valid(name: str) -> None:
"""Raise an error if this is not a valid bus name.
Expand All @@ -150,6 +151,7 @@ def assert_bus_name_valid(name: str) -> None:
raise InvalidBusNameError(name)


@lru_cache(maxsize=1024)
def assert_object_path_valid(path: str) -> None:
"""Raise an error if this is not a valid object path.
Expand All @@ -165,6 +167,7 @@ def assert_object_path_valid(path: str) -> None:
raise InvalidObjectPathError(path)


@lru_cache(maxsize=32)
def assert_interface_name_valid(name: str) -> None:
"""Raise an error if this is not a valid interface name.
Expand All @@ -180,6 +183,7 @@ def assert_interface_name_valid(name: str) -> None:
raise InvalidInterfaceNameError(name)


@lru_cache(maxsize=512)
def assert_member_name_valid(member: str) -> None:
"""Raise an error if this is not a valid member name.
Expand Down

0 comments on commit 04d6451

Please sign in to comment.