Debian Codegraph @

util/size-experiment.py

raw
1 #!/usr/bin/env python3
2 # spell-checker:ignore debuginfo
3 import os
4 import shutil
5 import subprocess
6 from collections import defaultdict
7 from itertools import product
8 from pprint import pprint
9
10 # Set to false if you've only changed the analysis and do not want to recompile
11 # the binaries.
12 RECOMPILE = True
13
14 STRIP_VALS = ["none", "debuginfo", "symbols"]
15 PANIC_VALS = ["unwind", "abort"]
16 OPT_LEVEL_VALS = [3, "s", "z"]
17 LTO_VALS = ["off", "thin", "fat"]
18
19
20 def config(name, val):
21 return ["--config", f"profile.release.{name}={val!r}"]
22
23
24 sizes = {}
25
26 for strip, panic, opt, lto in product(STRIP_VALS, PANIC_VALS, OPT_LEVEL_VALS, LTO_VALS):
27 if RECOMPILE:
28 cmd = [
29 "cargo",
30 "build",
31 "--release",
32 "--features=unix",
33 *config("strip", strip),
34 *config("panic", panic),
35 *config("opt-level", opt),
36 *config("lto", lto),
37 ]
38 print("RUN:", " ".join(cmd))
39 res = subprocess.call(cmd)
40
41 shutil.copyfile(
42 "target/release/coreutils",
43 "-".join(["coreutils", strip, panic, str(opt), lto]),
44 )
45 print(res)
46
47 sizes[(strip, panic, opt, lto)] = os.path.getsize(
48 "-".join(["coreutils", strip, panic, str(opt), lto])
49 )
50
51 changes_absolute = defaultdict(list)
52 changes_percent = defaultdict(list)
53
54
55 def with_val_at_idx(val, idx, other):
56 other = list(other)
57 other.insert(idx, val)
58 return tuple(other)
59
60
61 def collect_diff(idx, name):
62 all_params = [STRIP_VALS, PANIC_VALS, OPT_LEVEL_VALS, LTO_VALS]
63 vals = all_params.pop(idx)
64 for other in product(*all_params):
65 baseline = sizes[with_val_at_idx(vals[0], idx, other)]
66 for val in vals[1:]:
67 changes = sizes[with_val_at_idx(val, idx, other)] - baseline
68 changes_absolute[f"{name}={val}"].append(changes)
69 changes_percent[f"{name}={val}"].append(changes / baseline * 100)
70
71
72 collect_diff(0, "strip")
73 collect_diff(1, "panic")
74 collect_diff(2, "opt-level")
75 collect_diff(3, "lto")
76
77
78 def analyze(change):
79 return f"""MIN: {float(min(change)):.3},
80 AVG: {float(sum(change) / len(change)):.3}, MAX: {float(max(change)):.3}"""
81
82
83 print("Absolute changes")
84 pprint({k: analyze(v) for k, v in changes_absolute.items()})
85 print()
86 print("Percent changes")
87 pprint({k: analyze(v) for k, v in changes_percent.items()})
88
89 print()
90 print(changes_percent["opt-level=s"])
91 print(changes_percent["opt-level=z"])