vercmp: add fast path for when the versions or parts are equal

We compare  the same versions a lot, for example downstream with upstream,
or git with pacman repo.
This commit is contained in:
Christoph Reiter 2025-06-28 20:52:13 +02:00
parent 793616a5b7
commit cade6e9685

View File

@ -22,6 +22,9 @@ logger.setLevel(logging.DEBUG)
def vercmp(v1: str, v2: str) -> int: def vercmp(v1: str, v2: str) -> int:
if v1 == v2:
return 0
def cmp(a: Any, b: Any) -> int: def cmp(a: Any, b: Any) -> int:
res = (a > b) - (a < b) res = (a > b) - (a < b)
assert isinstance(res, int) assert isinstance(res, int)
@ -72,6 +75,9 @@ def vercmp(v1: str, v2: str) -> int:
return parts return parts
def rpmvercmp(v1: str, v2: str) -> int: def rpmvercmp(v1: str, v2: str) -> int:
if v1 == v2:
return 0
for x1, x2 in zip_longest(parse(v1), parse(v2), fillvalue=None): for x1, x2 in zip_longest(parse(v1), parse(v2), fillvalue=None):
if x1 is None: if x1 is None:
assert x2 is not None assert x2 is not None