diff options
| author | Paolo Bonzini <pbonzini@redhat.com> | 2024-11-07 12:15:09 +0100 |
|---|---|---|
| committer | Eli Schwartz <eschwartz93@gmail.com> | 2024-12-29 23:22:14 -0500 |
| commit | 3b36cb2c2c5e2978cc38283e4aa5d71ab5da4335 (patch) | |
| tree | 7d16db6b24a0b33f6c455589d61dad061ecd5836 | |
| parent | c9635daeba4c7de7c4a230d6c9088f94f06ee8dc (diff) | |
| download | meson-3b36cb2c2c5e2978cc38283e4aa5d71ab5da4335.tar.gz | |
ninjabackend: prefer "in" to regex search
Regexes can be surprisingly slow. This small change brings
ninja_quote() from 12 to 3 seconds when building QEMU.
Before:
ncalls tottime percall cumtime percall
3734443 4.872 0.000 11.944 0.000
After:
ncalls tottime percall cumtime percall
3595590 3.193 0.000 3.196 0.000
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Eli Schwartz <eschwartz93@gmail.com>
| -rw-r--r-- | mesonbuild/backend/ninjabackend.py | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 0c34e08de..7244ea90b 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -123,13 +123,6 @@ NINJA_QUOTE_BUILD_PAT = re.compile(r"[$ :\n]") NINJA_QUOTE_VAR_PAT = re.compile(r"[$ \n]") def ninja_quote(text: str, is_build_line: bool = False) -> str: - if is_build_line: - quote_re = NINJA_QUOTE_BUILD_PAT - else: - quote_re = NINJA_QUOTE_VAR_PAT - # Fast path for when no quoting is necessary - if not quote_re.search(text): - return text if '\n' in text: errmsg = f'''Ninja does not support newlines in rules. The content was: @@ -137,7 +130,12 @@ def ninja_quote(text: str, is_build_line: bool = False) -> str: Please report this error with a test case to the Meson bug tracker.''' raise MesonException(errmsg) - return quote_re.sub(r'$\g<0>', text) + + quote_re = NINJA_QUOTE_BUILD_PAT if is_build_line else NINJA_QUOTE_VAR_PAT + if ' ' in text or '$' in text or (is_build_line and ':' in text): + return quote_re.sub(r'$\g<0>', text) + + return text @dataclass |
