Improve performance of name normalization by jaraco · Pull Request #533 · python/importlib_metadata · GitHub
Skip to content

Improve performance of name normalization - #533

Merged
jaraco merged 7 commits into
maint/8.xfrom
backport-cpython-143658
Mar 20, 2026
Merged

jaraco merged 7 commits into
maint/8.xfrom
backport-cpython-143658

Conversation

@jaraco

@jaraco jaraco commented Mar 20, 2026

Copy link
Copy Markdown
Member

Backport of changes from python/cpython#143658

hugovk and others added 5 commits March 20, 2026 03:04
…ance of `importlib.metadata.Prepared.normalized` (#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>
…ce of `importlib.metadata.Prepared.normalized` (#144083)

Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
@jaraco

jaraco commented Mar 20, 2026

Copy link
Copy Markdown
Member Author

@jaraco

jaraco commented Mar 20, 2026

Copy link
Copy Markdown
Member Author

Running timeit directly reveals some run time:

 🐚 .tox/py/bin/python -m timeit --setup 'import importlib_metadata' -- 'importlib_metadata.Prepared("sample")'
1000000 loops, best of 5: 231 nsec per loop

Aah - so 231 nsec is smaller than the precision of Python's timespan, which only handles microseconds.

 importlib_metadata backport-cpython-143658 🐚 pip-run tempora
Python 3.14.3 (main, Feb  3 2026, 15:32:20) [Clang 17.0.0 (clang-1700.6.3.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempora
>>> tempora.parse_timedelta('231 nsec')
datetime.timedelta(0)

@jaraco

jaraco commented Mar 20, 2026

Copy link
Copy Markdown
Member Author

The latest benchmark shows a 74% reduction in execution time.

exercises.py:normalize_perf: 0:00:00.000100 (+-1 day, 23:59:59.999722, -74%)

I get different numbers when I run timeit manually (~230ns vs ~510ns, 55% reduction).

Was it worth it to save a few hundred nanoseconds?

@hugovk

hugovk commented Mar 20, 2026

Copy link
Copy Markdown
Member

In many cases, the speedup won't make any noticeable effect. The change is similar to an improvement from packaging, which was does have real benefits for pip when doing large resolutions (https://iscinumpy.dev/post/packaging-faster/).

Let's also ask if @henryiii is aware of any use cases.

Personally, I would make this change, I don't think it makes the code harder to read or maintain. But it's fine if you'd prefer to reject this and/or revert in CPython.


btw I'd already opened #529 against main, and this is against maint/8.x. I couldn't find a contrib guide, what's the normal workflow? Can this be documented somewhere, or did I miss it?

This is the better PR -- same prod code change, but parametrised tests and a performance benchmark added.

@jaraco

jaraco commented Mar 20, 2026

Copy link
Copy Markdown
Member Author

btw I'd already opened #529 against main, and this is against maint/8.x. I couldn't find a contrib guide, what's the normal workflow? Can this be documented somewhere, or did I miss it?

Thanks for that PR; I'd just not gotten to it as my attention/bandwidth are limited.

There's some documentation in https://github.com/python/importlib_metadata/wiki/Development-Methodology. And the contribution to main was the correct thing at the time. But because I'm now getting this change in place (along with some others) and there's a 9.0 release that I'd like to exclude for the time being, that's why I'm targeting 8.x.

…termediate implementations. Reference the rationale.
@jaraco

jaraco commented Mar 20, 2026

Copy link
Copy Markdown
Member Author

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