Skip to content

bench: the datasets, rows and grammar BENCH_SPEC actually asks for - #114

Merged
Andste82 merged 1 commit into
mainfrom
bench/bench-spec-datasets
Aug 9, 2026
Merged

Andste82 merged 1 commit into
mainfrom
bench/bench-spec-datasets

Conversation

@Andste82

@Andste82 Andste82 commented Aug 9, 2026

Copy link
Copy Markdown
Member

What this is

bench/ measured four of BENCH_SPEC's eleven rows. This adds the six
that were missing, adapts the whole suite to the API as the fix and optimisation
PRs left it, and puts the spec itself under test so a future gap fails CI
instead of showing up as a blank column in the cross-language table.

There is no issue for this one.

The gap that was there

The three absent datasets are not incidental extras:

  • blob 1MB is the only workload in the spec that reaches CORELIB_PLAN
    §5.1's divisible-run path. Every other dataset is schema-bounded and small
    enough that no flush happens mid-encode, so that path had never been measured
    here at all.
  • composite is the only one that touches a wrapper array, non-ASCII
    UTF-8, depth-3 nesting, a field the encoder is required not to write,
    or a two-byte field header.
  • composite skip-all is the walk-the-message-materialize-nothing path that
    §7.2 item 7 requires be tested and that nothing so far measured.

Driven through the current API

  • encode: blob 1MB one-shot — caller buffer of 1,000,005 bytes, no sink. Sized
    by hand, as BENCH_SPEC insists: the schema is unbounded, so a generated
    MAX_SIZE would be the configured ceiling rather than a bound.
  • encode: blob 1MB streaming — a 4096-byte caller buffer with a flush sink,
    pass-through not granted (this port implements none, so BENCH_SPEC's optional
    blob 1MB passthrough row is omitted rather than stubbed). The sink consumes
    and discards: one XOR'd byte per call plus a byte counter, and the counter is
    what the row's parity size comes from, so a row that did not move the whole
    megabyte fails instead of borrowing the one-shot row's figure.
  • decode: blob 1MB — fed in 4096-byte chunks, so every feed but the last
    returns INCOMPLETE and the field is reassembled across them. reset()
    between messages keeps the reassembly capacity, which is what the API's message
    loop is for; a fresh stream per op would have put a megabyte of allocator
    traffic in every iteration and measured malloc.
  • decode: composite reads into heap-free destinations
    (InlineVector<FixedString<16>,64>, FixedString<384>) for the same reason —
    a std::vector<std::string> would have charged 64 frees and 64 allocations to
    the decoder.

Numbers

All measured on this branch, GCC 15, -O3, x86-64.

bash bench/run_callgrind.sh — Ir/op (deterministic)

Workload                           instr/op     bytes
--------                           --------     -----
encode: u64 array (1000)              35046      9491
encode: typical message                 226        37
encode: blob 1MB one-shot           1000026   1000005
encode: blob 1MB streaming         13009127   1000005
encode: composite                     11514       956
decode: u64 array (1000)              43839      9491
decode: typical message                1275        37
decode: blob 1MB                    3654639   1000005
decode: composite                     22417       956
decode: composite skip-all             7671       956

bench — MB/s

encode: u64 array (1000)        4295.75      decode: u64 array (1000)     3529.74
encode: typical message         2051.08      decode: typical message       377.27
encode: blob 1MB one-shot      42063.92      decode: blob 1MB            16696.61
encode: blob 1MB streaming       970.19      decode: composite             539.41
encode: composite                922.58      decode: composite skip-all   1705.72

perf — unchanged, and still on parity

message size  : 170 bytes
serialize   : 201.7 cycles/op,  71.9 ns/op, 2364.4 MB/s
deserialize : 621.3 cycles/op, 222.3 ns/op,  764.7 MB/s

What the new rows say

The divisible-run path costs 13x. One-shot 1,000,026 Ir against streaming
13,009,127 — one instruction per payload byte against thirteen. pushBytes
takes a memcpy when the run fits the remaining buffer and falls back to a
byte-at-a-time pushByte loop when it does not, and a megabyte through a
4096-byte buffer is entirely the second case. Under MB/s the same gap reads 43x
(42,064 against 970), but that row is bandwidth-bound and Ir/op is the figure
to quote. This is a measurement, not a fix — the fix belongs in a perf: PR,
and this one deliberately changes no library code.

Not-decoding is worth two-thirds of a decode. composite skip-all 7,671
against composite 22,417 — the parse is the smaller half; the destinations are
the rest.

The flatten change, and why two README figures moved

GCC's inlining budget is per translation unit, so simply growing bench.cpp
from four workloads to ten moved encode: typical message from 224 to 330
Ir/op and decode: typical message from 1199 to 1355 — in a library that had
not changed a line. That is a benchmark that cannot be compared with its own past
readings.

The entry points are therefore flatten as well as noinline, which pins each
Callgrind toggle point to its own call tree. Verified: with flatten, main's
four workloads measure identically whether the file holds four workloads or
ten (226 / 1275 / 35,044 / 43,839). The cost is a couple of percent above the
absolute minimum an unconstrained GCC finds for a small file — encode: typical
226 vs 224, decode: typical 1275 vs 1199. A figure that is stable across
releases is worth more here than one that is minimal, and the README's
instruction-count table is updated to the flattened readings with that reasoning
spelled out.

Spec put under test

test_bench_tools used to check only that the three tools agreed with each
other. It now checks that they agree with BENCH_SPEC:

  • the published set is the spec's ten required rows, and no label outside its
    output grammar;
  • both printed tables match that grammar row for row — the harness regexes
    verbatim, not a paraphrase;
  • the cross-port parity sizes hold: blob 1MB = 1,000,005, composite = 956,
    perf = 170;
  • perf is present, runs, and prints all five value lines (BENCH_SPEC: "an
    implementation that ships only two is incomplete").

It drives the real loops over a token budget via SOFAB_BENCH_SECONDS, which
exists for that check alone — nothing that publishes a number changes it, and the
default stays BENCH_SPEC's ~1 s per workload. The whole test runs in 0.7 s.

The Ir table's title becomes pure-C++20, matching the label bench and perf
already print; plain C++ is corelib-c-cpp's wrapper column.

Test evidence

Full ctest green in all three configurations CI builds — Release, Debug, and
-DSOFAB_STRICT_UTF8=0 — 6/6 each. -Wall -Wextra -Wpedantic clean under GCC 15
and clang 21. Confirmed failing first: on main's bench.cpp the new test
reports the six absent BENCH_SPEC rows.

The suite measured four rows of BENCH_SPEC's eleven. The three it never ran are
not incidental: `blob 1MB` is the only workload in the spec that reaches
CORELIB_PLAN §5.1's divisible-run path, `composite` is the only one that touches
a wrapper array, non-ASCII UTF-8, depth-3 nesting, an omitted all-default field
or a two-byte field header, and `composite skip-all` is the only one that
measures the walk-and-materialize-nothing path §7.2 item 7 requires be tested.

Adds all six missing rows, driven through the API as it now is: the one-shot
`blob` row writes into a caller buffer sized by hand (the schema is unbounded,
so a generated MAX_SIZE would be the configured ceiling, not a bound), the
streaming row installs a 4096-byte buffer with a flush sink and no pass-through
permission, and the decode row is fed in 4096-byte chunks with `reset()` between
messages so the reassembly capacity is kept instead of re-acquired. The
composite decode reads into heap-free destinations, so no allocator traffic
lands in a figure that is meant to be the decoder.

What the new rows say, in Ir/op:

  encode: blob 1MB one-shot     1 000 026     decode: blob 1MB     3 654 639
  encode: blob 1MB streaming   13 009 127     decode: composite       22 417
  encode: composite                11 514     decode: composite
                                                     skip-all          7 671

The one-shot/streaming gap is 13x — 13 instructions per payload byte against
one. `pushBytes` takes a memcpy when the run fits the buffer and a
byte-at-a-time `pushByte` loop when it does not, and a megabyte through 4096
bytes is entirely the second case. Under MB/s the same gap reads 43x
(42 064 against 970), but that row is bandwidth-bound and the Ir figure is the
one to quote.

Two supporting changes:

* The entry points are `flatten` as well as `noinline`. GCC's inlining budget is
  TU-wide, so growing the file from four workloads to ten moved
  `encode: typical message` from 224 Ir/op to 330 and `decode: typical message`
  from 1199 to 1355 without the library changing a line. Flattened, the four
  pre-existing workloads measure identically at four workloads and at ten
  (226 / 1275 / 35 044 / 43 839), a couple of percent above what an
  unconstrained GCC finds for a small file. A figure that is stable across
  releases is worth more here than one that is minimal; the README table is
  updated to the flattened readings.
* `test_bench_tools` now holds the tooling against BENCH_SPEC rather than only
  against itself: the published set is the spec's, every label matches its
  output grammar, both printed tables match it row for row, and the cross-port
  parity sizes hold (`blob 1MB` 1 000 005, `composite` 956, `perf` 170). It
  drives the real loops over a token budget through `SOFAB_BENCH_SECONDS`, which
  exists for that check alone — the default stays BENCH_SPEC's ~1 s.

The Ir table's title becomes "pure-C++20" to match the label `bench` and `perf`
already print; plain "C++" is corelib-c-cpp's wrapper column.
@Andste82
Andste82 merged commit 451287b into main Aug 9, 2026
9 checks passed
@Andste82
Andste82 deleted the bench/bench-spec-datasets branch August 9, 2026 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant