Repository navigation
Conversation
Documentation build overview
5 files changed ·
|
|
I just saw #139123, it's already merged, so i'm sending this here Is there any reason to compare pids in the lock function instead of invalidating the state in atfork? glibc does not cache getpid, syscalls are in fact quite slow for a hot loop, and pid comparison is imperfect, see #160215. also does it call GetCurrentProcessId on windows for funsies? you can't fork on windows safely, you can do something cursed like calling NtCreateProcess with null pointer, but its undocumented and breaks tons of other things, so it's up to the developer of such programs to fix it finally, you probably can store the state in tls instead of making it global, this should greatly improve the free-threaded performance |
Follow-up to #139123; refs #139122.
Add a C implementation of
uuid.uuid6(), keeping the pure Python implementation as a fallback. Both implementations now use OS entropy for default clock sequences. The C implementation caches the default node ID per module, with a separate mutex for initialization and an atomic cache lookup. Critical sections around shared timestamp and entropy state stay short, and Python callbacks run outside the timestamp update.The tests compare the Python and C implementations byte for byte with matching time and entropy streams, including repeated and backward timestamps, argument masking, entropy buffer refills, and interleaved UUID4/6/7 calls. Additional tests cover callback failures, node cache initialization and reset, reentrancy, and concurrent calls.
Validation:
test_uuidsuite passed on debug GIL and free-threaded builds, including-R 3:3reference leak checks.-O3GIL and free-threaded builds; the expanded equivalence tests also passed on both.The benchmark below generates UUIDs for five seconds per implementation, retains them in per-thread sets, and checks for duplicates across all threads afterward. Each implementation runs in a fresh process. Sample results on non-debug
-O3builds (no PGO):These are individual runs and include set insertion costs. No duplicates were detected in the C runs. The eight-thread pure Python run produced 13 duplicates, exposing an existing race in its timestamp update; fixing that fallback race remains separate work. The new deterministic equivalence tests cover sequential behavior, while the concurrency tests exercise the C implementation.
Benchmark script (save as bench_uuid6.py)