feat: speed up unmarshaller (#101)

This commit is contained in:
J. Nick Koston 2022-10-10 13:56:58 -10:00 committed by GitHub
parent 12793c37f8
commit a6a248b3b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 3 deletions

View File

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

View File

@ -245,6 +245,9 @@ class Unmarshaller:
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]
@ -291,10 +294,13 @@ class Unmarshaller:
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]:

View File

@ -443,5 +443,13 @@ class Variant:
@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)