Skip to content

perf: skip hash property#1115

Merged
notatallshaw merged 2 commits intopypa:mainfrom
notatallshaw:version/skip-hash-property
Mar 11, 2026
Merged

perf: skip hash property#1115
notatallshaw merged 2 commits intopypa:mainfrom
notatallshaw:version/skip-hash-property

Conversation

@notatallshaw
Copy link
Member

Like all the other methods on Version, it provides a small performance improvement to skip accessing the property. In this case we don't have an other to check the instance on.

Access _key_cache directly and inline _cmpkey call in __hash__ instead of
going through the _key property. This avoids property descriptor overhead
on every hash call, matching the pattern already used by the comparison
method overrides.
@notatallshaw
Copy link
Member Author

It's fairly mild performance improvement but it's there:


|          | 4.45±0.05ms          | 4.32±0.02ms         |    0.97 | version.TimeVersionSuite.time_hash [runnervm0kj6c/virtualenv-py3.10-PYTHONHASHSEED0]                   |
|          | 2.94±0.01ms          | 2.85±0.01ms         |    0.97 | version.TimeVersionSuite.time_hash [runnervm0kj6c/virtualenv-py3.11-PYTHONHASHSEED0]                   |
|          | 3.56±0.03ms          | 3.51±0.03ms         |    0.98 | version.TimeVersionSuite.time_hash [runnervm0kj6c/virtualenv-py3.12-PYTHONHASHSEED0]                   |
|          | 3.33±0.01ms          | 3.33±0.03ms         |    1    | version.TimeVersionSuite.time_hash [runnervm0kj6c/virtualenv-py3.13-PYTHONHASHSEED0]                   |
|          | 3.22±0.04ms          | 3.18±0.03ms         |    0.99 | version.TimeVersionSuite.time_hash [runnervm0kj6c/virtualenv-py3.14-PYTHONHASHSEED0]                   |
|          | 4.72±0.02ms          | 4.64±0.01ms         |    0.98 | version.TimeVersionSuite.time_hash [runnervm0kj6c/virtualenv-py3.8-PYTHONHASHSEED0]                    |
|          | 4.54±0.03ms          | 4.49±0.02ms         |    0.99 | version.TimeVersionSuite.time_hash [runnervm0kj6c/virtualenv-py3.9-PYTHONHASHSEED0]                    |
|          | 1.13±0.01ms          | 1.04±0.01ms         |    0.93 | version.TimeVersionSuite.time_hash_warm [runnervm0kj6c/virtualenv-py3.10-PYTHONHASHSEED0]              |
|          | 735±6μs              | 672±3μs             |    0.91 | version.TimeVersionSuite.time_hash_warm [runnervm0kj6c/virtualenv-py3.11-PYTHONHASHSEED0]              |
|          | 885±8μs              | 857±6μs             |    0.97 | version.TimeVersionSuite.time_hash_warm [runnervm0kj6c/virtualenv-py3.12-PYTHONHASHSEED0]              |
|          | 886±3μs              | 866±6μs             |    0.98 | version.TimeVersionSuite.time_hash_warm [runnervm0kj6c/virtualenv-py3.13-PYTHONHASHSEED0]              |
| -        | 207±7μs              | 181±6μs             |    0.87 | version.TimeVersionSuite.time_hash_warm [runnervm0kj6c/virtualenv-py3.14-PYTHONHASHSEED0]              |
|          | 1.16±0ms             | 1.08±0ms            |    0.93 | version.TimeVersionSuite.time_hash_warm [runnervm0kj6c/virtualenv-py3.8-PYTHONHASHSEED0]               |
|          | 1.08±0.01ms          | 1000±6μs            |    0.93 | version.TimeVersionSuite.time_hash_warm [runnervm0kj6c/virtualenv-py3.9-PYTHONHASHSEED0]               |

Copy link
Contributor

@henryiii henryiii left a comment

Choose a reason for hiding this comment

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

Clearly measurable. I wonder if it's true that 3.14 is the fastest - maybe it's optimizing code better and this is more optimizable.

@notatallshaw
Copy link
Member Author

Clearly measurable. I wonder if it's true that 3.14 is the fastest - maybe it's optimizing code better and this is more optimizable.

I don't see Python 3.14 as an outlier running the benchmark a few times locally ¯\(ツ)

@notatallshaw notatallshaw force-pushed the version/skip-hash-property branch from f3d84d4 to ceff51b Compare March 11, 2026 15:38
@notatallshaw notatallshaw merged commit baa9ea8 into pypa:main Mar 11, 2026
110 checks passed
@henryiii
Copy link
Contributor

Okay, probably just had a good or bad CI run then.

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