version.py: set is_commit in constructor (#29369)

Speeds up comparison on `Version` by ~2.5x, e.g.

```python
In [1]: v = spack.version.Version('1.0.0'); w = spack.version.Version('1.0.2')

In [2]: %timeit v < w

1.47 µs ± 5.59 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

535 ns ± 1.75 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
```
This commit is contained in:
Harmen Stoppels 2022-03-08 10:50:48 +01:00 committed by GitHub
parent 4b5e071141
commit a39a6c4ea7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -167,7 +167,7 @@ def __gt__(self, other):
class Version(object):
"""Class to represent versions"""
__slots__ = ['version', 'separators', 'string', 'commit_lookup']
__slots__ = ['version', 'separators', 'string', 'is_commit', 'commit_lookup']
def __init__(self, string):
if not isinstance(string, str):
@ -189,6 +189,8 @@ def __init__(self, string):
)
self.separators = tuple(m[2] for m in segments)
self.is_commit = len(self.string) == 40 and COMMIT_VERSION.match(self.string)
def _cmp(self, other_lookups=None):
commit_lookup = self.commit_lookup or other_lookups
@ -205,15 +207,6 @@ def _cmp(self, other_lookups=None):
return self.version
@property
def is_commit(self):
"""
Determine if the original string is referencing a commit.
"""
if self.string in infinity_versions:
return False
return COMMIT_VERSION.match(self.string) is not None
@property
def dotted(self):
"""The dotted representation of the version.