diff options
author | Tom Scogland <tom.scogland@gmail.com> | 2022-03-15 11:53:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-15 18:53:28 +0000 |
commit | 94794d061afb007e06cbecad047fca411aa31d7b (patch) | |
tree | 8668688595ea6fbd6bd3cf46998c2491f9baa0c6 /lib | |
parent | 1250fac46795edeaa677b4a1188f402ed41cac84 (diff) | |
download | spack-94794d061afb007e06cbecad047fca411aa31d7b.tar.gz spack-94794d061afb007e06cbecad047fca411aa31d7b.tar.bz2 spack-94794d061afb007e06cbecad047fca411aa31d7b.tar.xz spack-94794d061afb007e06cbecad047fca411aa31d7b.zip |
optimize instantiation and comparison of versions (#29429)
Re-work the checks and comparisons around commit versions, when no
commit version is involved the overhead is now in the noise, where one
is the overhead is now constant rather than linear.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/spack/version.py | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/lib/spack/spack/version.py b/lib/spack/spack/version.py index 9108b1feb5..d3dd01c3a5 100644 --- a/lib/spack/spack/version.py +++ b/lib/spack/spack/version.py @@ -48,7 +48,7 @@ __all__ = ['Version', 'VersionRange', 'VersionList', 'ver'] VALID_VERSION = re.compile(r'^[A-Za-z0-9_.-]+$') # regex for a commit version -COMMIT_VERSION = re.compile(r'^[a-z0-9]{40}$') +COMMIT_VERSION = re.compile(r'^[a-f0-9]{40}$') # regex for version segments SEGMENT_REGEX = re.compile(r'(?:(?P<num>[0-9]+)|(?P<str>[a-zA-Z]+))(?P<sep>[_.-]*)') @@ -167,7 +167,14 @@ class VersionStrComponent(object): class Version(object): """Class to represent versions""" - __slots__ = ['version', 'separators', 'string', 'is_commit', 'commit_lookup'] + __slots__ = [ + "version", + "separators", + "string", + "commit_lookup", + "is_commit", + "commit_version", + ] def __init__(self, string): if not isinstance(string, str): @@ -182,7 +189,7 @@ class Version(object): # An object that can lookup git commits to compare them to versions self.commit_lookup = None - + self.commit_version = None segments = SEGMENT_REGEX.findall(string) self.version = tuple( int(m[0]) if m[0] else VersionStrComponent(m[1]) for m in segments @@ -195,6 +202,8 @@ class Version(object): commit_lookup = self.commit_lookup or other_lookups if self.is_commit and commit_lookup: + if self.commit_version is not None: + return self.commit_version commit_info = commit_lookup.get(self.string) if commit_info: prev_version, distance = commit_info @@ -203,7 +212,8 @@ class Version(object): # If commit is exactly a known version, no distance suffix prev_tuple = Version(prev_version).version if prev_version else () dist_suffix = (VersionStrComponent(''), distance) if distance else () - return prev_tuple + dist_suffix + self.commit_version = prev_tuple + dist_suffix + return self.commit_version return self.version |