Skip to content

Commit

Permalink
feat: small speed up to processing bluez passive data
Browse files Browse the repository at this point in the history
This is a ~5% speed up
  • Loading branch information
bdraco committed Aug 9, 2023
1 parent 642a5e8 commit b858d95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/dbus_fast/message_bus.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ cdef object assert_bus_name_valid

cdef _expects_reply(Message msg)

cdef _swallow_reply(Message msg)

cdef class SendReply:

cdef object _bus
Expand Down
14 changes: 10 additions & 4 deletions src/dbus_fast/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@


def _expects_reply(msg: _Message) -> bool:
"""Whether a message expects a reply."""
return not (msg.flags.value & NO_REPLY_EXPECTED_VALUE)


def _swallow_unexpected_reply(msg: _Message) -> None:
"""Swallow a reply if it's not expected."""


class SendReply:
"""A context manager to send a reply to a message."""

Expand All @@ -50,8 +55,7 @@ def __enter__(self):
return self

def __call__(self, reply: Message) -> None:
if _expects_reply(self._msg):
self._bus.send(reply)
self._bus.send(reply)

def _exit(
self,
Expand Down Expand Up @@ -855,9 +859,11 @@ def _process_message(self, msg: _Message) -> None:
if msg.message_type is MESSAGE_TYPE_CALL:
if not handled:
handler = self._find_message_handler(msg)
if not _expects_reply(msg):
handler(msg, _swallow_unexpected_reply)
return

send_reply = SendReply(self, msg)

with send_reply:
if handler:
handler(msg, send_reply)
Expand Down Expand Up @@ -913,7 +919,7 @@ def _callback_method_handler(

def _find_message_handler(
self, msg: _Message
) -> Optional[Callable[[Message, Callable], None]]:
) -> Optional[Callable[[Message, Callable[[Message], None]], None]]:
if (
msg.interface == "org.freedesktop.DBus.Introspectable"
and msg.member == "Introspect"
Expand Down

0 comments on commit b858d95

Please sign in to comment.