Skip to content

Commit a7932ae

Browse files
jimmodpgeorge
authored andcommitted
tools/makeqstrdefs.py: Run qstr preprocessing in parallel.
This gives a substantial speedup of the preprocessing step, i.e. the generation of qstr.i.last. For example on a clean build, making qstr.i.last: 21s -> 4s on STM32 (WB55) 8.9 -> 1.8s on Unix (dev). Done in collaboration with @stinos. Signed-off-by: Jim Mussared <[email protected]>
1 parent d7e1526 commit a7932ae

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

py/makeqstrdefs.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
from __future__ import print_function
99

10+
import io
11+
import os
1012
import re
1113
import subprocess
1214
import sys
13-
import io
14-
import os
15+
import multiprocessing, multiprocessing.dummy
1516

1617

1718
# Extract MP_QSTR_FOO macros.
@@ -39,11 +40,27 @@ def preprocess():
3940
os.makedirs(os.path.dirname(args.output[0]))
4041
except OSError:
4142
pass
42-
with open(args.output[0], "w") as out_file:
43-
if csources:
44-
subprocess.check_call(args.pp + args.cflags + csources, stdout=out_file)
45-
if cxxsources:
46-
subprocess.check_call(args.pp + args.cxxflags + cxxsources, stdout=out_file)
43+
44+
def pp(flags):
45+
def run(files):
46+
return subprocess.check_output(args.pp + flags + files)
47+
48+
return run
49+
50+
try:
51+
cpus = multiprocessing.cpu_count()
52+
except NotImplementedError:
53+
cpus = 1
54+
p = multiprocessing.dummy.Pool(cpus)
55+
with open(args.output[0], "wb") as out_file:
56+
for flags, sources in (
57+
(args.cflags, csources),
58+
(args.cxxflags, cxxsources),
59+
):
60+
batch_size = (len(sources) + cpus - 1) // cpus
61+
chunks = [sources[i : i + batch_size] for i in range(0, len(sources), batch_size or 1)]
62+
for output in p.imap(pp(flags), chunks):
63+
out_file.write(output)
4764

4865

4966
def write_out(fname, output):

0 commit comments

Comments
 (0)