summaryrefslogtreecommitdiff
path: root/mesonbuild/utils
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2025-01-08 12:51:46 +0100
committerEli Schwartz <eschwartz93@gmail.com>2025-01-08 13:45:26 -0500
commit95d0c40da5de2b85f11b9ccda93ed21969327464 (patch)
tree1b1f3b9e26560afa23a502654c3fd0cc4aa23f9f /mesonbuild/utils
parentff5b5e66c9a759573af5f265a3404c3a144489de (diff)
downloadmeson-95d0c40da5de2b85f11b9ccda93ed21969327464.tar.gz
optimize Version.__init__
Version objects are created thousands of times by the check_version method of decorators. Creation is inefficient, resulting in five calls to re.match() that do not even use precompiled regex. The use of re.match() however is fully redundant, as finditer() can provide the same information with a better use of groupings. Do that and precompile the tokenization regex. This saves about 3% in the "meson setup" run of QEMU. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'mesonbuild/utils')
-rw-r--r--mesonbuild/utils/universal.py15
1 files changed, 6 insertions, 9 deletions
diff --git a/mesonbuild/utils/universal.py b/mesonbuild/utils/universal.py
index eede9fd2d..e2634b925 100644
--- a/mesonbuild/utils/universal.py
+++ b/mesonbuild/utils/universal.py
@@ -828,21 +828,18 @@ def current_vs_supports_modules() -> bool:
return True
return vsver.startswith('16.9.0') and '-pre.' in vsver
+_VERSION_TOK_RE = re.compile(r'(\d+)|([a-zA-Z]+)')
+
# a helper class which implements the same version ordering as RPM
class Version:
def __init__(self, s: str) -> None:
self._s = s
- # split into numeric, alphabetic and non-alphanumeric sequences
- sequences1 = re.finditer(r'(\d+|[a-zA-Z]+|[^a-zA-Z\d]+)', s)
-
- # non-alphanumeric separators are discarded
- sequences2 = [m for m in sequences1 if not re.match(r'[^a-zA-Z\d]+', m.group(1))]
-
+ # extract numeric and alphabetic sequences
# numeric sequences are converted from strings to ints
- sequences3 = [int(m.group(1)) if m.group(1).isdigit() else m.group(1) for m in sequences2]
-
- self._v = sequences3
+ self._v = [
+ int(m.group(1)) if m.group(1) else m.group(2)
+ for m in _VERSION_TOK_RE.finditer(s)]
def __str__(self) -> str:
return '{} (V={})'.format(self._s, str(self._v))