From 1a3ccfc1142162b087ece9725d9f2c52881717c4 Mon Sep 17 00:00:00 2001 From: henadzit Date: Thu, 30 Jan 2025 11:02:39 +0100 Subject: [PATCH] perf(c): lazy load UUID --- psycopg_c/psycopg_c/types/uuid.pyx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/psycopg_c/psycopg_c/types/uuid.pyx b/psycopg_c/psycopg_c/types/uuid.pyx index 4d643ac9c..6f2a99d34 100644 --- a/psycopg_c/psycopg_c/types/uuid.pyx +++ b/psycopg_c/psycopg_c/types/uuid.pyx @@ -1,6 +1,6 @@ cimport cython -import uuid +from types import ModuleType from cpython.bytes cimport PyBytes_AsString cdef extern from "Python.h": @@ -9,6 +9,9 @@ cdef extern from "Python.h": const char *PyUnicode_AsUTF8(object unicode) except NULL +uuid: ModuleType | None = None + + @cython.final cdef class UUIDDumper(CDumper): format = PQ_TEXT @@ -37,6 +40,12 @@ cdef class UUIDBinaryDumper(CDumper): cdef class UUIDLoader(CLoader): format = PQ_TEXT + 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 + cdef object cload(self, const char *data, size_t length): cdef char[33] hex_str cdef size_t i @@ -58,6 +67,12 @@ cdef class UUIDLoader(CLoader): cdef class UUIDBinaryLoader(CLoader): format = PQ_BINARY + 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 + cdef object cload(self, const char *data, size_t length): u = uuid.UUID.__new__(uuid.UUID) object.__setattr__(u, 'is_safe', uuid.SafeUUID.unknown) -- 2.47.2