gh-143658: importlib.metadata: Use str.translate to improve performance of importlib.metadata.Prepared.normalized#143660
Conversation
Co-Authored-By: Henry Schreiner <henryschreineriii@gmail.com>
translate to improve performance of canonicalize_nametranslate to improve performance of canonicalize_name
Misc/NEWS.d/next/Library/2026-01-10-15-40-57.gh-issue-143658.Ox6pE5.rst
Outdated
Show resolved
Hide resolved
translate to improve performance of canonicalize_namestr.translate to improve performance of importlib.metadata.Prepared.normalized
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
picnixz
left a comment
There was a problem hiding this comment.
Do we have tests actually? if not, maybe it'd be good to add some.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
|
See #144083 for a followup. |
…erformance of `importlib.metadata.Prepared.normalized` (python#143660) Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
|
|
||
| class PreparedTests(unittest.TestCase): | ||
| def test_normalize(self): | ||
| tests = [ |
There was a problem hiding this comment.
I'd use fixtures.parameterize for the parameters.
|
This change needs to be ported to importlib_metadata to avoid getting overridden when syncing changes from that repo. I'd have contributed this change there instead, because that project provides performance tests for measuring the effect of changes such as these. |
Please see PR python/importlib_metadata#529, and let's fix the test parameters over there first.
Good to know, will target there first in the future. |
We can apply @henryiii's improvement to
packagingin pypa/packaging#1030 (see also https://iscinumpy.dev/post/packaging-faster/) to improve the performance ofcanonicalize_nameand make it ~3.7 times faster.Benchmark
Run
Prepared.normalize(n)on every name in PyPI:Benchmark data can be found at https://gist.github.com/hugovk/efdbee0620cc64df7b405b52cf0b6e42
Before
With optimisations:
After
3.7 times faster.
str.translateto improve performance ofimportlib.metadata.Prepared.normalized#143658