plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
a61458f
— Peter John
2026-11-10T22:31:03+05:30
benchmarks/dispatch: --base compiles with the base's own std
- benchmarks/dispatch/run.py +33 -18
benchmarks/dispatch/run.py
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
2
|
"""Times a trait method call through trait objects of 2, 9 and 20 implementers.
|
|
3
3
|
|
|
4
|
-
usage: python3 benchmarks/dispatch/run.py [--plum target/release/plum] [--base PATH]
|
|
4
|
+
usage: python3 benchmarks/dispatch/run.py [--plum target/release/plum] [--base PATH [--base-root DIR]]
|
|
5
5
|
[--runs 5] [--out target/dispatch] [--sizes 2,9,20]
|
|
6
|
+
[--calls 20000000]
|
|
6
7
|
|
|
7
8
|
Run from the repository root. For each size N the script writes a program with N struct
|
|
8
9
|
types implementing `Act<Int>` and one generic implementer `G<M>`, a Slice of 64 trait
|
|
9
10
|
objects that cycles through them in a scrambled order, and a loop that calls `a.val(x)` on
|
|
10
|
-
them
|
|
11
|
+
them `--calls` times. Each compiler (`--plum`, and `--base` when given) builds the program
|
|
12
|
+
once, each with its own std: `--plum` this checkout's, `--base` that of `--base-root`
|
|
13
|
+
(default: the checkout the compiler was built in, two folders up from its
|
|
14
|
+
`target/<profile>/plum`). The base must read this branch's syntax (`<T>` generics,
|
|
11
|
-
|
|
15
|
+
1dde2468 or later). Then every executable runs once per round for `--runs` rounds.
|
|
12
16
|
The table gives the median in-program ms and the wasm size, and fails if two builds print
|
|
13
17
|
different checksums.
|
|
14
18
|
"""
|
|
@@ -19,10 +23,7 @@ import statistics
|
|
|
19
23
|
import subprocess
|
|
20
24
|
import sys
|
|
21
25
|
|
|
22
|
-
CALLS = 20_000_000
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def program(n):
|
|
26
|
+
def program(n, calls):
|
|
26
27
|
out = ["import std/Int", "import std/Float", "import std/Str", "import std/Io", "import std/Slice", "import std/Option", "",
|
|
27
28
|
"trait Act<M> =", " fun val(self, x: Int) -> Int", " fun kind(self) -> Int = 0", ""]
|
|
28
29
|
for i in range(n):
|
|
@@ -49,7 +50,7 @@ def program(n):
|
|
|
49
50
|
out.append("fun main(io: Io) =")
|
|
50
51
|
out.append(" xs := objects()")
|
|
51
52
|
out.append(" t0 := io.nowMicros()")
|
|
52
|
-
out.append(f" r := go(xs, 0, {
|
|
53
|
+
out.append(f" r := go(xs, 0, {calls}, 1)")
|
|
53
54
|
out.append(" t1 := io.nowMicros()")
|
|
54
55
|
out.append(" el := {t1 - t0} / 1000.0")
|
|
55
56
|
out.append(' io.printLn("ms {el} checksum {r}")')
|
|
@@ -60,26 +61,40 @@ def main():
|
|
|
60
61
|
ap = argparse.ArgumentParser()
|
|
61
62
|
ap.add_argument("--plum", default="target/release/plum")
|
|
62
63
|
ap.add_argument("--base")
|
|
64
|
+
ap.add_argument("--base-root")
|
|
65
|
+
ap.add_argument("--calls", type=int, default=20_000_000)
|
|
63
66
|
ap.add_argument("--runs", type=int, default=5)
|
|
64
67
|
ap.add_argument("--out", default="target/dispatch")
|
|
65
68
|
ap.add_argument("--sizes", default="2,9,20")
|
|
66
69
|
a = ap.parse_args()
|
|
67
70
|
out = os.path.abspath(a.out)
|
|
68
71
|
os.makedirs(out, exist_ok=True)
|
|
72
|
+
if a.base_root and not a.base:
|
|
73
|
+
sys.exit("--base-root needs --base")
|
|
74
|
+
vers = [("new", os.path.abspath(a.plum), os.getcwd())]
|
|
75
|
+
if a.base:
|
|
76
|
+
base = os.path.abspath(a.base)
|
|
69
|
-
|
|
77
|
+
vers.append(("base", base, os.path.abspath(a.base_root) if a.base_root else os.path.dirname(os.path.dirname(os.path.dirname(base)))))
|
|
78
|
+
for v, plum, root in vers:
|
|
79
|
+
if not os.path.isfile(plum):
|
|
80
|
+
sys.exit(f"{v}: no compiler at {plum}")
|
|
81
|
+
if not os.path.isdir(os.path.join(root, "std")):
|
|
82
|
+
sys.exit(f"{v}: {root} has no std folder (give --base-root)")
|
|
70
83
|
sizes = [int(s) for s in a.sizes.split(",")]
|
|
71
84
|
res = {}
|
|
72
85
|
for n in sizes:
|
|
73
86
|
src = f"{out}/dispatch_{n}.plum"
|
|
74
|
-
open(src, "w").write(program(n))
|
|
87
|
+
open(src, "w").write(program(n, a.calls))
|
|
75
|
-
for v, plum in vers:
|
|
88
|
+
for v, plum, root in vers:
|
|
76
89
|
exe = f"{out}/dispatch_{n}.{v}"
|
|
90
|
+
for cmd in (["build", src, "-o", exe], ["compile", src, "-o", exe + ".wasm"]):
|
|
77
|
-
|
|
91
|
+
p = subprocess.run([plum, *cmd, "--lib-path", root], capture_output=True, text=True)
|
|
92
|
+
if p.returncode != 0:
|
|
78
|
-
|
|
93
|
+
sys.exit(f"dispatch_{n} {v}: plum {cmd[0]} failed: {p.stdout[-500:]} {p.stderr[-2000:]}")
|
|
79
94
|
res[(n, v)] = {"exe": exe, "size": os.path.getsize(exe + ".wasm"), "ms": [], "line": set()}
|
|
80
95
|
for k in range(a.runs):
|
|
81
96
|
for n in sizes:
|
|
82
|
-
for v, _ in vers[k % len(vers):] + vers[:k % len(vers)]:
|
|
97
|
+
for v, *_ in vers[k % len(vers):] + vers[:k % len(vers)]:
|
|
83
98
|
r = res[(n, v)]
|
|
84
99
|
p = subprocess.run([r["exe"]], capture_output=True, text=True)
|
|
85
100
|
m = re.search(r"ms (\S+) (checksum .*)", p.stdout)
|
|
@@ -87,14 +102,14 @@ def main():
|
|
|
87
102
|
sys.exit(f"dispatch_{n} {v} failed: {p.stdout[-500:]} {p.stderr[-2000:]}")
|
|
88
103
|
r["ms"].append(float(m.group(1)))
|
|
89
104
|
r["line"].add(m.group(2).strip())
|
|
90
|
-
print("| implementers | " + " | ".join(f"{v} ms" for v, _ in vers) + " | " + " | ".join(f"{v} wasm bytes" for v, _ in vers) + " |")
|
|
105
|
+
print("| implementers | " + " | ".join(f"{v} ms" for v, *_ in vers) + " | " + " | ".join(f"{v} wasm bytes" for v, *_ in vers) + " |")
|
|
91
106
|
print("|---|" + "---|" * (2 * len(vers)))
|
|
92
107
|
for n in sizes:
|
|
93
|
-
lines = {tuple(sorted(res[(n, v)]["line"])) for v, _ in vers}
|
|
108
|
+
lines = {tuple(sorted(res[(n, v)]["line"])) for v, *_ in vers}
|
|
94
109
|
if len(lines) != 1 or len(next(iter(lines))) != 1:
|
|
95
110
|
sys.exit(f"dispatch_{n}: the builds disagree: {lines}")
|
|
96
|
-
ms = [f"{statistics.median(res[(n, v)]['ms']):.1f} ({min(res[(n, v)]['ms']):.1f}-{max(res[(n, v)]['ms']):.1f})" for v, _ in vers]
|
|
111
|
+
ms = [f"{statistics.median(res[(n, v)]['ms']):.1f} ({min(res[(n, v)]['ms']):.1f}-{max(res[(n, v)]['ms']):.1f})" for v, *_ in vers]
|
|
97
|
-
sz = [str(res[(n, v)]["size"]) for v, _ in vers]
|
|
112
|
+
sz = [str(res[(n, v)]["size"]) for v, *_ in vers]
|
|
98
113
|
print(f"| {n} (+1 generic) | " + " | ".join(ms) + " | " + " | ".join(sz) + " |")
|
|
99
114
|
|
|
100
115
|
|