From 0710549d83f22560726e5f09bef0c3013fd838c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 15:18:48 +0100 Subject: [PATCH] revert UUIDv1 construction --- Doc/whatsnew/3.14.rst | 14 +++++--------- Lib/uuid.py | 10 ++++------ .../2024-12-21-11-12-50.gh-issue-128151.aq7vpG.rst | 4 ++-- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 7783064465c745..8d55e9e3b2af2c 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -675,18 +675,14 @@ uuid * Improve generation of :class:`~uuid.UUID` objects via their dedicated functions: - * For a given 48-bit hardware address *node* and a given 14-bit - clock sequence *clock_seq*, :func:`uuid1(node=node) ` - and :func:`uuid1(clock_seq=clock_seq) ` are 35% faster. - Performances for :func:`~uuid.uuid1` remain unchanged when neither - the hardware address nor the clock sequence is specified. - * :func:`~uuid.uuid3` is 27% faster for 16-byte names and 8% faster + * :func:`~uuid.uuid3` is 40% faster for 16-byte names and 10% faster for 1024-byte names. Performances for longer names remain unchanged. - * :func:`~uuid.uuid5` is 24% faster for 16-byte names and 11% faster + * :func:`~uuid.uuid5` is 38% faster for 16-byte names and 21% faster for 1024-byte names. Performances for longer names remain unchanged. - * :func:`~uuid.uuid4` and :func:`~uuid.uuid8` are 20% faster. + * :func:`~uuid.uuid4` is 31% faster and :func:`~uuid.uuid8` is 37% faster. - Overall, dedicated generation of UUID objects is 20% faster. + Overall, dedicated generation of UUID objects version 3, 4, 5, and 8 is + roughly 30% faster. (Contributed by Bénédikt Tran in :gh:`128150`.) diff --git a/Lib/uuid.py b/Lib/uuid.py index 56071b58993f09..d68007ba295ca2 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -710,14 +710,12 @@ def uuid1(node=None, clock_seq=None): time_low = timestamp & 0xffffffff time_mid = (timestamp >> 32) & 0xffff time_hi_version = (timestamp >> 48) & 0x0fff + clock_seq_low = clock_seq & 0xff + clock_seq_hi_variant = (clock_seq >> 8) & 0x3f if node is None: node = getnode() - clock_seq = clock_seq & 0x3fff - int_uuid_1 = ((time_low << 96) | (time_mid << 80) | - (time_hi_version << 64) | (clock_seq << 48) | node) - # by construction, the variant and version bits are already cleared - int_uuid_1 |= _RFC_4122_VERSION_1_FLAGS - return UUID(int=int_uuid_1, version=None) + return UUID(fields=(time_low, time_mid, time_hi_version, + clock_seq_hi_variant, clock_seq_low, node), version=1) def uuid3(namespace, name): """Generate a UUID from the MD5 hash of a namespace UUID and a name.""" diff --git a/Misc/NEWS.d/next/Library/2024-12-21-11-12-50.gh-issue-128151.aq7vpG.rst b/Misc/NEWS.d/next/Library/2024-12-21-11-12-50.gh-issue-128151.aq7vpG.rst index 1a17fa27535004..04c744fb2ba54f 100644 --- a/Misc/NEWS.d/next/Library/2024-12-21-11-12-50.gh-issue-128151.aq7vpG.rst +++ b/Misc/NEWS.d/next/Library/2024-12-21-11-12-50.gh-issue-128151.aq7vpG.rst @@ -1,2 +1,2 @@ -Improve generation of :class:`~uuid.UUID` objects via their dedicated -functions by 20%. Patch by Bénédikt Tran. +Improve generation of :class:`~uuid.UUID` objects version 3, 4, 5, and 8 +via their dedicated functions by 30%. Patch by Bénédikt Tran.