Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: speed up decoding headers by avoiding unicode checks #125

Merged
merged 3 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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