bench/external design
Design goal
Put a number on how far floating is from the established C libraries, on the
same machine, the same operands and the same measurement protocol, so that a
statement such as “N times slower than MPFR at 512 bits” can be checked and
tracked from release to release. Benchmarks that compare floating with its
own earlier versions cannot say that.
Mathematical background
Let and be the time per operation of floating and of the
reference library for one case (operation and precision). The number the
project is judged by is the ratio of the net times, which are the times of the
operation without the cost of the benchmark loop around it. The method is fixed
in advance and versioned (net-ratio-v1); a change of any number below is a
new method version.
Pairs. Each side is timed in pairs of blocks, in one process: a block of the case, , and a block of its null, (the same loop without the library call, see below), both in ns per operation, each with its own calibrated batch of about 20 ms, run back to back with the order inside the pair alternating. Here is the round and the confirmatory pair of the round ( rounds of pairs, after 3 exploratory pairs per round that are discarded by position). The difference of a pair is
The two blocks of a pair are 40 ms apart, so a drift of the machine that is slower than that cancels in ; it does not cancel in or alone.
Net time and its interval. The net time of a side is the median of all its differences,
Blocks of one round come from one process and share memory layout, frequency state and system noise, so they are not independent. The interval of is a two-level percentile bootstrap that keeps the pairs intact: a replicate draws rounds with replacement, then draws the pairs of each chosen round with replacement, and is the median of the drawn differences; the 2.5% and 97.5% quantiles of 2000 seeded replicates are the interval.
Ratio. . The two sides run in different processes and share no structure, so their replicates are drawn independently, (with when ). Replicate of one side is divided by replicate of the other only to form the distribution of the ratio; it is not a pairing. The seed of every draw is derived from the run seed, the case, the side and the purpose, so two sides never share a stream, even when their data are identical. The interval is the 2.5% and 97.5% quantiles of the . The raw ratio of the 0.11.0 method, the median of the case blocks over the median of the case blocks, is kept beside it, with its interval from a bootstrap that pairs the rounds of the two sides.
Criterion. A case meets the target when . This is a one-sided 97.5% statement made from a two-sided 95% interval, so it is conservative. A resolved case that does not meet it is marked borderline when .
Unresolved. A case is reported as unresolved, and never as met, when (1) the 2.5% quantile of is at most zero, or (the reference net is not distinguishable from zero), (2) , or (3) (the interval is too wide; in the 0.11.0 summary the raw interval has of 1.02 at the median case and 1.07 at the worst). The raw ratio decides such a case and is printed as “met (raw)” or “not met (raw)”; it is never counted among the met cases.
Flags. The null loop scales with the clock, so the null blocks are a
thermometer. With the median of the null blocks of round , a side is
flagged d (drift) when exceeds
5%. A case is flagged o (order) when the mean of over the pairs where the
null ran first differs from the mean over the pairs where it ran second by more
than 5% of . Flags are reported; they remove no data and change no
verdict by themselves (a drift between rounds widens the interval, and an
interval above the width bound makes the case unresolved).
A raw cell is flagged as noisy when the half-width of its median interval exceeds 5% of the median or when the medians of the rounds spread (maximum minus minimum) by more than 10% of the pooled median, on either side.
Nothing is removed from the data after it is measured: not by hand, not by a rule that looks at the result.
Design decisions
One operand file, read by both sides
A seeded generator (Python’s random.Random seeded by a string, stable across
versions) writes operands-per-case = 8 operand sets for every case. Binary
operands are exact ±0x<mantissa>p<exponent> values with exactly the case
precision in mantissa bits and the leading bit set; decimal operands are exact
±<coefficient>E<exponent> values with exactly the case digit count. Parsing
is therefore exact on both sides and needs no rounding mode, and both sides
time the same values. Magnitudes are chosen so that no operation takes a
special-case shortcut: the leading-bit exponent of add, mul, div and
sqrt operands lies in , exp and sin arguments have
, ln arguments lie in ; for decimal the
magnitudes are for arithmetic, for exp and
for ln and sqrt. Every operand is a different random
mantissa, so no result repeats and nothing can be answered from a cache of
results.
Oracle: the reference result of the same operands
The C harness prints the correctly rounded MPFR (round to nearest even) and
libmpdec (HalfEven, allcr = 1) results for every operand to a reference
file. The floating benchmark reads that file as its oracle, so Maremark
validates every floating output against the reference library before it is
timed. A mismatch is reported (it would be a correctness finding) and the case
is still timed, so the table never silently compares different results. The
elementary functions of both libraries are correctly rounded, so for them,
too, equal precision implies equal results.
Same protocol on both sides
The C harness implements the Maremark protocol literally: warm-up passes until both a count and a time are reached, the calibration rule that grows the batch towards a target batch time, then exploratory and confirmatory blocks of a fixed number of passes. The numbers (warm-up, batch target, block counts) are passed to both sides from one place. Only confirmatory blocks enter the statistics.
Alternating sides, shuffled cases
Each round runs the C harness and the floating test as two processes, the
first side alternating from round to round (seeded AB/BA), with one shuffled
case order per round shared by both sides, so a case occupies different times
of the run in different rounds. Seven rounds give each case seven process-level
measurements per side to bootstrap over; between-process variation (core
placement, allocator layout, other load) is larger than the variation between
blocks of one process, and the two-level bootstrap accounts for it. Before each
side starts the runner waits until other processes use under 150% CPU and
records the load it saw.
What is not in the timed region
Operand parsing, context and mpfr_t / mpd_t initialisation, result
buffers (MPFR and libmpdec write into preallocated results; floating writes
into a preallocated array of results), and printing are outside. What stays
inside is the cost of the library’s own API: for MPFR and libmpdec the call
and any internal temporaries, for floating the construction of an immutable
result value, which is how the library is used. That difference is inherent in
the two designs and is part of what the ratio reports.
The null of a case: the same loop without the library call
The null removes everything the library needs to do and nothing else, so that the net time is what the library call costs.
floating. A second implementation of the same case, run by Maremark next to the operation (the blocks of the implementations are interleaved, each implementation has its own calibration). Its closure has the shape of the operation closures, an indirect call with the same arguments returning its first one, and it loads fromexpectedinstead of the operands so that its output validates against the reference. It keeps the closure call, the array loads, the reference-counted store of a result and Maremark’s per-pass work. It removes the library call and the allocation, construction and release of a result. The operation of a case is a closure chosen at run time by the operation name, and the generated C calls it through the closure object (f->code(f, a, b), checked on the release build). A top-level function with a known body would be called directly and could be inlined, which would make the null cheaper than the harness part of the case and the net larger, so the null closure is held in aRefand is called through the closure object the same way (checked in the generated C as well).- MPFR and libmpdec.
pass_nullruns the loop of the case with the same operand and result accesses (the three operand pointers of every row, and the context and the status of the decimal calls) handed to an emptyasmstatement, so the compiler can neither drop the accesses nor fuse the loop. For binary the null computes the three row addresses (a[i],b[i]andr[i]are addresses into arrays ofmpfr_t); for decimal it loads three pointers from the pointer arrays, and takes the address of the context and of the status. That is exactly whatpassdoes before the call.
What the null cannot remove, and what the net therefore still contains:
- The call itself: argument set-up and return of
mpfr_addare in the net of the reference; the direct call into the library is in the net offloating, while the closure that reaches it is in the null. The closure belongs to the harness, the library call to the library. - Cache, branch-predictor and allocator state differ between a loop with and a loop without the call, so the net is an approximation of “the operation alone”, not an identity. Its error is of the order of the variation of the null and is the same for every build of the library, so comparisons between builds are not affected.
- Per-pass and per-block costs (the pair of clock reads,
Bench::keep) are in both blocks and cancel in the difference; they are not removed from the individual times. - The release of the previous result happens in the next call: in the null
out[i]holds a shared value, in the case it frees an allocation. That cost is caused by the operation and stays in the net.
A regression of the time on the number of operations per block was considered and not adopted: it removes per-pass costs but not the per-operation cost of the harness (the closure call, the array traffic, the reference loop), which is the largest part of the floor, so a null would still be needed.
Configurations of the floating side
The floating side is built and published in named configurations, recorded as
floating_configuration in the protocol block of the summary and in the names of
the published files: native (the native limb primitives where the build has
them), native-portable (the portable path forced), and the runtimes wasm and
js. The target of the baseline has to be reached by the portable path as well.
The runner passes the name and the environment variables that select a
configuration (--configuration, --moon-env KEY=VALUE); how the portable path
is selected is defined by the kernel work, not by the benchmark. One summary, one
samples file and one section of the manual page exist per configuration.
Runtimes other than native
--target native|wasm|js chooses the moon target the floating side is built
and run for (wasm runs under moonrun, js under node; the versions are
recorded in meta.runtime). The reference side is always the native C process of
MPFR and libmpdec, run in every round of the same run, so the number of a cell on
another runtime is the library on that runtime against the native reference. That
mixes two differences, the library and the runtime. The second ratio separates
them: the net time of floating on the runtime over the net time of the same
case in native-portable, which runs the same code path. It is computed at
publishing time from the two samples files of the same release (they must have
read the same operand file and used the same protocol; each has its own
configuration in the seed of every draw, so the two resamplings are independent)
and has an interval and no verdict: it is a diagnostic of the cost of the runtime.
wasm-gc is not a target: the benchmark driver uses moonbitlang/async, which
has no wasm-gc runtime (the module initialiser of that package casts between two
struct types of the runtime’s scheduler and the runtime rejects the module at
validation; a reduced reproduction with that package alone shows it, and nothing
is filed).
The criterion of a configuration is one row of a table in the tool
(CRITERIA in tools/external_baseline.py): which ratio is judged and two bounds,
the target and the limit. Every configuration is judged on the net ratio to the
native reference process (MPFR, libmpdec); for wasm and js that is the ratio of
this library on the runtime to the native reference. The upper end of the 95%
interval decides:
| configuration | target | limit | met | tolerated | not met |
|---|---|---|---|---|---|
native, native-portable | 3 | 3 | at most 3 | none | above 3 |
wasm, js | 5 | 10 | at most 5 | above 5, at most 10 | above 10 |
A runtime that compiles while it runs is expected to stay within 5 times the native
reference and may not exceed 10 times; where the limit equals the target there is no
tolerated band. The unresolved and borderline rules above apply to both bounds: a
case whose interval contains the target or the limit is marked borderline, and an
unresolved case shows its raw ratio as met, tolerated or not met. The summary
records the row (criterion_judged, target_ratio, limit_ratio,
criterion_applies); a summary without limit_ratio is read with the limit equal to
the target. A row that judges nothing makes the status of every case
not_applicable and the page prints no verdict for it; no configuration has such a
row today. The ratio to native-portable is never judged.
What the method does differently on a runtime that compiles while it runs:
- Warm-up. The code of a case may still be compiled during the first
blocks. The warm-up time is a number of the method, fixed per runtime before
any cell is measured, from the calibration experiment below. A flag
wis reported when the median net of the first third of the pairs of every round differs from that of the last third by more than 5% of the net; it removes no data. - The floor differs per runtime and is measured in every pair, so the net handles it. A call site of the null’s closure can be inlined where that of the case cannot, which makes the null cheaper than the harness part of the case and the net slightly larger; it errs against the library, and nothing corrects it.
- Calibration of the batch size follows the warm-up, so it is done on warm code.
The calibration experiment of the warm-up
Fixed here before it is run. One experiment per runtime (wasm, js), in the
bench window of the measurement, with just bench external warmup --target T:
- Cases:
binary/add/53,binary/mul/512,binary/ln/2048anddecimal_gda/add/34(a fast case, a mid-size case, an elementary function and a decimal case; the same operand file as the measurement). - Warm-up times: 5, 50, 200, 1000 and 3000 ms. Each (warm-up, case) is run three times, each in its own process, with no exploratory blocks and 60 confirmatory pairs, the standard calibration target of 20 ms per block.
- Statistic of a run: the warm-up drift
wof the design above, the difference of the median net of the first and of the last third of the 60 pairs, in percent of the net. - Rule: the smallest warm-up for which every run (every case and every repeat)
has a drift of at most 2%; the warm-up of the runtime is twice that value, rounded
up to a whole 100 ms. If no warm-up reaches 2%, the experiment is reported and the
runtime is measured only with the
wflag on and the longest warm-up, with the result stated as not resolved by the method. - The value, the table of drifts and the versions of the runtime are published with
the first summary of the runtime and written into the
protocolblock of every later one. The native value is the preset’s 5 ms and is not part of the experiment.
Correctness and invariants
- The operand file and the reference file are checksummed in
machine.json. - A
floatingresult that differs from the reference is counted in the Maremark validation tally and listed inresults.json. - A summary is a function of its samples file (the pairs of every case, per side and round) and of its protocol block;
just bench external recomputerecomputes it and says whether every stored number agrees. - A pair is used only when both of its blocks were measured; exploratory pairs are never used.
- Seeds are recorded, so the round order and the bootstrap are reproducible from the samples; the timings themselves are not.
Alternatives rejected
- A binding from MoonBit to MPFR. It would put FFI marshalling inside the timed region of one side only.
- Comparing against mpmath or gmpy2. Their overhead is the Python interpreter, not the library.
- Timing one operand repeatedly. A single fixed operand lets branch predictors and caches memorise the data path; eight different operands per pass avoid it at a cost of eight times the batch duration.
Boundaries
- Native target only; no claims for JavaScript or WebAssembly.
- One machine, one operating system, one compiler per run; see the performance page for the machine.
- Elementary functions are measured on a few well-conditioned argument ranges; huge arguments and arguments near singularities are not measured.
- The library caches of MPFR (for and ) and the corresponding
caches of
floatingare warm during timing; cold-start cost is not measured. - Only a single thread is used; memory use is not measured.
- The correction for the loop floor moves few verdicts. Computed from the 0.11.0 medians with the floating floor then measured (9.21 ns) and a reference floor between 0.4 and 1.1 ns, 43 of 59 cases are above 3 on the raw ratio and 42 on the net ratio: only
decimal_gdamultiplication at 100 digits (3.03) changes side. The method makes the number honest; it does not move the target.