From 18213c0a00f162cbf74fa7fc0bbcf12c1109c347 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 6 Oct 2022 21:55:11 -1000 Subject: [PATCH] feat: speed up unmarshalling int16 types (#81) --- src/dbus_fast/_private/unmarshaller.pxd | 3 +++ src/dbus_fast/_private/unmarshaller.py | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/dbus_fast/_private/unmarshaller.pxd b/src/dbus_fast/_private/unmarshaller.pxd index d6da01c..7dccb07 100644 --- a/src/dbus_fast/_private/unmarshaller.pxd +++ b/src/dbus_fast/_private/unmarshaller.pxd @@ -16,11 +16,14 @@ cdef str HEADER_SIGNATURE cdef unsigned int UINT32_SIZE +cdef unsigned int INT16_SIZE cdef unsigned int HEADER_ARRAY_OF_STRUCT_SIGNATURE_POSITION cdef unsigned int HEADER_SIGNATURE_SIZE cdef unsigned int LITTLE_ENDIAN cdef unsigned int BIG_ENDIAN cdef str UINT32_CAST +cdef str INT16_CAST + cdef object UINT32_SIGNATURE cdef class MarshallerStreamEndError(Exception): diff --git a/src/dbus_fast/_private/unmarshaller.py b/src/dbus_fast/_private/unmarshaller.py index b6493e5..4e05c1f 100644 --- a/src/dbus_fast/_private/unmarshaller.py +++ b/src/dbus_fast/_private/unmarshaller.py @@ -3,7 +3,7 @@ import io import socket import sys from struct import Struct -from typing import Any, Callable, Dict, List, Tuple +from typing import Any, Callable, Dict, List, Optional, Tuple from ..constants import MESSAGE_FLAG_MAP, MESSAGE_TYPE_MAP from ..errors import InvalidMessageError @@ -30,16 +30,20 @@ UINT32_SIZE = 4 UINT32_DBUS_TYPE = "u" UINT32_SIGNATURE = get_signature_tree(UINT32_DBUS_TYPE).types[0] +INT16_CAST = "h" +INT16_SIZE = 2 +INT16_DBUS_TYPE = "n" + DBUS_TO_CTYPE = { "y": ("B", 1), # byte - "n": ("h", 2), # int16 + INT16_DBUS_TYPE: (INT16_CAST, INT16_SIZE), # int16 "q": ("H", 2), # uint16 "i": ("i", 4), # int32 UINT32_DBUS_TYPE: (UINT32_CAST, UINT32_SIZE), # uint32 "x": ("q", 8), # int64 "t": ("Q", 8), # uint64 "d": ("d", 8), # double - "h": ("I", 4), # uint32 + "h": (UINT32_CAST, UINT32_SIZE), # uint32 } UINT32_UNPACK_BY_ENDIAN = { @@ -237,10 +241,14 @@ class Unmarshaller: if len(data) + start_len != pos: raise MarshallerStreamEndError() - def read_uint32_cast(self, type_: SignatureType) -> Any: + def read_uint32_cast(self, type_: SignatureType) -> int: self._pos += UINT32_SIZE + (-self._pos & (UINT32_SIZE - 1)) # align return self._view[self._pos - UINT32_SIZE : self._pos].cast(UINT32_CAST)[0] + def read_int16_cast(self, type_: SignatureType) -> int: + self._pos += INT16_SIZE + (-self._pos & (INT16_SIZE - 1)) # align + return self._view[self._pos - INT16_SIZE : self._pos].cast(INT16_CAST)[0] + def read_boolean(self, type_: SignatureType) -> bool: return bool(self._readers[UINT32_SIGNATURE.token](self, UINT32_SIGNATURE)) @@ -414,7 +422,7 @@ class Unmarshaller: validate=False, ) - def unmarshall(self): + def unmarshall(self) -> Optional[Message]: """Unmarshall the message. The underlying read function will raise MarshallerStreamEndError @@ -453,6 +461,7 @@ class Unmarshaller: "v": read_variant, "h": read_uint32_cast, UINT32_DBUS_TYPE: read_uint32_cast, + INT16_DBUS_TYPE: read_int16_cast, } _ctype_by_endian: Dict[