Skip to content

Conversation

@henryiii
Copy link
Contributor

@henryiii henryiii commented Nov 26, 2025

I was looking at the Python 3.15 flamegraph for SpecifierSet's (using all python-required on PyPI), and noticed a lot of time was spent inside this singledispatch. Dropping it simplifies the code and makes creating SpecifierSet's take 7% less time.

Signed-off-by: Henry Schreiner <henryfs@princeton.edu>

if isinstance(version, str):
try:
version = str(Version(version))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't str redundant here?

Copy link
Contributor Author

@henryiii henryiii Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think mypy had an issue without it? I know it was acting up here, it didn't like some variations.

Almost sure mypy gets confused in the except block without it. The other fix was to make this a new variable but then I have to add an else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that makes sense, we're inside an isinstance(version, str) block, so MyPy is not going to be happy if you assign version a type which it isn't declared for, even if it's not actually needed.

@notatallshaw
Copy link
Member

Dropping it simplifies the code

I agree, I had to read up on this single dispatch pattern, it might be helpful when dealing with a large array of types, but we only have two types here.

@henryiii
Copy link
Contributor Author

It's really nice for a specific type of design where you register every type you know how to handle. I use it in uproot-browser to register types we know how to plot, it's nice there. Not great for this, though, and I don't think I've seen a registered dispatch call the original like this before.

@henryiii henryiii merged commit cf45527 into pypa:main Nov 27, 2025
40 checks passed
@henryiii henryiii deleted the henryiii/perf/canonicalize_version branch November 27, 2025 07:19
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