Skip to content

Conversation

@akx
Copy link
Contributor

@akx akx commented Oct 27, 2025

This PR follows up on #140663 by, well, following up on my musing about whether there should be an optimized C implementation of UUID stringification.

I tried to tighten the C implementation as much as possible, e.g. by directly writing into an 1BYTE_KIND PyUnicode object (as e.g. pystrhex.c does, which I couldn't use here because of the unique separator situation UUIDs have). I'd appreciate extra eyes on it, though!

The speedup seems pleasant for a fairly small patch – on my machine, some 4.5x:

$ ./python.exe -m timeit -s 'import uuid; uuid.UUID.__str__ = uuid._py_uuid_str_method; u = uuid.uuid4()' 'str(u)'
500000 loops, best of 5: 443 nsec per loop
$ ./python.exe -m timeit -s 'import uuid; u = uuid.uuid4()' 'str(u)'
2000000 loops, best of 5: 97.2 nsec per loop

@akx akx force-pushed the faster-uuid-str-2 branch from b395ff9 to 82f3279 Compare October 27, 2025 11:51
@akx akx changed the title gh-131197: Add C implementation of UUID stringification gh-131196: Add C implementation of UUID stringification Oct 27, 2025
@akx
Copy link
Contributor Author

akx commented Oct 27, 2025

Would be superseded by #139123 which is far further along in review, so closing :)

@akx akx closed this Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant