23 Commits
Author SHA1 Message Date
Yury Selivanov cf690004e3 gh-139122: Reimplement base UUID type, uuid4(), and uuid7() in C (#139123)
The C implementation considerably boosts the performance of the key UUID operations:

------------------------------------
Operation                    Speedup
------------------------------------
uuid4() generation            15.01x
uuid7() generation            29.64x
UUID from string               6.76x
UUID from bytes                5.16x
str(uuid) conversion           6.66x
------------------------------------

Summary of changes:

* The UUID type is reimplemented in C in its entirety.

* The pure-Python is kept around and is used of the C implementation
  isn't available for some reason.

* Both implementations are tested extensively; additional tests are
  added to ensure that the C implementation of the type follows the pure
  Python implementation fully.

* The Python implementation stores UUID values as int objects. The C
  implementation stores them as `uint8_t[16]` array.

* The C implementation has faster hash() implementation but also caches
  the computed hash value to speedup cases when UUIDs are used as
  set/dict keys.

* The C implementation has a freelist to make new UUID object
  instantiation as fast as possible.

* uuid4() and uuid7() are now implmented in C. The most performance
  boost (10x) comes from overfetching entropy to decrease the number of
  _PyOS_URandom() calls. On its own it's a safe optimization with the
  edge case that Unix fork needs to be explicitly handled. We do that by
  comparing the current PID to the PID of when the random buffer was
  populated.

* Portions of code are coming from my implementation of faster UUID
  in gel-python [1]. I did use AI during the development, but basically
  had to rewrite the code it generated to be more idiomatic and
  efficient.

* The benchmark can be found here [2].

* This PR makes Python UUID operations as fast as they are in NodeJS and
  Bun runtimes.

[1] https://github.com/MagicStack/py-pgproto/blob/b8109fb311a59f30f9947567a13508da9a776564/uuid.pyx

[2] https://gist.github.com/1st1/f03e816f34a61e4d46c78ff98baf4818
2026-10-21 14:17:07 -07:00
thexai ec1057ebb6 gh-152433: Windows: `_uuid` module: improve UWP compatibility (#152793) 2026-07-19 15:19:40 +02:00
Bénédikt Tran 3bffada467 gh-132710: only use stable _uuid.generate_time_safe() to deduce MAC address (#132901) 2025-05-26 10:56:31 +02:00
Brett Simmers c2627d6eea gh-116322: Add Py_mod_gil module slot (#116882)
This PR adds the ability to enable the GIL if it was disabled at
interpreter startup, and modifies the multi-phase module initialization
path to enable the GIL when loading a module, unless that module's spec
includes a slot indicating it can run safely without the GIL.

PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went
with `Py_MOD_GIL_NOT_USED` for consistency with gh-104148.

A warning will be issued up to once per interpreter for the first
GIL-using module that is loaded. If `-v` is given, a shorter message
will be printed to stderr every time a GIL-using module is loaded
(including the first one that issues a warning).
2024-05-03 11:30:55 -04:00
Victor Stinner 2b67fc57f6 gh-108494: Fix Argument Clinic LIMITED_CAPI_REGEX (#116610)
Accept spaces in "#  define Py_LIMITED_API 0x030d0000".
2024-03-11 22:42:18 +00:00
Steve Dower 79dad03747 gh-111650: Ensure pyconfig.h includes Py_GIL_DISABLED on Windows (GH-112778) 2023-12-13 15:38:45 +00:00
Hugo van Kemenade 3b3ec0d77f gh-111863: Rename Py_NOGIL to Py_GIL_DISABLED (#111864)
Rename Py_NOGIL to Py_GIL_DISABLED
2023-11-20 15:52:00 +02:00
Victor Stinner ef83b3fc00 gh-110968: Py_MOD_PER_INTERPRETER_GIL_SUPPORTED was added to 3.12 (#111584) 2023-11-01 04:24:33 +01:00
Sam Gross 6dfb8fe023 gh-110481: Implement biased reference counting (gh-110764) 2023-10-30 16:06:09 +00:00
Victor Stinner 73a003f646 gh-85283: Build _uuid extension with limited C API (#111010) 2023-10-17 23:07:12 +00:00
Inada Naoki d5bd32fb48 gh-104922: remove PY_SSIZE_T_CLEAN (#106315) 2023-07-02 15:07:46 +09:00
Eric Snow a9c6e0618f gh-99113: Add Py_MOD_PER_INTERPRETER_GIL_SUPPORTED (gh-104205)
Here we are doing no more than adding the value for Py_mod_multiple_interpreters and using it for stdlib modules.  We will start checking for it in gh-104206 (once PyInterpreterState.ceval.own_gil is added in gh-104204).
2023-05-05 21:11:27 +00:00
Christian Heimes 8af6481f6b bpo-45847: Port _uuid to PY_STDLIB_MOD (GH-29741) 2021-11-24 10:20:37 +01:00
Steve Dower d6b727e2c9 bpo-40501: Replace ctypes code in uuid with native module (GH-19948) 2020-05-12 23:32:32 +01:00
Dong-hee Na 1cb763b880 bpo-1635741: Port _uuid module to multiphase initialization (GH-19242) 2020-03-31 14:43:47 +02:00
ziheng 6ffd9b05df bpo-32627: Fix compile error when conflicting _uuid headers included (GH-11751) 2019-06-24 13:59:50 -04:00
Serhiy Storchaka 17d8830312 bpo-32493: Fix uuid.uuid1() on FreeBSD. (GH-7099)
Use uuid_enc_be() if available to encode UUID to bytes as big endian.
2018-05-25 00:45:09 +02:00
Siddhesh Poyarekar 55edd0c185 bpo-33012: Fix invalid function cast warnings with gcc 8 for METH_NOARGS. (GH-6030)
METH_NOARGS functions need only a single argument but they are cast
into a PyCFunction, which takes two arguments.  This triggers an
invalid function cast warning in gcc8 due to the argument mismatch.
Fix this by adding a dummy unused argument.
2018-04-29 21:59:33 +03:00
David Carlier b4ebaa7099 bpo-32493: Not only AIX, but FreeBSD has uuid_create support (#5089)
Allow building the _uuid extension module on FreeBSD and OpenBSD.
2018-01-09 20:38:07 +01:00
Michael Felt 0d3ccb4395 bpo-32399: Starting with AIX6.1 there is support in libc.a for uuid (RFC4122) (#4974)
Starting with AIX6.1 there is support in libc.a for uuid (RFC4122)
This patch provides the changes needed for this integration with the OS.

On AIX the base function is uuid_create() rather than uuid_generate_time()
The AIX uuid_t typedef is more aligned to the UUID field based definition
while the Linux typedef that is more aligned with UUID bytes
(or perhaps UUID bytes_le) definitions.
2017-12-30 22:39:20 +01:00
Berker Peksag 9a10ff4deb bpo-11063: Add a configure check for uuid_generate_time_safe (GH-4287) 2017-11-08 23:09:16 +03:00
Victor Stinner 4337a0d995 bpo-11063: Fix _uuid module on macOS (#3855)
On macOS, use uuid_generate_time() instead of
uuid_generate_time_safe() of libuuid, since uuid_generate_time_safe()
is not available.
2017-10-02 07:57:59 -07:00
Antoine Pitrou a106aec2ed bpo-11063, bpo-20519: avoid ctypes and improve import time for uuid (#3796)
bpo-11063, bpo-20519: avoid ctypes and improve import time for uuid.
2017-09-28 23:03:06 +02:00