Skip to content

Commit 3b36cb2

Browse files
bonzinieli-schwartz
authored andcommitted
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>
1 parent c9635da commit 3b36cb2

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

mesonbuild/backend/ninjabackend.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,19 @@ def get_rsp_threshold() -> int:
123123
NINJA_QUOTE_VAR_PAT = re.compile(r"[$ \n]")
124124

125125
def ninja_quote(text: str, is_build_line: bool = False) -> str:
126-
if is_build_line:
127-
quote_re = NINJA_QUOTE_BUILD_PAT
128-
else:
129-
quote_re = NINJA_QUOTE_VAR_PAT
130-
# Fast path for when no quoting is necessary
131-
if not quote_re.search(text):
132-
return text
133126
if '\n' in text:
134127
errmsg = f'''Ninja does not support newlines in rules. The content was:
135128
136129
{text}
137130
138131
Please report this error with a test case to the Meson bug tracker.'''
139132
raise MesonException(errmsg)
140-
return quote_re.sub(r'$\g<0>', text)
133+
134+
quote_re = NINJA_QUOTE_BUILD_PAT if is_build_line else NINJA_QUOTE_VAR_PAT
135+
if ' ' in text or '$' in text or (is_build_line and ':' in text):
136+
return quote_re.sub(r'$\g<0>', text)
137+
138+
return text
141139

142140

143141
@dataclass

0 commit comments

Comments
 (0)