Skip to content

Commit c4f5056

Browse files
authored
Limit build parallelism in perf_compare script (#18288)
Running too many parallel builds risks running out of memory, especially on systems with 16 GB or less RAM. By default run 8 builds, which may already be too many for smaller systems, but `-j N` can be used to lower the number of parallel builds. Also rename `-n` to `--num-runs` to avoid ambiguity, since `-n` is used by pytest to set parallelism.
1 parent 46c7ec7 commit c4f5056

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

misc/perf_compare.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import statistics
2626
import subprocess
2727
import sys
28-
import threading
2928
import time
29+
from concurrent.futures import ThreadPoolExecutor, as_completed
3030

3131

3232
def heading(s: str) -> None:
@@ -99,39 +99,45 @@ def main() -> None:
9999
help="measure incremental run (fully cached)",
100100
)
101101
parser.add_argument(
102-
"-n",
103-
metavar="NUM",
102+
"--num-runs",
103+
metavar="N",
104104
default=15,
105105
type=int,
106-
help="number of measurements to perform (default=15)",
106+
help="set number of measurements to perform (default=15)",
107+
)
108+
parser.add_argument(
109+
"-j",
110+
metavar="N",
111+
default=8,
112+
type=int,
113+
help="set maximum number of parallel builds (default=8)",
107114
)
108115
parser.add_argument("commit", nargs="+", help="git revision to measure (e.g. branch name)")
109116
args = parser.parse_args()
110117
incremental: bool = args.incremental
111118
commits = args.commit
112-
num_runs: int = args.n + 1
119+
num_runs: int = args.num_runs + 1
120+
max_workers: int = args.j
113121

114122
if not (os.path.isdir(".git") and os.path.isdir("mypyc")):
115123
sys.exit("error: Run this the mypy repo root")
116124

117-
build_threads = []
118125
target_dirs = []
119126
for i, commit in enumerate(commits):
120127
target_dir = f"mypy.{i}.tmpdir"
121128
target_dirs.append(target_dir)
122129
clone(target_dir, commit)
123-
t = threading.Thread(target=lambda: build_mypy(target_dir))
124-
t.start()
125-
build_threads.append(t)
126130

127131
self_check_dir = "mypy.self.tmpdir"
128132
clone(self_check_dir, commits[0])
129133

130134
heading("Compiling mypy")
131135
print("(This will take a while...)")
132136

133-
for t in build_threads:
134-
t.join()
137+
with ThreadPoolExecutor(max_workers=max_workers) as executor:
138+
futures = [executor.submit(build_mypy, target_dir) for target_dir in target_dirs]
139+
for future in as_completed(futures):
140+
future.result()
135141

136142
print(f"Finished compiling mypy ({len(commits)} builds)")
137143

0 commit comments

Comments
 (0)