Skip to content

Commit baa9ea8

Browse files
authored
perf: skip hash property (#1115)
1 parent e839d73 commit baa9ea8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

benchmarks/version.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def time_hash(self) -> None:
4141
for v in self.valid_versions:
4242
hash(Version(v))
4343

44+
@add_attributes(pretty_name="Version hash (warm cache)")
45+
def time_hash_warm(self) -> None:
46+
for v in self.version_objects_warm:
47+
hash(v)
48+
4449
@add_attributes(pretty_name="Version __str__")
4550
def time_str(self) -> None:
4651
for version in self.valid_versions:

src/packaging/version.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,16 @@ def _key(self) -> CmpKey:
551551
# __hash__ must be defined when __eq__ is overridden,
552552
# otherwise Python sets __hash__ to None.
553553
def __hash__(self) -> int:
554-
return hash(self._key)
554+
if self._key_cache is None:
555+
self._key_cache = _cmpkey(
556+
self._epoch,
557+
self._release,
558+
self._pre,
559+
self._post,
560+
self._dev,
561+
self._local,
562+
)
563+
return hash(self._key_cache)
555564

556565
# Override comparison methods to use direct _key_cache access
557566
# This is faster than property access, especially before Python 3.12

0 commit comments

Comments
 (0)