Files
AILang/examples/bench_mono_dispatch.ail.json
T
Brummel 4cacfcbdac bench: mono-vs-vdisp micro-benchmark + revised Decision 11 rationale
Hypothesis-driven measurement of "did monomorphisation actually
buy us performance?" on a 100M-iter LCG hot loop, AILang mono'd
code vs. four C reference variants (direct-inlinable, direct-
noinline, indirect-monomorphic, indirect-polymorphic). Zen 3,
clang -O2, median-of-15.

Headline: H1 supported, but the mechanism is inlining, not
dispatch shape. AILang mono = hand-C direct (1.000x). Indirect-
monomorphic = direct-noinline (1.000x) — saturating branch
predictor makes the indirect-call cost vanish on this hardware.
Inlining is the actual 3.31x win; polymorphic indirect adds
another 21% predictor-miss penalty.

DESIGN.md Decision 11 gains a rationale paragraph reframing mono
as inlining-enabler rather than indirect-call-eliminator, with
explicit pointer to the bench. JOURNAL entry records the full
methodology, ratios, limitations, and the side-effect mono-pass
env.globals-seeding bug surfaced while building the AILang fixture
(separate RED-first debug iter to follow).
2026-05-10 01:03:21 +02:00

140 lines
3.9 KiB
JSON

{
"schema": "ailang/v0",
"name": "bench_mono_dispatch",
"imports": [],
"defs": [
{
"kind": "class", "name": "Foo", "param": "a",
"methods": [{
"name": "foo",
"type": {
"k": "fn", "params": [{ "k": "var", "name": "a" }],
"ret": { "k": "con", "name": "Int" }, "effects": []
}
}]
},
{
"kind": "instance",
"class": "Foo",
"type": { "k": "con", "name": "Int" },
"methods": [{
"name": "foo",
"body": {
"t": "lam",
"params": ["x"],
"paramTypes": [{ "k": "var", "name": "a" }],
"retType": { "k": "con", "name": "Int" },
"body": {
"t": "app",
"fn": { "t": "var", "name": "+" },
"args": [
{
"t": "app",
"fn": { "t": "var", "name": "*" },
"args": [
{ "t": "var", "name": "x" },
{ "t": "lit", "lit": { "kind": "int", "value": 1103515245 } }
]
},
{ "t": "lit", "lit": { "kind": "int", "value": 12345 } }
]
}
}
}]
},
{
"kind": "class", "name": "Looper", "param": "a",
"methods": [{
"name": "loop_call",
"type": {
"k": "fn", "params": [{ "k": "var", "name": "a" }, { "k": "con", "name": "Int" }],
"ret": { "k": "con", "name": "Int" }, "effects": []
}
}]
},
{
"kind": "instance",
"class": "Looper",
"type": { "k": "con", "name": "Int" },
"methods": [{
"name": "loop_call",
"body": {
"t": "lam",
"params": ["i", "acc"],
"paramTypes": [{ "k": "var", "name": "a" }, { "k": "con", "name": "Int" }],
"retType": { "k": "con", "name": "Int" },
"body": {
"t": "if",
"cond": {
"t": "app",
"fn": { "t": "var", "name": "==" },
"args": [
{ "t": "var", "name": "i" },
{ "t": "lit", "lit": { "kind": "int", "value": 0 } }
]
},
"then": { "t": "var", "name": "acc" },
"else": {
"t": "app",
"tail": true,
"fn": { "t": "var", "name": "loop_call" },
"args": [
{
"t": "app",
"fn": { "t": "var", "name": "-" },
"args": [
{ "t": "var", "name": "i" },
{ "t": "lit", "lit": { "kind": "int", "value": 1 } }
]
},
{
"t": "app",
"fn": { "t": "var", "name": "+" },
"args": [
{ "t": "var", "name": "acc" },
{
"t": "app",
"fn": { "t": "var", "name": "foo" },
"args": [
{
"t": "app",
"fn": { "t": "var", "name": "+" },
"args": [
{ "t": "var", "name": "acc" },
{ "t": "var", "name": "i" }
]
}
]
}
]
}
]
}
}
}
}]
},
{
"kind": "fn",
"name": "main",
"type": {
"k": "fn", "params": [], "ret": { "k": "con", "name": "Unit" },
"effects": ["IO"]
},
"params": [],
"body": {
"t": "do",
"op": "io/print_int",
"args": [{
"t": "app",
"fn": { "t": "var", "name": "loop_call" },
"args": [
{ "t": "lit", "lit": { "kind": "int", "value": 100000000 } },
{ "t": "lit", "lit": { "kind": "int", "value": 0 } }
]
}]
}
}
]
}