feat: speed up marshaller by pre-packing bools (#139)

This commit is contained in:
J. Nick Koston 2022-11-03 10:01:26 +01:00 committed by GitHub
parent 4a23e0e0c5
commit c10a241dc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -3,9 +3,12 @@
import cython
cdef bytes PACKED_UINT32_ZERO
cdef object PACK_UINT32
cdef bytes PACKED_UINT32_ZERO
cdef bytes PACKED_BOOL_TRUE
cdef bytes PACKED_BOOL_FALSE
cdef class Marshaller:
cdef object signature_tree

View File

@ -5,6 +5,8 @@ from ..signature import SignatureType, Variant, get_signature_tree
PACK_UINT32 = Struct("<I").pack
PACKED_UINT32_ZERO = PACK_UINT32(0)
PACKED_BOOL_FALSE = PACK_UINT32(int(0))
PACKED_BOOL_TRUE = PACK_UINT32(int(1))
class Marshaller:
@ -34,7 +36,7 @@ class Marshaller:
def write_boolean(self, boolean: bool, type_: SignatureType) -> int:
written = self._align(4)
self._buf.extend(PACK_UINT32(int(boolean)))
self._buf += PACKED_BOOL_TRUE if boolean else PACKED_BOOL_FALSE
return written + 4
def write_signature(self, signature: str, type_: SignatureType) -> int: