From 7847e26e6e6cfe172437544d7709dc0c87a65402 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Oct 2022 13:16:03 -1000 Subject: [PATCH] feat: speed up marshalling variants (#86) --- src/dbus_fast/_private/marshaller.pxd | 8 +++++++- src/dbus_fast/_private/marshaller.py | 15 +++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/dbus_fast/_private/marshaller.pxd b/src/dbus_fast/_private/marshaller.pxd index 32fd554..430e85c 100644 --- a/src/dbus_fast/_private/marshaller.pxd +++ b/src/dbus_fast/_private/marshaller.pxd @@ -23,6 +23,12 @@ cdef class Marshaller: ) cpdef write_string(self, object value, _ = *) + @cython.locals( + signature_bytes=cython.bytes, + signature_len=cython.uint, + ) + cdef _write_signature(self, str signature) + @cython.locals( array_len=cython.uint, written=cython.uint, @@ -40,7 +46,7 @@ cdef class Marshaller: @cython.locals( written=cython.uint, ) - cpdef write_single(self, object type_, object body) + cdef _write_single(self, object type_, object body) @cython.locals( written=cython.uint, diff --git a/src/dbus_fast/_private/marshaller.py b/src/dbus_fast/_private/marshaller.py index 0501f3e..fcf5d1d 100644 --- a/src/dbus_fast/_private/marshaller.py +++ b/src/dbus_fast/_private/marshaller.py @@ -37,6 +37,9 @@ class Marshaller: return written + 4 def write_signature(self, signature: str, _=None) -> int: + return self._write_signature(signature) + + def _write_signature(self, signature) -> int: signature_bytes = signature.encode() signature_len = len(signature) self._buf.append(signature_len) @@ -56,8 +59,8 @@ class Marshaller: return written def write_variant(self, variant: Variant, _=None) -> int: - written = self.write_signature(variant.signature) - written += self.write_single(variant.type, variant.value) + written = self._write_signature(variant.signature) + written += self._write_single(variant.type, variant.value) return written def write_array(self, array: Iterable[Any], type_: SignatureType) -> int: @@ -103,16 +106,16 @@ class Marshaller: def write_struct(self, array: List[Any], type_: SignatureType) -> int: written = self._align(8) for i, value in enumerate(array): - written += self.write_single(type_.children[i], value) + written += self._write_single(type_.children[i], value) return written def write_dict_entry(self, dict_entry: List[Any], type_: SignatureType) -> int: written = self._align(8) - written += self.write_single(type_.children[0], dict_entry[0]) - written += self.write_single(type_.children[1], dict_entry[1]) + written += self._write_single(type_.children[0], dict_entry[0]) + written += self._write_single(type_.children[1], dict_entry[1]) return written - def write_single(self, type_: SignatureType, body: Any) -> int: + def _write_single(self, type_: SignatureType, body: Any) -> int: t = type_.token if t not in self._writers: