New time-based UUID formats which are suited for use as a database key
Project description
uuid6
New time-based UUID formats which are suited for use as a database key.
This module extends immutable UUID objects (the UUID class) with the functions uuid6()
and uuid7()
from the IETF draft.
Install
pip install uuid6
Usage
from uuid6 import uuid6, uuid7
my_uuid = uuid6()
print(my_uuid)
assert my_uuid < uuid6()
my_uuid = uuid7()
print(my_uuid)
assert my_uuid < uuid7()
UUIDv6 Field and Bit Layout
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| time_high |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| time_mid | time_low_and_version |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|clk_seq_hi_res | clk_seq_low | node (0-1) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| node (2-5) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
UUIDv7 Field and Bit Layout
Draft 02
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| unixts |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|unixts | subsec_a | ver | subsec_b |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|var| subsec_seq_node |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| subsec_seq_node |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
This implementation
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| unixts |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|unixts | subsec_a | ver | subsec_b |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|var| subsec_c | rand |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| rand |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
unixts
: 36 bit big-endian unsigned Unix Timestamp valuesubsec_a
: 12 bits allocated to sub-second precision valuesver
: The 4 bit UUIDv7 version (0111)subsec_b
: 12 bits allocated to sub-second precision valuesvar
: 2 bit UUID variant (10)subsec_c
: 6 bits allocated to sub-second precision valuesrand
: The remaining 56 bits are filled with pseudo-random data
30 bits dedicated to sub-second precision provide nanosecond resolution. The unixts
and subsec
fields guarantee the order of UUIDs generated within the same nanosecond by monotonically incrementing the timer.
This implementation does not include a clock sequence counter as defined in the draft RFC.
Performance
Run the shell script bench.sh to test on your own machine.
Results
MacBook Air 2020
Python 3.10.2
Mean +- std dev: 1.02 us +- 0.01 us
Mean +- std dev: 1.11 us +- 0.01 us
Mean +- std dev: 2.34 us +- 0.02 us
Mean +- std dev: 2.04 us +- 0.02 us
+-----------+---------+-----------------------+-----------------------+-----------------------+
| Benchmark | uuid1 | uuid4 | uuid6 | uuid7 |
+===========+=========+=======================+=======================+=======================+
| timeit | 1.02 us | 1.11 us: 1.08x slower | 2.34 us: 2.28x slower | 2.04 us: 1.99x slower |
+-----------+---------+-----------------------+-----------------------+-----------------------+
Google Cloud Shell VM
Python 3.7.3
Mean +- std dev: 10.1 us +- 0.7 us
Mean +- std dev: 4.25 us +- 0.79 us
Mean +- std dev: 9.37 us +- 1.75 us
Mean +- std dev: 7.51 us +- 1.42 us
+-----------+---------+-----------------------+-----------------------+-----------------------+
| Benchmark | uuid1 | uuid4 | uuid6 | uuid7 |
+===========+=========+=======================+=======================+=======================+
| timeit | 10.1 us | 4.25 us: 2.38x faster | 9.37 us: 1.08x faster | 7.51 us: 1.35x faster |
+-----------+---------+-----------------------+-----------------------+-----------------------+
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.