Skip to content

Commit

Permalink
py/makeqstrdefs.py: Optimise by using compiled re's so it runs faster.
Browse files Browse the repository at this point in the history
By using pre-compiled regexs, using startswith(), and explicitly checking
for empty lines (of which around 30% of the input lines are), automatic
qstr extraction is speed up by about 10%.
  • Loading branch information
dpgeorge committed Mar 16, 2018
1 parent 06aa13c commit f6a1f18
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions py/makeqstrdefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ def write_out(fname, output):
f.write("\n".join(output) + "\n")

def process_file(f):
re_line = re.compile(r"#[line]*\s\d+\s\"([^\"]+)\"")
re_qstr = re.compile(r'MP_QSTR_[_a-zA-Z0-9]+')
output = []
last_fname = None
for line in f:
if line.isspace():
continue
# match gcc-like output (# n "file") and msvc-like output (#line n "file")
if line and (line[0:2] == "# " or line[0:5] == "#line"):
m = re.match(r"#[line]*\s\d+\s\"([^\"]+)\"", line)
if line.startswith(('# ', '#line')):
m = re_line.match(line)
assert m is not None
fname = m.group(1)
if not fname.endswith(".c"):
Expand All @@ -39,7 +43,7 @@ def process_file(f):
output = []
last_fname = fname
continue
for match in re.findall(r'MP_QSTR_[_a-zA-Z0-9]+', line):
for match in re_qstr.findall(line):
name = match.replace('MP_QSTR_', '')
if name not in QSTRING_BLACK_LIST:
output.append('Q(' + name + ')')
Expand Down

0 comments on commit f6a1f18

Please sign in to comment.