ninjabackend: optimize length_estimate

Optimize the regular expression so that the variable expansion part
always ends up in group 1, and the trailer after the variable is discarded
in the same match.  Do not use re.sub to remove braces, and do not bother
building the expanded command, just adjust the estimated length on the fly.

functools.reduce is extremely slow, so I am keeping ' '.join(chunk).

On a QEMU build the time spend in the function goes from 1.072s to 0.757s.
pull/7607/head
Paolo Bonzini 4 years ago
parent fcf9746232
commit b14601da7b
  1. 22
      mesonbuild/backend/ninjabackend.py

@ -265,18 +265,20 @@ class NinjaRule:
# expand variables in command # expand variables in command
command = ' '.join([self._quoter(x) for x in self.command + self.args]) command = ' '.join([self._quoter(x) for x in self.command + self.args])
expanded_command = '' estimate = len(command)
for m in re.finditer(r'(\${\w*})|(\$\w*)|([^$]*)', command): for m in re.finditer(r'(\${\w*}|\$\w*)?[^$]*', command):
chunk = m.group() if m.start(1) != -1:
if chunk.startswith('$'): estimate -= m.end(1) - m.start(1) + 1
chunk = chunk[1:] chunk = m.group(1)
chunk = re.sub(r'{(.*)}', r'\1', chunk) if chunk[1] == '{':
chunk = ninja_vars.get(chunk, []) # undefined ninja variables are empty chunk = chunk[2:-1]
chunk = ' '.join(chunk) else:
expanded_command += chunk chunk = chunk[1:]
chunk = ninja_vars.get(chunk, []) # undefined ninja variables are empty
estimate += len(' '.join(chunk))
# determine command length # determine command length
return len(expanded_command) return estimate
class NinjaBuildElement: class NinjaBuildElement:
def __init__(self, all_outputs, outfilenames, rulename, infilenames, implicit_outs=None): def __init__(self, all_outputs, outfilenames, rulename, infilenames, implicit_outs=None):

Loading…
Cancel
Save