polydat 0.13.1

Polydat — a variates construction engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
---
type: guide
title: Engine-ladder performance
timestamp: 2026-09-25
description: One typed graph and one tile measured on all four engines, with the measurement contract, correctness gate, and benchmark commands.
tags: [performance, engines]
---

# Engine-ladder performance

Polydat can execute one statically typed function graph at three progressively
lower-overhead levels, and the benchmark adds a fourth rung, pure native code,
the tier P3 is differentially tested against. This page makes that claim concrete with a checked-in
graph, a semantic-equivalence test, and a focused Criterion benchmark.

The benchmark is intentionally small enough to understand. It is not a claim
that every Polydat graph will have the same ratios: node mix, graph shape,
invalidation, requested outputs, host ISA, and compiler version all matter.

## The graph

The example models four fields derived for a workload operation. A changing
`cycle` is combined with tenant and operation seeds. Shared entropy fans out
into identity, routing, payload, and final event-token paths:

```mermaid
flowchart LR
    subgraph Inputs
        C["cycle: u64"]
        T["tenant_seed: u64"]
        O["operation_seed: u64"]
    end

    subgraph Outputs
        ACCOUNT(["account_id"])
        SHARD_OUT(["shard"])
        CLASS_OUT(["payload_class"])
        TOKEN_OUT(["event_token"])
    end

    C --> SEED["u64_add"]
    T --> SEED
    SEED --> IDH["hash"]
    IDH --> IDMOD["mod · 10,000,000"]
    IDMOD --> ACCOUNT

    IDMOD --> ROUTE["u64_xor"]
    O --> ROUTE
    ROUTE --> RH["hash"]
    RH --> SHARD["mod · 64"]
    SHARD --> SHARD_OUT

    IDH --> PAYLOAD["u64_add"]
    O --> PAYLOAD
    PAYLOAD --> PH["hash"]
    PH --> CLASS["mod · 8"]
    CLASS --> CLASS_OUT

    RH --> TOKEN["u64_add"]
    PH --> TOKEN
    TOKEN --> TH["hash"]
    TH --> TOKEN_OUT
```

The graph is useful as a first performance example because it contains three
inputs, four outputs, fan-out, fan-in, shared intermediate products, and eleven
non-trivial nodes while remaining readable. Every wire is `u64`, and every node
has P1 semantics, a P2 compiled closure, and a P3 lowering. P3 therefore cannot
silently fall back while this benchmark is being assembled.

The exact source is
[`examples/engine_ladder.polydat`](../../examples/engine_ladder.polydat):

```polydat
input cycle: u64
input tenant_seed: u64
input operation_seed: u64

seeded_cycle := u64_add(cycle, tenant_seed)
identity_entropy := hash(seeded_cycle)
account_id := mod(identity_entropy, 10000000)

route_seed := u64_xor(account_id, operation_seed)
route_entropy := hash(route_seed)
shard := mod(route_entropy, 64)

payload_seed := u64_add(identity_entropy, operation_seed)
payload_entropy := hash(payload_seed)
payload_class := mod(payload_entropy, 8)

token_seed := u64_add(route_entropy, payload_entropy)
event_token := hash(token_seed)
```

## One graph, three execution levels

The ladder changes the representation and dispatch cost, not the graph's typed
contract:

![Performance ladder: the resolved and type-checked DAG runs on P1, the interpreter; compiled_u64 hooks lift it to P2, compiled closures over flat u64 slots; Cranelift lowering lifts it to P3, native segments with closure steps; when every node lowers it becomes pure native code, one function](../diagrams/performance-ladder.png)

| Level | Construction used by this benchmark | Timed execution |
| --- | --- | --- |
| P1 | `JitMode::Off` plus `PolydatAssembler::compile()` | Set three typed inputs and demand-pull four pre-resolved outputs through the interpreter. |
| P2 | `try_compile_raw()` | Copy three coordinates, execute all compiled closures over flat slots, then read four pre-resolved slots. |
| P3 | `try_compile_jit_raw()` | Copy the same coordinates, run the native segments and closure steps over the shared slots (on this graph, one segment), then read the same four slots. |
| pure | `try_compile_pure_jit_raw()` | Copy the same coordinates, call the one generated native function, then read the same four slots. |

The P2, P3, and pure `raw` variants are deliberate. Every cycle changes the driving
input and all four outputs are consumed, so the test isolates execution-level
overhead without mixing in provenance strategies. A host's default,
`Engine::default()`, is the P3 hybrid kernel with provenance chosen by the
selector; `JitMode::Auto` applies only when the interpreter is named, where
it keeps P1 as the host and embeds eligible cones.

## Measurement contract

One Criterion iteration means one complete logical cycle:

1. Increment `cycle`; keep the two seed inputs stable.
2. Supply all three inputs.
3. Evaluate the eleven graph nodes.
4. Consume `account_id`, `shard`, `payload_class`, and `event_token`.

Graph parsing, validation, compilation, JIT code generation, and output-name
resolution happen before timing. Criterion reports both time per cycle and
cycles per second. The benchmark uses a two-second warm-up, three-second
measurement window, and 60 samples per level.

Before comparing timings, the integration test evaluates boundary and ordinary
input cases and requires P2, P3, and pure native code to return exactly the
same four `u64` values as P1. It also checks all bounded outputs.

## Local reference result

The following run was recorded on 2026-09-14. Each interval is Criterion's
reported confidence interval; the middle value is the point estimate. Because
one benchmark element is one complete graph cycle, `Melem/s` is reported here as
million cycles per second.

| Level | Time per cycle | Throughput | Speed relative to P1 |
| --- | ---: | ---: | ---: |
| P1 interpreter | 322.25 ns `[322.13, 322.38]` | 3.103 M cycles/s `[3.102, 3.104]` | 1.00× |
| P2 closures | 71.006 ns `[70.958, 71.057]` | 14.083 M cycles/s `[14.073, 14.093]` | 4.54× |
| P3 native segments | 25.347 ns `[25.332, 25.364]` | 39.452 M cycles/s `[39.426, 39.475]` | 12.71× |
| pure native code | 14.212 ns `[14.181, 14.250]` | 70.363 M cycles/s `[70.174, 70.517]` | 22.67× |

For this graph, moving from typed node dispatch to flat-slot closures removes
most of the execution cost. Native lowering then provides another 2.80× over
P2. Every node of this graph lowers and the graph is connected, so P3 runs one
native segment. Pure native code runs the same code as one block of a function
that dispatches over the fusion units a pull needs; the two rungs differ in that
dispatch and in how each keeps a nondeterministic node or a side channel
current. These are end-to-end cycle measurements, including
input copies and four output reads, rather than isolated instruction timings.

Reference environment:

- Intel Xeon Platinum 8375C at 2.90 GHz, 64 cores and 128 logical
  processors across two sockets;
- Linux 6.8.0-1047-aws, `x86_64-unknown-linux-gnu`;
- Rust and Cargo 1.98.0, optimized `bench` profile with thin LTO;
- Cranelift 0.116.1, the 0.3.0 release tree at commit b3a7fa5; and
- 60 samples per level after a two-second warm-up, with a three-second
  measurement window, on an otherwise idle machine.

This graph uses scalar `u64` operations. The P3 result demonstrates native
machine-code lowering, not SIMD auto-promotion.

The previous record, from 2026-09-10 on an AMD Ryzen 9 3900X under Windows,
read 441.83 ns, 121.05 ns, 76.104 ns, and 75.942 ns for P1, P2, P3, and
pure native code, with P3 at 5.81× P1. The machine changed between the
records, so the absolute times are not comparable; the ratios also changed
because the code changed. Since 2026-09-13 every node with a kit lowers
natively, native code that calls no helper runs without the failure-catching
trampoline, and the hybrid kernel schedules its constant steps first. On the
earlier machine, native timings drifted between 57 ns and 76 ns across runs
in one day. Compare rungs within one run, not runs across days or machines.

## The tile ladder

A second ladder, `benches/tile_render.rs`, renders one JSON document
through the same four levels. The document is the reading of the toy test
definition (`examples/toy_test_definition.polydat`): a static arm, seven
holes of four types with two formats, a nested object, one projection of
four tuples with a formatted hole, and a declared boolean. Each case is
one complete cycle that pulls the rendered tile, and the five are chosen
so that subtracting one from another isolates a cost:

| Case | What it isolates |
| --- | --- |
| `reading` | the reading's eight wires read directly, no tile: subtract it from any other case to get that case's render cost |
| `one_hole` | one numeric hole in a two-byte skeleton: the floor for a render |
| `flat` | the document without its projection: encode and copy cost per hole |
| `projected` | the document as written: what the projection's tuples and body add |
| `wide` | the flat document with twenty holes of three types: how cost scales with hole count |

The cases are meant to be compared in pairs. A tile's cost is the bytes it copies plus the
holes it encodes plus, per projection, the tuples times the body
([Polytile](../design/polytile.md) §7.3), and each of those three terms
has a case that isolates it. A render cost that grows with the skeleton
rather than the hole count, or a projection that costs more than its
tuples, is a regression, and the case that isolates that term shows it.

The current record was taken on 2026-09-14 in the same session as the engine
ladder above, on the same machine and tree. Each entry is Criterion's point
estimate in nanoseconds per cycle. Pure native code runs every case, since
every node lowers; in the tile measurements before this record it ran only
the one-hole case.

| Case | P1 interpreter | P2 closures | P3 native segments | pure native |
| --- | ---: | ---: | ---: | ---: |
| `reading` | 1490 | 1145 | 1073 | 1049 |
| `one_hole` | 190 | 140 | 141 | 136 |
| `flat` | 2389 | 1518 | 1401 | 1365 |
| `projected` | 3732 | 2906 | 2675 | 2651 |
| `wide` | 4000 | 2877 | 2349 | 2312 |

Taking each case less `reading`: the seven-hole `flat` document costs about
330 ns on P3 and 900 ns on P1, under 50 ns and 130 ns per hole; the
twenty-hole `wide` document costs 1.3 µs on P3, 64 ns per hole; and the
four-tuple projection adds 1.3 µs on P3 over `flat`, about 320 ns per tuple.
The last paired run on the earlier machine, on 2026-09-11, put `projected`
at 7949 ns on P1, 7104 on P2, and 6693 on P3, from 18810, 17479, and 14851
before the work began. The same drift caveat applies: compare rungs within
one run.

## Run it locally

Run the semantic gate first:

```sh
cargo nextest run -p polydat --test suite engine_ladder_equivalence::
```

Then run only the focused performance target. The engine ladder measures each
compiled tier from its own kernel type as well as through `dyn`. The normative
surface does not let a caller name a kernel type, so the target requires the
`bench-tiers` feature and cargo refuses to build it without that feature:

```sh
cargo bench -p polydat --features bench-tiers --bench engine_ladder
```

The tile ladder runs the same way:

```sh
cargo bench -p polydat --bench tile_render
```

The P3 and pure cases require the default `jit` feature. A no-default-features
run still builds and measures P1 and P2:

```sh
cargo bench -p polydat --no-default-features --features bench-tiers --bench engine_ladder
```

## `polydat perf`

The binary measures programs itself, through the surface a host uses:
`set_inputs` and `pull_at` on a `Box<dyn Kernel>`, on all four engines. Its numbers
are therefore the per-cycle cost a host sees, and they are larger than the engine
ladder's, which times `eval` and slot reads on a kernel named by its own type.
Use the ladder bench to look inside an engine and `polydat perf` to see what a
program costs a host.

```sh
polydat perf                                   # the built-in suite
polydat perf --group conversions --rounds 20   # one group, more rounds
polydat perf --print-config > suite.toml       # a suite file to start from
polydat perf --config suite.toml --save before.json
polydat perf --config suite.toml --compare before.json
```

A suite is TOML. Each `[[group]]` is one program, from `program` (a path
relative to the suite file) or `source`, measured on each of its `engines`
(`interpreter`, `interpreter-cones`, `closures`, `native`, `pure-native`).
`outputs` lists what each cycle pulls, every output when it is left out;
`cycle` names the input advanced each cycle, and `inputs` fixes the others.
`[settings]` holds `rounds`, `warmup_ms`, `measure_ms`, `batch_ms`, and
`provenance`, which defaults to `auto`, the setting a host gets by default. The command line's
`--rounds`, `--warmup-ms`, `--measure-ms`, `--provenance`, `--group`, and
`--engine` override the file.

A group can also build its program, with `generate` in place of `program`
or `source`, to measure how an engine scales with the shape of a graph and
how much of it a pull's cone covers:

```toml
[[group]]
name = "chains-64-one"
generate = { shape = "chains", width = 64, depth = 16 }
outputs = ["o0"]
```

`chains` is `width` independent chains of `depth` hash steps, so one
output's cone is `1 / width` of the graph. `trunk` is one shared chain of
`depth` steps fanning out into `width` branches of `branch` steps, so the
cones overlap in the trunk. `lattice` is `depth` layers of `width` nodes,
each mixing two neighbours of the layer below, so a cone widens by one node
per layer until it covers the whole layer. The outputs are `o0`, `o1`, …, and a group
pulls all of them unless `outputs` names some. Each group's heading gives
the shape, the number of bindings, and how many outputs a cycle pulls.
[`examples/perf/cone_spectrum.toml`](../../examples/perf/cone_spectrum.toml)
is a suite across the three shapes, several widths, one pull against all,
and a single chain at three depths.

### The cone spectrum, 2026-09-24

The suite was run on all four engines at commit f6ae61f, paired round by round
with the build before cone-shaped native code (2c51167). Each entry is the
median of five rounds in nanoseconds per cycle; the native columns give the
earlier build, then this one. The interpreter and the closures did not move
beyond the run's noise between the two builds, so they are given once.

| Group | interpreter | closures | native | pure native |
| --- | ---: | ---: | ---: | ---: |
| `chains-4-one` | 434 | 187 | 194 → 82 | 162 → 76 |
| `chains-4-all` | 1739 | 743 | 311 → 312 | 553 → 257 |
| `chains-16-one` | 536 | 193 | 624 → 85 | 494 → 82 |
| `chains-16-all` | 7047 | 3011 | 1139 → 1256 | 5658 → 1025 |
| `chains-64-one` | 800 | 193 | 2348 → 97 | 1981 → 97 |
| `chains-64-all` | 28406 | 12129 | 4470 → 5306 | 107292 → 4431 |
| `trunk-16-one` | 505 | 205 | 142 → 125 | 170 → 118 |
| `trunk-16-all` | 1961 | 1174 | 647 → 487 | 1755 → 413 |
| `lattice-16x8-one` | 2166 | 584 | 290 → 282 | 451 → 267 |
| `lattice-16x8-all` | 8347 | 3184 | 795 → 642 | 4903 → 563 |
| `chain-8` | 241 | 115 | 64 → 58 | 77 → 49 |
| `chain-64` | 1712 | 614 | 242 → 233 | 284 → 225 |
| `chain-256` | 6875 | 2339 | 835 → 826 | 1170 → 818 |

What the columns show:

- **A pull costs its cone.** Pulling one output of `width` independent
  chains costs the same at every width on both native engines, about half
  the closures' cost. The earlier build ran every chain, so its cost grew
  with the graph (97 ns against 2348 at width 64).
- **The ladder holds everywhere.** Interpreter, closures, native, pure
  native, slowest to fastest, in every group; at `chains-64-one` native
  and pure native are within the noise of each other.
- **Pure native is fastest.** Its generated function tests each fusion
  unit's clean flag in native code, so a pull does no search or filtering
  before the units run.
- **One regression remains:** the native tier pulling every output of
  many independent chains, 18% slower at width 64. The earlier build ran
  all 64 chains in one segment at the first pull; a pull now runs its own
  chain's segment, so a round enters 64 segments where it entered one.
  The difference from pure native grows with the depth of the chains, so
  it is not only a fixed cost per pull; its cause is not yet isolated.
  Pure native is as fast as the earlier build here.

Reference environment: AMD Ryzen 9 3900X, 12 cores and 24 logical
processors; Windows 11 Pro 10.0.26200; Rust 1.96.0, release profile with
thin LTO; Cranelift 0.116.1; five rounds of a 100 ms warm-up and a 400 ms
measurement per rung, with an IDE open in power-save mode and a game
client idle. The closure column is the run's canary: its per-group
intervals reached ±12% on two groups, so a difference under about 5% should
not be read as a real difference.

Each rung (one group on one engine) is calibrated into batches of about
`batch_ms`, warmed, and measured; its value for a round is the median batch.
Rounds interleave every rung and rotate their order, so drift during the run
affects every rung equally. Progress goes to stderr. The results are a table per
group: ns per cycle (the median of the rounds), the spread across rounds, the
fastest round, and the speedup over the slowest engine, with a check that the
interpreter, closure, and native engines are in ladder order. The compiler's
warnings about the suite's programs are listed once each after the table.

## Reading a comparison

Checking for a regression means **building both binaries first and then
alternating them**, never rebuilding between the legs you compare. A release
build of this workspace takes a minute or more at full load, so a leg measured
straight after one starts on a hotter machine than the leg before it. That
drift is monotonic over a session and larger than the effects worth finding:
on 2026-09-22 `p1_interpreter` walked from 429 ns to 520 ns across a morning
without a line of its code changing. Compiling between legs adds that drift
to the measured difference, where it looks like an effect of the diff.

Build each commit once, keep the executable, and leave the compiler out of the
measurement:

```sh
git checkout --detach <baseline>
cargo build --release -p polydat
cp <target dir>/release/polydat /tmp/polydat-base
git checkout <branch>
cargo build --release -p polydat
cp <target dir>/release/polydat /tmp/polydat-head
md5sum /tmp/polydat-base /tmp/polydat-head   # they must differ
```

The target directory is shared (`~/.cargo/config.toml`), so it is not under
this repository, and a stale local `target/` can supply a different binary
that looks plausible. Watch the build for `Compiling polydat`; its absence
means cargo thought the tree unchanged. The hashes are the check that the two
legs are two programs; if they match, the comparison is meaningless and
nothing else in the output will say so.

Then pair them. `--against` runs both binaries one round each, in an order that
alternates round by round, so drift cancels out of each round's delta:

```sh
/tmp/polydat-head perf --against /tmp/polydat-base --rounds 30
```

It reports each rung as the two medians and the mean of the per-round deltas
with its 95% interval; a delta inside the interval is not a difference. The
baseline has to have `polydat perf` too, so a commit older than the command
cannot be paired this way; for those, `cargo bench --no-run` each commit's
`engine_ladder` and alternate the two executables by hand, as the command does.

One pairing
cannot resolve a few percent: the round-to-round standard deviation of a single
rung here is about 4 percent, so a lone A/B has a confidence interval near ±10
and will flag differences that are not there. Thirty paired rounds bring it to
about ±1.6, which is what it took on 2026-09-22 to turn an unreproducible
result into a measured +2.6 percent at p≈0.003.

Carry a **canary**: a rung the change under test cannot affect, read in the same
runs. If it moves more than the effect you are chasing, the comparison is
measuring the machine and not the code, and no amount of reasoning
about the diff will fix that. Both false alarms of 2026-09-21 were caught this
way — `p1_interpreter` moved 17 percent and a tile rung 41 percent between legs
whose diff could not touch either.

Do not use two worktrees. Every cargo project on this machine builds into one
target directory (`~/.cargo/config.toml`), and two worktrees of this repository
produce the *same* artifact filename while cargo tracks freshness per package
path. So each worktree believes its own artifact is current, neither relinks,
and both run whichever binary was built last — `cargo bench --no-run` in each,
alternately, reports `Finished` in 0.3 s and compiles nothing. A pair measured
that way can be the same binary twice, and it will not say so. If you must use
a second worktree, give it a `CARGO_TARGET_DIR` of its own.

Watch each `--no-run` build for `Compiling polydat`. Its absence means cargo
thought the tree was unchanged and you are about to copy the previous leg's
binary over the new one's name.

Criterion's `Performance has regressed` is a statement about the samples it
took, not about the code, and it is the wrong verdict to read. Its statistics
compare samples within a run, not across runs, so it cannot know that the process
before it was laid out differently or that the machine had warmed, and two runs
of one unmodified binary flag each other with p below 0.05 routinely. Read the
per-round deltas above instead, and treat any single flagged cell as a prompt to
collect rounds rather than to start reading diffs — on 2026-09-21 three of the
four cells the first pairing flagged did not survive one. A run whose
confidence intervals are several times wider than its neighbours' was
disturbed and should be discarded before any comparison.

The benchmark source is
[`benches/engine_ladder.rs`](../../benches/engine_ladder.rs), and its cross-engine
gate is
[`tests/engine_ladder_equivalence.rs`](../../tests/engine_ladder_equivalence.rs).

## Interpreting results

Use the absolute times to estimate whether graph execution matters in the
surrounding workload. Use the ratios to understand dispatch and representation
overhead for this topology on the tested machine. Do not treat a single-machine
ratio as a universal constant.

This benchmark does not include graph compilation latency, partially clean
graphs, selective output pulls, mixed P1/P3 cones, SIMD auto-promotion, strings
or reference-backed values, host I/O, or application-level scheduling. Those
are separate questions with different cost centers. See the detailed
[engine design](../design/engines.md) and [SIMD ISA and auto-promotion
study](../design/simd_isa_autopromotion.md) for those execution modes.