From 88f73fedd207c48f07562142791fce18037aa82d Mon Sep 17 00:00:00 2001 From: henadzit Date: Fri, 7 Feb 2025 19:11:40 +0100 Subject: [PATCH] perf(c): use PyObject_CallFunctionObjArgs in UUIDLoader --- psycopg_c/psycopg_c/types/uuid.pyx | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/psycopg_c/psycopg_c/types/uuid.pyx b/psycopg_c/psycopg_c/types/uuid.pyx index 4e8cd8b18..8bac18f86 100644 --- a/psycopg_c/psycopg_c/types/uuid.pyx +++ b/psycopg_c/psycopg_c/types/uuid.pyx @@ -65,12 +65,22 @@ static const int8_t hex_to_int_map[] = { cdef class UUIDLoader(CLoader): format = PQ_TEXT + cdef PyObject *_uuid_type + cdef object _uuid_new + cdef object _obj_setattr + cdef PyObject *_safeuuid_unknown + def __cinit__(self, oid: int, context: AdaptContext | None = None): global uuid # uuid is slow to import, lazy load it if uuid is None: import uuid + self._uuid_type = uuid.UUID + self._uuid_new = uuid.UUID.__new__ + self._obj_setattr = object.__setattr__ + self._safeuuid_unknown = uuid.SafeUUID.unknown + cdef object cload(self, const char *data, size_t length): cdef uint64_t high = 0 cdef uint64_t low = 0 @@ -92,12 +102,11 @@ cdef class UUIDLoader(CLoader): if ndigits != 32: raise ValueError("Invalid UUID string") - cdef object py_high = PyLong_FromUnsignedLongLong(high) - cdef object py_low = PyLong_FromUnsignedLongLong(low) + cdef object int_value = (PyLong_FromUnsignedLongLong(high) << 64) | PyLong_FromUnsignedLongLong(low) - u = uuid.UUID.__new__(uuid.UUID) - object.__setattr__(u, 'is_safe', uuid.SafeUUID.unknown) - object.__setattr__(u, 'int', (py_high << 64) | py_low) + cdef object u = PyObject_CallFunctionObjArgs(self._uuid_new, self._uuid_type, NULL) + PyObject_CallFunctionObjArgs(self._obj_setattr, u, "is_safe", self._safeuuid_unknown, NULL) + PyObject_CallFunctionObjArgs(self._obj_setattr, u, "int", int_value, NULL) return u -- 2.47.2