pg.ddx.io  psycopg/psycopg GitHub issues and pull requests  
help / color / mirror / Atom feed
[psycopg/psycopg] PR #1272: refactor(c): drop class replacement hack to make UUID writable
5+ messages / 2 participants
[nested] [flat]

* [psycopg/psycopg] PR #1272: refactor(c): drop class replacement hack to make UUID writable
@ 2026-03-01 16:45 "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
  0 siblings, 0 replies; 5+ messages in thread

From: dvarrazzo (@dvarrazzo) @ 2026-03-01 16:45 UTC (permalink / raw)
  To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>

From a suggestion by @akx Aarni Koskela, see
https://github.com/psycopg/psycopg/pull/1269#discussion_r2869270196

inline patch 1: psycopg_c/psycopg_c/types/uuid.pyx (+77/-3 lines) download inline-1.patchdiff --git a/psycopg_c/psycopg_c/types/uuid.pyx b/psycopg_c/psycopg_c/types/uuid.pyx index 164be69c3..ef243d82f 100644 --- a/psycopg_c/psycopg_c/types/uuid.pyx +++ b/psycopg_c/psycopg_c/types/uuid.pyx @@ -8,7 +8,7 @@ static const int8_t hex_to_int_map[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0-15 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32-47 - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 48-63 ('0'-'9') + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 48-63 ('0'-'9') -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 64-79 ('A'-'F') -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80-95 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 96-111 ('a'-'f') @@ -28,6 +28,80 @@ static const int8_t hex_to_int_map[] = { cdef class _UUIDLoader(CLoader): + cdef object _object_new + cdef object _object_setattr + cdef PyObject *_uuid_type + cdef PyObject *_safeuuid_unknown + + def __cinit__(self, oid: int, context: AdaptContext | None = None): + import uuid + + self._object_new = object.__new__ + self._object_setattr = object.__setattr__ + self._uuid_type = <PyObject *>uuid.UUID + self._safeuuid_unknown = <PyObject *>uuid.SafeUUID.unknown + + cdef object _return_uuid(self, uint64_t low, uint64_t high): + cdef object py_low = PyLong_FromUnsignedLongLong(low) + cdef object py_high = PyLong_FromUnsignedLongLong(high) + cdef object py_value = (py_high << 64) | py_low + + cdef object u = PyObject_CallFunctionObjArgs( + self._object_new, self._uuid_type, NULL) + PyObject_CallFunctionObjArgs( + self._object_setattr, + <PyObject *>u, <PyObject *>"int", <PyObject *>py_value, NULL) + PyObject_CallFunctionObjArgs( + self._object_setattr, + <PyObject *>u, <PyObject *>"is_safe", self._safeuuid_unknown, NULL) + return u + + +@cython.final +cdef class UUIDLoader(_UUIDLoader): + format = PQ_TEXT + + cdef object cload(self, const char *data, size_t length): + cdef uint64_t high = 0 + cdef uint64_t low = 0 + cdef size_t i + cdef int ndigits = 0 + cdef int8_t c + + for i in range(length): + c = data[i] + if hex_to_int_map[c] == -1: + continue + + if ndigits < 16: + high = (high << 4) | hex_to_int_map[c] + else: + low = (low << 4) | hex_to_int_map[c] + ndigits += 1 + + if ndigits != 32: + raise ValueError("Invalid UUID string") + + return self._return_uuid(low, high) + + +@cython.final +cdef class UUIDBinaryLoader(_UUIDLoader): + format = PQ_BINARY + + cdef object cload(self, const char *data, size_t length): + cdef uint64_t be[2] + if length != sizeof(be): + raise ValueError("Invalid UUID data") + memcpy(&be, data, sizeof(be)) + + cdef uint64_t high = endian.be64toh(be[0]) + cdef uint64_t low = endian.be64toh(be[1]) + return self._return_uuid(low, high) + + +cdef class _UUIDLoaderOld(CLoader): + cdef object _object_new cdef object _uuid_type cdef PyObject *_wuuid_type @@ -55,7 +129,7 @@ cdef class _UUIDLoader(CLoader): @cython.final -cdef class UUIDLoader(_UUIDLoader): +cdef class UUIDLoaderOld(_UUIDLoaderOld): format = PQ_TEXT cdef object cload(self, const char *data, size_t length): @@ -83,7 +157,7 @@ cdef class UUIDLoader(_UUIDLoader): @cython.final -cdef class UUIDBinaryLoader(_UUIDLoader): +cdef class UUIDBinaryLoaderOld(_UUIDLoaderOld): format = PQ_BINARY cdef object cload(self, const char *data, size_t length):
^ permalink raw [nested|flat] 5+ messages in thread

* Re: [psycopg/psycopg] PR #1272: refactor(c): drop class replacement hack to make UUID writable
@ 2026-03-01 22:06 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: dvarrazzo (@dvarrazzo) @ 2026-03-01 22:06 UTC (permalink / raw)
  To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>

Uhm... The original idea in #998 was actually what reimplemented here: see https://github.com/psycopg/psycopg/pull/998#issuecomment-2619228112

Looking at comments in the MR it seems that class replacement was an optimization.

We should benchmark the two solutions.

^ permalink  raw  [nested|flat] 5+ messages in thread

* Re: [psycopg/psycopg] PR #1272: refactor(c): drop class replacement hack to make UUID writable
@ 2026-03-02 06:43 ` "akx (@akx)" <noreply+akx@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: akx (@akx) @ 2026-03-02 06:43 UTC (permalink / raw)
  To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>

Huh 😄 If a benchmark proves that the `WritableUUID` approach is faster, a PR should at least add a comment in the source next to this so the next person wondering what's up (indeed, as I said in https://github.com/psycopg/psycopg/pull/1269#discussion_r2869270196, "something more subtle about it") knows...

^ permalink  raw  [nested|flat] 5+ messages in thread

* Re: [psycopg/psycopg] PR #1272: refactor(c): drop class replacement hack to make UUID writable
@ 2026-11-11 21:40 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: dvarrazzo (@dvarrazzo) @ 2026-11-11 21:40 UTC (permalink / raw)
  To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>

@dvarrazzo closed this.

^ permalink  raw  [nested|flat] 5+ messages in thread

* Re: [psycopg/psycopg] PR #1272: refactor(c): drop class replacement hack to make UUID writable
@ 2026-11-11 21:40 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: dvarrazzo (@dvarrazzo) @ 2026-11-11 21:40 UTC (permalink / raw)
  To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>

Overridden by #1494

^ permalink  raw  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2026-11-11 21:40 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-03-01 16:45 [psycopg/psycopg] PR #1272: refactor(c): drop class replacement hack to make UUID writable "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
2026-03-01 22:06 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
2026-03-02 06:43 ` "akx (@akx)" <noreply+akx@github.com>
2026-11-11 21:40 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>
2026-11-11 21:40 ` "dvarrazzo (@dvarrazzo)" <noreply+dvarrazzo@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox