Skip to content

Commit 8a3a4b2

Browse files
committed
Micro-optimize UUID.__str__
1 parent eb34fe3 commit 8a3a4b2

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

uuid.pyx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,20 @@ cdef class PgBaseUUID:
134134
def __str__(self):
135135
cdef:
136136
uint64_t u
137-
char buf[16]
138137
char out[36]
139138

140139
u = <uint64_t>hton.unpack_int64(self._data)
141-
i64_to_hex(u, buf)
142-
memcpy(out, buf, 8)
140+
i64_to_hex(u, out)
141+
u = <uint64_t>hton.unpack_int64(self._data + 8)
142+
i64_to_hex(u, out + 20)
143+
144+
memcpy(out + 14, out + 12, 4)
145+
memcpy(out + 9, out + 8, 4)
146+
memcpy(out + 19, out + 20, 4)
143147
out[8] = b'-'
144-
memcpy(out + 9, buf + 8, 4)
145148
out[13] = b'-'
146-
memcpy(out + 14, buf + 12, 4)
147149
out[18] = b'-'
148-
149-
u = <uint64_t>hton.unpack_int64(self._data + 8)
150-
i64_to_hex(u, buf)
151-
memcpy(out + 19, buf, 4)
152150
out[23] = b'-'
153-
memcpy(out + 24, buf + 4, 12)
154151

155152
return cpythonx.PyUnicode_FromKindAndData(
156153
cpythonx.PyUnicode_1BYTE_KIND, <void*>out, 36)

0 commit comments

Comments
 (0)