Skip to content

Commit

Permalink
feat: speed up unmarshaller (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 10, 2022
1 parent 12793c3 commit a6a248b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/dbus_fast/_private/unmarshaller.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ cdef class Unmarshaller:

cpdef read_uint32_cast(self, object type_)

cpdef read_int16_cast(self, object type_)

cdef _read_int16_cast(self)

cpdef read_string_unpack(self, object type_)

cdef _read_string_unpack(self)
Expand Down
10 changes: 8 additions & 2 deletions src/dbus_fast/_private/unmarshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ def read_uint32_cast(self, type_: SignatureType) -> int:
return self._view[self._pos - UINT32_SIZE : self._pos].cast(UINT32_CAST)[0]

def read_int16_cast(self, type_: SignatureType) -> int:
return self._read_int16_cast()

def _read_int16_cast(self) -> int:
self._pos += INT16_SIZE + (-self._pos & (INT16_SIZE - 1)) # align
return self._view[self._pos - INT16_SIZE : self._pos].cast(INT16_CAST)[0]

Expand Down Expand Up @@ -291,10 +294,13 @@ def _read_variant(self) -> Variant:
tree = get_signature_tree(self._read_signature())
signature_type = tree.types[0]
# verify in Variant is only useful on construction not unmarshalling
token = signature_type.token
if token == "n" and self._uint32_unpack is None:
return Variant(tree, self._read_int16_cast(), False)
return Variant(
tree,
self._readers[signature_type.token](self, signature_type),
verify=False,
self._readers[token](self, signature_type),
False,
)

def read_struct(self, type_: SignatureType) -> List[Any]:
Expand Down
10 changes: 9 additions & 1 deletion src/dbus_fast/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,5 +443,13 @@ def __repr__(self) -> str:


@lru_cache(maxsize=None)
def get_signature_tree(signature: str = "") -> SignatureTree:
def get_signature_tree(signature: str) -> SignatureTree:
"""Get a signature tree for the given signature.
:param signature: The signature to get a tree for.
:type signature: str
:returns: The signature tree for the given signature.
:rtype: :class:`SignatureTree`
"""
return SignatureTree(signature)

0 comments on commit a6a248b

Please sign in to comment.