Skip to content

Commit 084aaa8

Browse files
committed
build: Make genlast write the "split" files
This gets a further speedup of about 2s (12s -> 9.5s elapsed build time) for stm32f405_feather For what are probably historical reasons, the qstr process involves preprocessing a large number of source files into a single "qstr.i.last" file, then reading this and splitting it into one "qstr" file for each original source ("*.c") file. By eliminating the step of writing qstr.i.last as well as making the regular-expression-matching part be parallelized, build speed is further improved. Because the step to build QSTR_DEFS_COLLECTED does not access qstr.i.last, the path is replaced with "-" in the Makefile.
1 parent 607e4a9 commit 084aaa8

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

py/genlast.py

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,69 @@
11
#!/usr/bin/env python3
22

33
import sys
4-
from concurrent.futures import ThreadPoolExecutor
4+
import re
5+
import os
6+
import itertools
7+
from concurrent.futures import ProcessPoolExecutor
58
import multiprocessing
69
import threading
710
import subprocess
811

12+
from makeqstrdefs import qstr_unescape, QSTRING_BLACK_LIST
13+
14+
re_line = re.compile(r"#[line]*\s(\d+)\s\"([^\"]+)\"", re.DOTALL)
15+
re_qstr = re.compile(r'MP_QSTR_[_a-zA-Z0-9]+', re.DOTALL)
16+
re_translate = re.compile(r'translate\(\"((?:(?=(\\?))\2.)*?)\"\)', re.DOTALL)
17+
18+
def write_out(fname, output):
19+
if output:
20+
for m, r in [("/", "__"), ("\\", "__"), (":", "@"), ("..", "@@")]:
21+
fname = fname.replace(m, r)
22+
with open(output_dir + "/" + fname + ".qstr", "w") as f:
23+
f.write("\n".join(output) + "\n")
24+
25+
def process_file(fname, content):
26+
content = content.decode('utf-8', errors='ignore')
27+
output = []
28+
for match in re_qstr.findall(content):
29+
name = match.replace('MP_QSTR_', '')
30+
if name not in QSTRING_BLACK_LIST:
31+
output.append('Q(' + qstr_unescape(name) + ')')
32+
for match in re_translate.findall(content):
33+
output.append('TRANSLATE("' + match[0] + '")')
34+
35+
write_out(fname, output)
36+
937

1038
def checkoutput1(args):
1139
info = subprocess.run(args, check=True, stdout=subprocess.PIPE, input='')
1240
return info.stdout
1341

14-
idx1 = sys.argv.index('--')
15-
idx2 = sys.argv.index('--', idx1+1)
16-
check = sys.argv[1:idx1]
17-
always = sys.argv[idx1+1:idx2]
18-
command = sys.argv[idx2+1:]
19-
20-
output_lock = threading.Lock()
21-
def preprocess(fn):
22-
output = checkoutput1(command + [fn])
23-
# Ensure our output doesn't interleave with others
24-
# a threading.Lock is not a context manager object :(
42+
def preprocess(command, fn):
2543
try:
26-
output_lock.acquire()
27-
sys.stdout.buffer.write(output)
28-
finally:
29-
output_lock.release()
44+
output = checkoutput1(self.command + [fn])
45+
process_file(fn, output)
46+
except Exception as e:
47+
print(e, file=sys.stderr)
3048

31-
def maybe_preprocess(fn):
49+
def maybe_preprocess(command, fn):
3250
if subprocess.call(["grep", "-lqE", "(MP_QSTR|translate)", fn]) == 0:
33-
preprocess(fn)
51+
self.preprocess(command, fn)
52+
53+
if __name__ == '__main__':
54+
55+
idx1 = sys.argv.index('--')
56+
idx2 = sys.argv.index('--', idx1+1)
57+
output_dir = sys.argv[1]
58+
check = sys.argv[2:idx1]
59+
always = sys.argv[idx1+1:idx2]
60+
command = sys.argv[idx2+1:]
61+
62+
if not os.path.isdir(output_dir):
63+
os.makedirs(output_dir)
3464

35-
executor = ThreadPoolExecutor(max_workers=multiprocessing.cpu_count() + 1)
36-
executor.map(maybe_preprocess, check)
37-
executor.map(preprocess, always)
38-
executor.shutdown()
65+
worker = Worker(command)
66+
executor = ProcessPoolExecutor(max_workers=multiprocessing.cpu_count() + 1)
67+
executor.map(worker.maybe_preprocess, itertools.repeat(command), check)
68+
executor.map(worker.preprocess, itertools.repeat(command), always)
69+
executor.shutdown()

py/mkrules.mk

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,14 @@ $(OBJ): | $(HEADER_BUILD)/qstrdefs.enum.h $(HEADER_BUILD)/mpversion.h
7676
# - if anything in QSTR_GLOBAL_DEPENDENCIES is newer, then process all source files ($^)
7777
# - else, if list of newer prerequisites ($?) is not empty, then process just these ($?)
7878
# - else, process all source files ($^) [this covers "make -B" which can set $? to empty]
79-
$(HEADER_BUILD)/qstr.i.last: $(SRC_QSTR) $(SRC_QSTR_PREPROCESSOR) $(QSTR_GLOBAL_DEPENDENCIES) | $(HEADER_BUILD)/mpversion.h
79+
$(HEADER_BUILD)/qstr.split: $(SRC_QSTR) $(SRC_QSTR_PREPROCESSOR) $(QSTR_GLOBAL_DEPENDENCIES) | $(HEADER_BUILD)/mpversion.h $(PY_SRC)/genlast.py
8080
$(STEPECHO) "GEN $@"
81-
$(Q)$(PYTHON3) $(PY_SRC)/genlast.py $(if $(filter $?,$(QSTR_GLOBAL_DEPENDENCIES)),$^,$(if $?,$?,$^)) -- $(SRC_QSTR_PREPROCESSOR) -- $(CPP) $(QSTR_GEN_EXTRA_CFLAGS) $(CFLAGS) >$(HEADER_BUILD)/qstr.i.last;
82-
83-
$(HEADER_BUILD)/qstr.split: $(HEADER_BUILD)/qstr.i.last $(PY_SRC)/makeqstrdefs.py
84-
$(STEPECHO) "GEN $@"
85-
$(Q)$(PYTHON3) $(PY_SRC)/makeqstrdefs.py split $(HEADER_BUILD)/qstr.i.last $(HEADER_BUILD)/qstr $(QSTR_DEFS_COLLECTED)
81+
$(Q)$(PYTHON3) $(PY_SRC)/genlast.py $(HEADER_BUILD)/qstr $(if $(filter $?,$(QSTR_GLOBAL_DEPENDENCIES)),$^,$(if $?,$?,$^)) -- $(SRC_QSTR_PREPROCESSOR) -- $(CPP) $(QSTR_GEN_EXTRA_CFLAGS) $(CFLAGS)
8682
$(Q)touch $@
8783

8884
$(QSTR_DEFS_COLLECTED): $(HEADER_BUILD)/qstr.split $(PY_SRC)/makeqstrdefs.py
8985
$(STEPECHO) "GEN $@"
90-
$(Q)$(PYTHON3) $(PY_SRC)/makeqstrdefs.py cat $(HEADER_BUILD)/qstr.i.last $(HEADER_BUILD)/qstr $(QSTR_DEFS_COLLECTED)
86+
$(Q)$(PYTHON3) $(PY_SRC)/makeqstrdefs.py cat - $(HEADER_BUILD)/qstr $(QSTR_DEFS_COLLECTED)
9187

9288
# $(sort $(var)) removes duplicates
9389
#

0 commit comments

Comments
 (0)