Skip to content

gh-150226: Speed up uuid.UUID string parsing#150227

Closed
esadomer wants to merge 1 commit into
python:mainfrom
esadomer:codex/speed-up-uuid-string-parsing
Closed

gh-150226: Speed up uuid.UUID string parsing#150227
esadomer wants to merge 1 commit into
python:mainfrom
esadomer:codex/speed-up-uuid-string-parsing

Conversation

@esadomer
Copy link
Copy Markdown

@esadomer esadomer commented May 22, 2026

gh-150226

Summary

This speeds up uuid.UUID construction from the documented string forms by handling the documented urn:uuid: prefix directly with str.removeprefix() before stripping braces and hyphens.

The previous broader normalization path is still used as a fallback whenever the direct normalization does not produce 32 hex characters, preserving legacy accepted inputs such as urn:<hex>.

Benchmark

Same-process benchmark on a local Windows CPython debug build, comparing origin/main:Lib/uuid.py with this patch while blocking _uuid to isolate the pure-Python constructor path:

plain_hex:  11584.5 ns -> 11366.8 ns (1.9%)
canonical:  12424.6 ns -> 12253.1 ns (1.4%)
braced:     13293.5 ns -> 12981.1 ns (2.3%)
urn:        13800.0 ns -> 12912.8 ns (6.4%)

Absolute timings are from a debug build; the benchmark compares old and new code in the same process.

Tests

  • PCbuild\build.bat -d -p x64
  • PCbuild\amd64\python_d.exe -m test test_uuid -v

I also attempted a local Windows Release build, but MSVC failed with an internal compiler error while compiling/linking externals\zlib-ng-2.2.4\arch\x86\chunkset_avx2.c, unrelated to this uuid change.

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 22, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@esadomer esadomer force-pushed the codex/speed-up-uuid-string-parsing branch from bdf214f to 95fbb8b Compare May 22, 2026 09:04
@esadomer esadomer force-pushed the codex/speed-up-uuid-string-parsing branch from 95fbb8b to b457838 Compare May 22, 2026 09:57
@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 22, 2026

See issue for the rationale of closing this.

@picnixz picnixz closed this May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants