gh-143658: Use str.lower and replace to further improve performance of importlib.metadata.Prepared.normalized
#144083
+6
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Follow on from #143660 which replaced
re.subwithstr.translate.@henryiii discovered that whilst that is an improvement on Python 3.10-3.11 and 3.14, the performance of
str.translateis much worse on Python 3.12 and 3.13, and can be worse than the originalre.sub.Further, his fix in pypa/packaging#1064 to replace
str.translatewithstr.lowerandstr.replacecalls 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:
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
packagingthat support a wide range of Pythons,importlib.metadatadoes have a backport, and I'll update python/importlib_metadata#529 once this is merged.str.translateto improve performance ofimportlib.metadata.Prepared.normalized#143658