Commit 6b609383 authored by wychen's avatar wychen Committed by Commit bot

Optimize check_gn_headers.py for speed

Process the output of 'ninja' live through the pipe rather than waiting
for it to finish.

Before:
20.74user 16.36system 0:24.57elapsed 151%CPU

After:
16.63user 9.24system 0:13.94elapsed 185%CPU

BUG=661774

Review-Url: https://codereview.chromium.org/2842513003
Cr-Commit-Position: refs/heads/master@{#466900}
parent 77c5ade4
......@@ -19,7 +19,21 @@ import sys
def GetHeadersFromNinja(out_dir):
"""Return all the header files from ninja_deps"""
ninja_out = subprocess.check_output(['ninja', '-C', out_dir, '-t', 'deps'])
def NinjaSource():
cmd = ['ninja', '-C', out_dir, '-t', 'deps']
# A negative bufsize means to use the system default, which usually
# means fully buffered.
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=-1)
for line in iter(popen.stdout.readline, ''):
yield line
popen.stdout.close()
return_code = popen.wait()
if return_code:
raise subprocess.CalledProcessError(return_code, cmd)
ninja_out = NinjaSource()
return ParseNinjaDepsOutput(ninja_out)
......@@ -30,7 +44,7 @@ def ParseNinjaDepsOutput(ninja_out):
prefix = '..' + os.sep + '..' + os.sep
is_valid = False
for line in ninja_out.split('\n'):
for line in ninja_out:
if line.startswith(' '):
if not is_valid:
continue
......
......@@ -58,7 +58,7 @@ a/b/c
class CheckGnHeadersTest(unittest.TestCase):
def testNinja(self):
headers = check_gn_headers.ParseNinjaDepsOutput(ninja_input)
headers = check_gn_headers.ParseNinjaDepsOutput(ninja_input.split('\n'))
expected = set([
'dir/path/b.h',
'c.hh',
......@@ -71,7 +71,8 @@ class CheckGnHeadersTest(unittest.TestCase):
old_sep = os.sep
os.sep = '\\'
headers = check_gn_headers.ParseNinjaDepsOutput(ninja_input_win)
headers = check_gn_headers.ParseNinjaDepsOutput(
ninja_input_win.split('\n'))
expected = set([
'dir\\path\\b.h',
'c.hh',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment