From 4362b93fc84406adfa026b6573bc076327c71c5b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 9 Sep 2022 21:13:34 -0500 Subject: [PATCH] feat: improve unmarshalling performance (#18) --- src/dbus_fast/_private/unmarshaller.py | 22 ++++++++++++++++++++-- src/dbus_fast/message.py | 17 +++++++++++++++++ src/dbus_fast/signature.py | 2 ++ src/dbus_fast/validators.py | 2 +- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/dbus_fast/_private/unmarshaller.py b/src/dbus_fast/_private/unmarshaller.py index 1c31367..6c5758c 100644 --- a/src/dbus_fast/_private/unmarshaller.py +++ b/src/dbus_fast/_private/unmarshaller.py @@ -94,6 +94,24 @@ class Unmarshaller: unpack: Dict[str, Struct] readers: READER_TYPE + __slots__ = ( + "unix_fds", + "can_cast", + "buf", + "view", + "offset", + "stream", + "sock", + "message", + "readers", + "body_len", + "serial", + "header_len", + "message_type", + "flag", + "msg_len", + ) + def __init__(self, stream: io.BufferedRWPair, sock=None): self.unix_fds: List[int] = [] self.can_cast = False @@ -109,6 +127,7 @@ class Unmarshaller: self.header_len: int | None = None self.message_type: MessageType | None = None self.flag: MessageFlag | None = None + self.msg_len = 0 def read_sock(self, length: int) -> bytes: """reads from the socket, storing any fds sent and handling errors @@ -220,8 +239,7 @@ class Unmarshaller: def read_argument(self, type_: SignatureType) -> Any: """Dispatch to an argument reader or cast/unpack a C type.""" - token = type_.token - reader, ctype, size, struct = self.readers[token] + reader, ctype, size, struct = self.readers[type_.token] if reader: # complex type return reader(self, type_) self.offset += size + (-self.offset & (size - 1)) # align diff --git a/src/dbus_fast/message.py b/src/dbus_fast/message.py index 8433378..8d24e32 100644 --- a/src/dbus_fast/message.py +++ b/src/dbus_fast/message.py @@ -76,6 +76,23 @@ class Message: - :class:`InvalidInterfaceNameError` - If ``error_name`` or ``interface`` is not a valid interface name. """ + __slots__ = ( + "destination", + "path", + "interface", + "member", + "message_type", + "flags", + "error_name", + "reply_serial", + "sender", + "unix_fds", + "signature", + "signature_tree", + "body", + "serial", + ) + def __init__( self, destination: str = None, diff --git a/src/dbus_fast/signature.py b/src/dbus_fast/signature.py index 1fd7b00..da8ff71 100644 --- a/src/dbus_fast/signature.py +++ b/src/dbus_fast/signature.py @@ -398,6 +398,8 @@ class Variant: :class:`SignatureBodyMismatchError` if the signature does not match the body. """ + __slots__ = ("type", "signature", "value") + def __init__( self, signature: Union[str, SignatureTree, SignatureType], diff --git a/src/dbus_fast/validators.py b/src/dbus_fast/validators.py index 6ff7aae..176f79c 100644 --- a/src/dbus_fast/validators.py +++ b/src/dbus_fast/validators.py @@ -49,7 +49,7 @@ def is_bus_name_valid(name: str) -> bool: return True -@lru_cache(maxsize=512) +@lru_cache(maxsize=1024) def is_object_path_valid(path: str) -> bool: """Whether this is a valid object path.