From: Daniele Varrazzo Date: Sun, 16 May 2021 00:26:04 +0000 (+0200) Subject: Streamline fixed size dumpers by dumping directly in the output buffer X-Git-Tag: 3.0.dev0~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a4752640e825bd1518cd2132eaf490124193f1da;p=thirdparty%2Fpsycopg.git Streamline fixed size dumpers by dumping directly in the output buffer --- diff --git a/psycopg3_c/psycopg3_c/types/numeric.pyx b/psycopg3_c/psycopg3_c/types/numeric.pyx index 765582058..68ecf3301 100644 --- a/psycopg3_c/psycopg3_c/types/numeric.pyx +++ b/psycopg3_c/psycopg3_c/types/numeric.pyx @@ -123,12 +123,12 @@ cdef class Int2BinaryDumper(CDumper): self.oid = oids.INT2_OID cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1: - cdef char *buf = CDumper.ensure_size(rv, offset, sizeof(int16_t)) + cdef int16_t *buf = CDumper.ensure_size( + rv, offset, sizeof(int16_t)) cdef int16_t val = PyLong_AsLongLong(obj) # swap bytes if needed cdef uint16_t *ptvar = (&val) - cdef int16_t beval = endian.htobe16(ptvar[0]) - memcpy(buf, &beval, sizeof(int16_t)) + buf[0] = endian.htobe16(ptvar[0]) return sizeof(int16_t) @@ -141,12 +141,12 @@ cdef class Int4BinaryDumper(CDumper): self.oid = oids.INT4_OID cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1: - cdef char *buf = CDumper.ensure_size(rv, offset, sizeof(int32_t)) + cdef int32_t *buf = CDumper.ensure_size( + rv, offset, sizeof(int32_t)) cdef int32_t val = PyLong_AsLongLong(obj) # swap bytes if needed cdef uint32_t *ptvar = (&val) - cdef int32_t beval = endian.htobe32(ptvar[0]) - memcpy(buf, &beval, sizeof(int32_t)) + buf[0] = endian.htobe32(ptvar[0]) return sizeof(int32_t) @@ -159,12 +159,12 @@ cdef class Int8BinaryDumper(CDumper): self.oid = oids.INT8_OID cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1: - cdef char *buf = CDumper.ensure_size(rv, offset, sizeof(int64_t)) + cdef int64_t *buf = CDumper.ensure_size( + rv, offset, sizeof(int64_t)) cdef int64_t val = PyLong_AsLongLong(obj) # swap bytes if needed cdef uint64_t *ptvar = (&val) - cdef int64_t beval = endian.htobe64(ptvar[0]) - memcpy(buf, &beval, sizeof(int64_t)) + buf[0] = endian.htobe64(ptvar[0]) return sizeof(int64_t) @@ -383,10 +383,10 @@ cdef class FloatBinaryDumper(CDumper): cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1: cdef double d = PyFloat_AsDouble(obj) cdef uint64_t *intptr = &d - cdef uint64_t swp = endian.htobe64(intptr[0]) - cdef char *tgt = CDumper.ensure_size(rv, offset, sizeof(swp)) - memcpy(tgt, &swp, sizeof(swp)) - return sizeof(swp) + cdef uint64_t *buf = CDumper.ensure_size( + rv, offset, sizeof(uint64_t)) + buf[0] = endian.htobe64(intptr[0]) + return sizeof(uint64_t) @cython.final diff --git a/psycopg3_c/psycopg3_c/types/singletons.pyx b/psycopg3_c/psycopg3_c/types/singletons.pyx index a176ac5e5..c0f8c229d 100644 --- a/psycopg3_c/psycopg3_c/types/singletons.pyx +++ b/psycopg3_c/psycopg3_c/types/singletons.pyx @@ -16,21 +16,18 @@ cdef class BoolDumper(CDumper): self.oid = oids.BOOL_OID cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1: - CDumper.ensure_size(rv, offset, 1) + cdef char *buf = CDumper.ensure_size(rv, offset, 1) # Fast paths, just a pointer comparison - cdef char val if obj is True: - val = b"t" + buf[0] = b"t" elif obj is False: - val = b"f" + buf[0] = b"f" elif obj: - val = b"t" + buf[0] = b"t" else: - val = b"f" + buf[0] = b"f" - cdef char *buf = PyByteArray_AS_STRING(rv) - buf[offset] = val return 1 def quote(self, obj: bool) -> bytes: @@ -51,21 +48,18 @@ cdef class BoolBinaryDumper(CDumper): self.oid = oids.BOOL_OID cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1: - CDumper.ensure_size(rv, offset, 1) + cdef char *buf = CDumper.ensure_size(rv, offset, 1) # Fast paths, just a pointer comparison - cdef char val if obj is True: - val = b"\x01" + buf[0] = b"\x01" elif obj is False: - val = b"\x00" + buf[0] = b"\x00" elif obj: - val = b"\x01" + buf[0] = b"\x01" else: - val = b"\x00" + buf[0] = b"\x00" - cdef char *buf = PyByteArray_AS_STRING(rv) - buf[offset] = val return 1