From 3dc98be7e948d61cd98b326ece4bc9eef7803684 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 20 Apr 2023 14:31:51 -1000 Subject: [PATCH] feat: improve unmarshall performance (#199) --- src/dbus_fast/_private/unmarshaller.pxd | 25 ++++++++++++++----------- src/dbus_fast/_private/unmarshaller.py | 4 ++-- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/dbus_fast/_private/unmarshaller.pxd b/src/dbus_fast/_private/unmarshaller.pxd index b00c1fec..120db911 100644 --- a/src/dbus_fast/_private/unmarshaller.pxd +++ b/src/dbus_fast/_private/unmarshaller.pxd @@ -43,7 +43,7 @@ cdef object UINT16_UNPACK_BIG_ENDIAN cdef cython.dict MESSAGE_TYPE_MAP cdef cython.dict MESSAGE_FLAG_MAP -cdef object HEADER_MESSAGE_ARG_NAME +cdef dict HEADER_MESSAGE_ARG_NAME cdef SignatureTree SIGNATURE_TREE_EMPTY cdef SignatureTree SIGNATURE_TREE_B @@ -108,8 +108,8 @@ cdef class Unmarshaller: cdef unsigned int _body_len cdef unsigned int _serial cdef unsigned int _header_len - cdef unsigned int _message_type - cdef unsigned int _flag + cdef object _message_type + cdef object _flag cdef unsigned int _msg_len cdef unsigned int _is_native cdef object _uint32_unpack @@ -154,9 +154,12 @@ cdef class Unmarshaller: @cython.locals( str_start=cython.uint, ) - cdef _read_string_unpack(self) + cdef str _read_string_unpack(self) - cdef _read_variant(self) + @cython.locals( + tree=SignatureTree, + ) + cdef Variant _read_variant(self) cpdef read_array(self, SignatureType type_) @@ -165,7 +168,7 @@ cdef class Unmarshaller: array_length=cython.uint, child_type=SignatureType, ) - cdef _read_array(self, SignatureType type_) + cdef object _read_array(self, SignatureType type_) cpdef read_signature(self, SignatureType type_) @@ -173,17 +176,18 @@ cdef class Unmarshaller: o=cython.ulong, signature_len=cython.uint, ) - cdef _read_signature(self) + cdef str _read_signature(self) @cython.locals( endian=cython.uint, protocol_version=cython.uint, - key=cython.str + key=cython.str, ) cdef _read_header(self) @cython.locals( - body=cython.list + body=cython.list, + header_fields=cython.dict ) cdef _read_body(self) @@ -194,8 +198,7 @@ cdef class Unmarshaller: @cython.locals( beginning_pos=cython.ulong, o=cython.ulong, - field_0=cython.uint, token_as_int=cython.uint, signature_len=cython.uint, ) - cdef header_fields(self, unsigned int header_length) + cdef cython.dict _header_fields(self, unsigned int header_length) diff --git a/src/dbus_fast/_private/unmarshaller.py b/src/dbus_fast/_private/unmarshaller.py index b4afbe0d..ab330906 100644 --- a/src/dbus_fast/_private/unmarshaller.py +++ b/src/dbus_fast/_private/unmarshaller.py @@ -494,7 +494,7 @@ def _read_array(self, type_: _SignatureType) -> Iterable[Any]: result_list.append(reader(self, child_type)) return result_list - def header_fields(self, header_length: int) -> Dict[str, Any]: + def _header_fields(self, header_length: int) -> Dict[str, Any]: """Header fields are always a(yv).""" beginning_pos = self._pos headers = {} @@ -586,7 +586,7 @@ def _read_body(self) -> None: """Read the body of the message.""" self._read_to_pos(HEADER_SIGNATURE_SIZE + self._msg_len) self._pos = HEADER_ARRAY_OF_STRUCT_SIGNATURE_POSITION - header_fields = self.header_fields(self._header_len) + header_fields = self._header_fields(self._header_len) self._pos += -self._pos & 7 # align 8 header_fields.pop("unix_fds", None) # defined by self._unix_fds signature = header_fields.pop("signature", "")