Skip to content

Commit

Permalink
ninjabackend: prefer "in" to regex search
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bonzini committed Nov 7, 2024
1 parent 0a8ed5e commit 234adf6
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,26 @@ def get_rsp_threshold() -> int:
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:
{text}
Please report this error with a test case to the Meson bug tracker.'''
raise MesonException(errmsg)
return quote_re.sub(r'$\g<0>', text)

if is_build_line:
if ':' in text:
return NINJA_QUOTE_BUILD_PAT.sub(r'$\g<0>', text)
quote_re = NINJA_QUOTE_BUILD_PAT
else:
quote_re = NINJA_QUOTE_VAR_PAT

if ' ' in text:
return quote_re.sub(r'$\g<0>', text)
if '$' in text:
return quote_re.sub(r'$\g<0>', text)
return text


@dataclass
Expand Down

0 comments on commit 234adf6

Please sign in to comment.