feat: improve unmarshalling performance (#18)

This commit is contained in:
J. Nick Koston 2022-09-09 21:13:34 -05:00 committed by GitHub
parent 62aa40a7be
commit 4362b93fc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 3 deletions

View File

@ -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

View File

@ -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,

View File

@ -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],

View File

@ -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.