Skip to content

Conversation

@hugovk
Copy link
Member

@hugovk hugovk commented Jan 20, 2026

Follow on from #143660 which replaced re.sub with str.translate.

@henryiii discovered that whilst that is an improvement on Python 3.10-3.11 and 3.14, the performance of str.translate is much worse on Python 3.12 and 3.13, and can be worse than the original re.sub.

Further, his fix in pypa/packaging#1064 to replace str.translate with str.lower and str.replace calls is better than both the other options across all Python versions, to varying degrees.

I benchmarked all three across all available Python Build Standalone versions from 3.10-3.15 on macOS:

image

I also benchmarked Windows and Ubuntu it's the same pattern.

Whilst the improvement isn't as large for CPython 3.15 as it is for libraries such as packaging that support a wide range of Pythons, importlib.metadata does have a backport, and I'll update python/importlib_metadata#529 once this is merged.

….metadata.Prepared.normalized

Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
@hugovk hugovk requested review from jaraco and warsaw as code owners January 20, 2026 20:09
@hugovk hugovk added performance Performance or resource usage topic-importlib labels Jan 20, 2026
@hugovk hugovk changed the title gh-143660: Use str.lower and replace to further improve performance of importlib.metadata.Prepared.normalized gh-143658: Use str.lower and replace to further improve performance of importlib.metadata.Prepared.normalized Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants