]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
perf(c): lazy load UUID
authorhenadzit <henadzi.tsaryk@gmail.com>
Thu, 30 Jan 2025 10:02:39 +0000 (11:02 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 20 Feb 2025 10:15:41 +0000 (11:15 +0100)
psycopg_c/psycopg_c/types/uuid.pyx

index 4d643ac9c99536a2418fb98fee934e6d8ca7ec8b..6f2a99d345e04e273f032fba1441420c3a3e3ddf 100644 (file)
@@ -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)