psycopg.git / summary / log / commit / refs
commit 91ead7983f2c8ba3d697f0befcd289fec64dcfb4
Author: henadzit <henadzi.tsaryk@gmail.com>
Commit: Daniele Varrazzo <daniele.varrazzo@gmail.com>
Date: Thu Jan 30 10:02:39 2025 +0000
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 4d643ac9..6f2a99d3 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)
[parent: 342b0db77310]