Skip to content

Conversation

@henryiii
Copy link
Contributor

@henryiii henryiii commented Nov 27, 2025

NamedTuples are expensive. This one line sticks out in the flame graph from 3.15's profiler. Dropping the _Version named tuple speeds up Version creation, taking 20% less time; it also simplifies the code a bit. Since we already provide read-only indirection for all the attributes (and I assume the NamedTuple was to ensure the values were not mutable), this remains safe.

We could provide a backward compatibility property _version attribute that generates this NamedTuple (and even a setter to be perfectly backward compatible), though if we did that I'd like to also have it generate a deprecation warning; no one should be using our internals ideally. :)

Version creation is a key part of SpecifierSet creation too, I was noticing this line on the flame graph for SpecifierSet.

@henryiii henryiii changed the title perf: drop _Version namedtuple perf: drop _Version NamedTuple Nov 27, 2025
@henryiii henryiii force-pushed the henryiii/perf/_version branch from 96c8f2c to 626a598 Compare November 27, 2025 06:03
Copy link
Member

@notatallshaw notatallshaw left a comment

Choose a reason for hiding this comment

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

I think _Version quite nicely collects all the canonical components of a Version, if this weren't performance critical code I might be against this, but the Version object can be used millions, possibly billions, of times in a resolver.

uv has put a lot of effort into making their representation of a version object super efficient, and that has allowed them to push their resolver forward. Hopefully a more efficient packaging Version will help the resolvers that use it.

@henryiii henryiii force-pushed the henryiii/perf/_version branch 2 times, most recently from 835a2c2 to 95ba059 Compare November 27, 2025 15:57
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
@henryiii henryiii force-pushed the henryiii/perf/_version branch from 95ba059 to 3c2794a Compare November 27, 2025 16:04
@henryiii
Copy link
Contributor Author

I can't find anyone using ._version, so since I have an approval, I'll put this in, but if anyone does need this added for backcompat, it's really easy to add a @property that returns the old NamedTuple.

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