Skip to content

Commit

Permalink
feat: speed up decoding headers by avoiding unicode checks (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 29, 2022
1 parent a9108b5 commit 6121781
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/dbus_fast/_private/unmarshaller.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ cdef object SIGNATURE_TREE_AY_TYPES_0
cdef object SIGNATURE_TREE_A_QV
cdef object SIGNATURE_TREE_A_QV_TYPES_0

cdef unsigned int TOKEN_O_AS_INT
cdef unsigned int TOKEN_S_AS_INT
cdef unsigned int TOKEN_G_AS_INT


cpdef get_signature_tree

Expand Down Expand Up @@ -170,6 +174,7 @@ cdef class Unmarshaller:
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)
11 changes: 8 additions & 3 deletions src/dbus_fast/_private/unmarshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
SIGNATURE_TREE_OA_SA_SV = get_signature_tree("oa{sa{sv}}")
SIGNATURE_TREE_OA_SA_SV_TYPES_1 = SIGNATURE_TREE_OA_SA_SV.types[1]

TOKEN_O_AS_INT = ord("o")
TOKEN_S_AS_INT = ord("s")
TOKEN_G_AS_INT = ord("g")

HEADER_MESSAGE_ARG_NAME = {
1: "path",
2: "interface",
Expand Down Expand Up @@ -483,16 +487,17 @@ def header_fields(self, header_length: int) -> Dict[str, Any]:
signature_len = buf[self._pos] # byte
o = self._pos + 1
self._pos += signature_len + 2 # one for the byte, one for the '\0'
token = buf[o : o + signature_len].decode()
token_as_int = buf[o]
# Now that we have the token we can read the variant value
key = HEADER_MESSAGE_ARG_NAME[field_0]
# Strings and signatures are the most common types
# so we inline them for performance
if token in "os":
if token_as_int == TOKEN_O_AS_INT or token_as_int == TOKEN_S_AS_INT:
headers[key] = self._read_string_unpack()
elif token == "g":
elif token_as_int == TOKEN_G_AS_INT:
headers[key] = self._read_signature()
else:
token = buf[o : o + signature_len].decode()
# There shouldn't be any other types in the header
# but just in case, we'll read it using the slow path
headers[key] = readers[token](self, get_signature_tree(token).types[0])
Expand Down

0 comments on commit 6121781

Please sign in to comment.