]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Streamline fixed size dumpers by dumping directly in the output buffer
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 16 May 2021 00:26:04 +0000 (02:26 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 16 May 2021 00:26:04 +0000 (02:26 +0200)
psycopg3_c/psycopg3_c/types/numeric.pyx
psycopg3_c/psycopg3_c/types/singletons.pyx

index 765582058a69e671233050e23fad4dfa56ecc1bc..68ecf3301822cf6304d31ae41521463a14e761e7 100644 (file)
@@ -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 = <int16_t *>CDumper.ensure_size(
+            rv, offset, sizeof(int16_t))
         cdef int16_t val = PyLong_AsLongLong(obj)
         # swap bytes if needed
         cdef uint16_t *ptvar = <uint16_t *>(&val)
-        cdef int16_t beval = endian.htobe16(ptvar[0])
-        memcpy(buf, <void *>&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 = <int32_t *>CDumper.ensure_size(
+            rv, offset, sizeof(int32_t))
         cdef int32_t val = PyLong_AsLongLong(obj)
         # swap bytes if needed
         cdef uint32_t *ptvar = <uint32_t *>(&val)
-        cdef int32_t beval = endian.htobe32(ptvar[0])
-        memcpy(buf, <void *>&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 = <int64_t *>CDumper.ensure_size(
+            rv, offset, sizeof(int64_t))
         cdef int64_t val = PyLong_AsLongLong(obj)
         # swap bytes if needed
         cdef uint64_t *ptvar = <uint64_t *>(&val)
-        cdef int64_t beval = endian.htobe64(ptvar[0])
-        memcpy(buf, <void *>&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 = <uint64_t *>&d
-        cdef uint64_t swp = endian.htobe64(intptr[0])
-        cdef char *tgt = CDumper.ensure_size(rv, offset, sizeof(swp))
-        memcpy(tgt, <void *>&swp, sizeof(swp))
-        return sizeof(swp)
+        cdef uint64_t *buf = <uint64_t *>CDumper.ensure_size(
+            rv, offset, sizeof(uint64_t))
+        buf[0] = endian.htobe64(intptr[0])
+        return sizeof(uint64_t)
 
 
 @cython.final
index a176ac5e5679866b99252ad208c80094042b668b..c0f8c229df625079426ee30238d15ecf39c791cd 100644 (file)
@@ -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