Skip to content

Commit

Permalink
feat: improve performance of unmarshalling variants (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 7, 2025
1 parent e1aaf0a commit d376bb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/dbus_fast/_private/unmarshaller.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ cdef class Unmarshaller:
@cython.locals(
tree=SignatureTree,
token_as_int=cython.uint,
var=Variant,
)
cdef Variant _read_variant(self)

Expand Down
37 changes: 25 additions & 12 deletions src/dbus_fast/_private/unmarshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,53 +457,66 @@ def read_variant(self, type_: _SignatureType) -> Variant:
def _read_variant(self) -> Variant:
signature = self._read_signature()
token_as_int = ord(signature[0])
var = Variant.__new__(Variant)
# verify in Variant is only useful on construction not unmarshalling
if len(signature) == 1:
if token_as_int == TOKEN_N_AS_INT:
return Variant(SIGNATURE_TREE_N, self._read_int16_unpack(), False)
var._init_variant(SIGNATURE_TREE_N, self._read_int16_unpack(), False)
return var
if token_as_int == TOKEN_S_AS_INT:
return Variant(SIGNATURE_TREE_S, self._read_string_unpack(), False)
var._init_variant(SIGNATURE_TREE_S, self._read_string_unpack(), False)
return var
if token_as_int == TOKEN_B_AS_INT:
return Variant(SIGNATURE_TREE_B, self._read_boolean(), False)
var._init_variant(SIGNATURE_TREE_B, self._read_boolean(), False)
return var
if token_as_int == TOKEN_O_AS_INT:
return Variant(SIGNATURE_TREE_O, self._read_string_unpack(), False)
var._init_variant(SIGNATURE_TREE_O, self._read_string_unpack(), False)
return var
if token_as_int == TOKEN_U_AS_INT:
return Variant(SIGNATURE_TREE_U, self._read_uint32_unpack(), False)
var._init_variant(SIGNATURE_TREE_U, self._read_uint32_unpack(), False)
return var
if token_as_int == TOKEN_Y_AS_INT:
self._pos += 1
return Variant(SIGNATURE_TREE_Y, self._buf[self._pos - 1], False)
var._init_variant(SIGNATURE_TREE_Y, self._buf[self._pos - 1], False)
return var
elif token_as_int == TOKEN_A_AS_INT:
if signature == "ay":
return Variant(
var._init_variant(
SIGNATURE_TREE_AY, self.read_array(SIGNATURE_TREE_AY_TYPES_0), False
)
return var
if signature == "a{qv}":
return Variant(
var._init_variant(
SIGNATURE_TREE_A_QV,
self.read_array(SIGNATURE_TREE_A_QV_TYPES_0),
False,
)
return var
if signature == "as":
return Variant(
var._init_variant(
SIGNATURE_TREE_AS, self.read_array(SIGNATURE_TREE_AS_TYPES_0), False
)
return var
if signature == "a{sv}":
return Variant(
var._init_variant(
SIGNATURE_TREE_A_SV,
self.read_array(SIGNATURE_TREE_A_SV_TYPES_0),
False,
)
return var
if signature == "ao":
return Variant(
var._init_variant(
SIGNATURE_TREE_AO, self.read_array(SIGNATURE_TREE_AO_TYPES_0), False
)
return var
tree = get_signature_tree(signature)
signature_type = tree.types[0]
return Variant(
var._init_variant(
tree,
self._readers[signature_type.token](self, signature_type),
False,
)
return var

def read_struct(self, type_: _SignatureType) -> list[Any]:
self._pos += -self._pos & 7 # align 8
Expand Down

0 comments on commit d376bb1

Please sign in to comment.