From: henadzit Date: Wed, 29 Jan 2025 23:32:17 +0000 (+0100) Subject: perf(c): use UUID.__new__ X-Git-Tag: 3.2.5~2^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=553b17b5d68053880d9d0111d2de6a551647e3eb;p=thirdparty%2Fpsycopg.git perf(c): use UUID.__new__ --- diff --git a/psycopg_c/psycopg_c/types/uuid.pyx b/psycopg_c/psycopg_c/types/uuid.pyx index 0c391da6e..4d643ac9c 100644 --- a/psycopg_c/psycopg_c/types/uuid.pyx +++ b/psycopg_c/psycopg_c/types/uuid.pyx @@ -8,12 +8,6 @@ cdef extern from "Python.h": # support 3.0.x const char *PyUnicode_AsUTF8(object unicode) except NULL -from libc.stdio cimport printf - - -#cdef extern from "Python.h": -# Py_ssize_t PyLong_AsNativeBytes(PyObject* vv, void* buffer, Py_ssize_t n, int flags) - @cython.final cdef class UUIDDumper(CDumper): @@ -37,10 +31,6 @@ cdef class UUIDBinaryDumper(CDumper): cdef char *buf = CDumper.ensure_size(rv, offset, 16) memcpy(buf, src, 16) return 16 - #cdef PyObject *pyobj = obj.int - #cdef char *buf = CDumper.ensure_size(rv, offset, 16) - #PyLong_AsNativeBytes(pyobj, buf, 16, 4) - #return 16 @cython.final @@ -48,7 +38,20 @@ cdef class UUIDLoader(CLoader): format = PQ_TEXT cdef object cload(self, const char *data, size_t length): - return uuid.UUID(hex=data[:length].decode()) + cdef char[33] hex_str + cdef size_t i + cdef int j = 0 + for i in range(36): + if data[i] == b'-': + continue + hex_str[j] = data[i] + j += 1 + hex_str[32] = 0 + + u = uuid.UUID.__new__(uuid.UUID) + object.__setattr__(u, 'is_safe', uuid.SafeUUID.unknown) + object.__setattr__(u, 'int', PyLong_FromString(hex_str, NULL, 16)) + return u @cython.final @@ -56,4 +59,7 @@ cdef class UUIDBinaryLoader(CLoader): format = PQ_BINARY cdef object cload(self, const char *data, size_t length): - return uuid.UUID(bytes=data[:length]) + u = uuid.UUID.__new__(uuid.UUID) + object.__setattr__(u, 'is_safe', uuid.SafeUUID.unknown) + object.__setattr__(u, 'int', int.from_bytes(data[:length], 'big')) + return u diff --git a/tests/types/test_uuid.py b/tests/types/test_uuid.py index 3e9987bfb..a9c46544d 100644 --- a/tests/types/test_uuid.py +++ b/tests/types/test_uuid.py @@ -18,9 +18,11 @@ def test_uuid_dump(conn, fmt_in): @pytest.mark.crdb_skip("copy") @pytest.mark.parametrize("fmt_out", pq.Format) -def test_uuid_load(conn, fmt_out): +@pytest.mark.parametrize( + "val", ["12345678123456781234567812345679", "12345678-1234-5678-1234-567812345679"] +) +def test_uuid_load(conn, fmt_out, val): cur = conn.cursor(binary=fmt_out) - val = "12345678123456781234567812345679" cur.execute("select %s::uuid", (val,)) assert cur.fetchone()[0] == UUID(val)