feat: improve performance of checking SignatureType tokens (#410)

* feat: improve performance of checking SignatureType tokens

* chore: fixes
This commit is contained in:
J. Nick Koston 2025-03-06 11:44:09 -10:00 committed by GitHub
parent a56381a7a1
commit 488716020e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 5 deletions

View File

@ -617,7 +617,7 @@ class Unmarshaller:
else:
array_length = self._uint32_unpack(self._buf, self._pos - UINT32_SIZE)[0]
child_type: SignatureType = type_.children[0]
token_as_int = ord(child_type.token[0])
token_as_int = child_type.token_as_int
if (
token_as_int == TOKEN_X_AS_INT
@ -635,12 +635,13 @@ class Unmarshaller:
if token_as_int == TOKEN_LEFT_CURLY_AS_INT:
result_dict: dict[Any, Any] = {}
key: str | int
beginning_pos = self._pos
children = child_type.children
child_0 = children[0]
child_1 = children[1]
child_0_token_as_int = ord(child_0.token[0])
child_1_token_as_int = ord(child_1.token[0])
child_0_token_as_int = child_0.token_as_int
child_1_token_as_int = child_1.token_as_int
# Strings with variant values are the most common case
# so we optimize for that by inlining the string reading
# and the variant reading here
@ -650,7 +651,7 @@ class Unmarshaller:
) and child_1_token_as_int == TOKEN_V_AS_INT:
while self._pos - beginning_pos < array_length:
self._pos += -self._pos & 7 # align 8
key: str | int = self._read_string_unpack()
key = self._read_string_unpack()
result_dict[key] = self._read_variant()
elif (
child_0_token_as_int == TOKEN_Q_AS_INT

View File

@ -6,6 +6,7 @@ import cython
cdef class SignatureType:
cdef public str token
cdef public unsigned int token_as_int
cdef public list children
cdef str _signature

View File

@ -21,11 +21,12 @@ class SignatureType:
"""
_tokens = "ybnqiuxtdsogavh({"
__slots__ = ("token", "children", "_signature")
__slots__ = ("token", "token_as_int", "children", "_signature")
def __init__(self, token: str) -> None:
"""Init a new SignatureType."""
self.token: str = token
self.token_as_int = ord(token)
self.children: list[SignatureType] = []
self._signature: Optional[str] = None