Skip to content

Commit 14b9196

Browse files
committed
[Omit needless words script] Add multiprocessing support.
Dumping APIs for large swaths of SDK takes time; parallelize it in the most trivial way.
1 parent 280779f commit 14b9196

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

utils/omit-needless-words.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import re
1212
import subprocess
13+
import multiprocessing
1314

1415
DEFAULT_TARGET_BASED_ON_SDK = {
1516
'macosx': 'x86_64-apple-macosx10.11',
@@ -50,6 +51,7 @@ def create_parser():
5051
prog='omit-needless-words.py',
5152
usage='python omit-needless-words.py -m AppKit')
5253
parser.add_argument('-m', '--module', help='The module name.')
54+
parser.add_argument('-j', '--jobs', type=int, help='The number of parallel jobs to execute')
5355
parser.add_argument('-s', '--sdk', nargs='+', required=True, help="The SDKs to use.")
5456
parser.add_argument('-t', '--target', help="The target triple to use.")
5557
parser.add_argument('-i', '--swift-ide-test', default='swift-ide-test', help="The swift-ide-test executable.")
@@ -88,7 +90,7 @@ def collect_submodules(common_args, module):
8890
return sorted(list(submodules))
8991

9092
# Dump the API for the given module.
91-
def dump_module_api(cmd, extra_dump_args, output_dir, module, quiet):
93+
def dump_module_api((cmd, extra_dump_args, output_dir, module, quiet)):
9294
# Collect the submodules
9395
submodules = collect_submodules(cmd, module)
9496

@@ -150,7 +152,7 @@ def collect_frameworks(sdk):
150152

151153
return (sorted(list(frameworks)), sdk_path)
152154

153-
def dump_sdk_api(cmd_common, cmd_extra_args, sdk, module, target, source_filename, output_dir, quiet):
155+
def create_dump_module_api_args(cmd_common, cmd_extra_args, sdk, module, target, source_filename, output_dir, quiet):
154156

155157
# Determine the SDK root and collect the set of frameworks.
156158
(frameworks, sdk_root) = collect_frameworks(sdk)
@@ -165,15 +167,16 @@ def dump_sdk_api(cmd_common, cmd_extra_args, sdk, module, target, source_filenam
165167
pretty_sdk = pretty_sdk_name(sdk)
166168
sdk_output_dir = '%s/%s' % (output_dir, pretty_sdk)
167169

168-
# Dump the APIs
170+
# Create the sets of arguments to dump_module_api.
171+
results = []
169172
cmd = cmd_common + ['-sdk', sdk_root, '-target', sdk_target]
170173
if module:
171-
dump_module_api(cmd, cmd_extra_args, sdk_output_dir, module, quiet)
174+
results.append((cmd, cmd_extra_args, sdk_output_dir, module, quiet))
172175
else:
173176
for framework in frameworks:
174-
dump_module_api(cmd, cmd_extra_args, sdk_output_dir, framework, quiet)
177+
results.append((cmd, cmd_extra_args, sdk_output_dir, framework, quiet))
175178

176-
return
179+
return results
177180

178181
def main():
179182
source_filename = 'omit-needless-words.swift'
@@ -190,9 +193,14 @@ def main():
190193
# Create a .swift file we can feed into swift-ide-test
191194
subprocess.call(['touch', source_filename])
192195

193-
# Dump the requested APIs.
196+
# Construct the set of API dumps we should perform.
197+
jobs = []
194198
for sdk in args.sdk:
195-
dump_sdk_api(cmd_common, extra_args, sdk, args.module, args.target, source_filename, args.output_dir, args.quiet)
199+
jobs = jobs + create_dump_module_api_args(cmd_common, extra_args, sdk, args.module, args.target, source_filename, args.output_dir, args.quiet)
200+
201+
# Execute the API dumps
202+
pool = multiprocessing.Pool(processes=args.jobs)
203+
pool.map(dump_module_api, jobs)
196204

197205
# Remove the .swift file we fed into swift-ide-test
198206
subprocess.call(['rm', '-f', source_filename])

0 commit comments

Comments
 (0)