Skip to content

Add audited cross-library benchmarks and search diagnostics - #6

Merged
minelogy-dev merged 6 commits into
mainfrom
bench/native-string-baseline
Nov 12, 2026
Merged

minelogy-dev merged 6 commits into
mainfrom
bench/native-string-baseline

Conversation

@minelogy-dev

@minelogy-dev minelogy-dev commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Audited rerun of the disputed search timings

Fresh benchmark run passed at benchmark commit 7f8a624af4809c9dc606c41533991acd1b51917d, measuring the unchanged patched StringX implementation 40987da6ddaa2f31ca04d5d7f4545fa0704b7eb4. This rerun is the current validation of the unusually large gaps discussed below.

  • 79 fixtures, 4 adapters, 2 data-cache conditions, GCC 14 and Clang 19, three fresh processes and seven samples per process: 26,544 raw batch samples.
  • An independent byte-window oracle validates every result. Exact lengths, byte content, binary NUL cases, fixture hashes and hit positions are checked. Fixture signature 91aaca138a68b3db agrees across compilers/processes.
  • Separate ASan/UBSan/leak-check builds verify nine adapter calls per fixture/library, totaling 2,844 checked calls per compiler.
  • Release adapters are separate translation units with no LTO and memmem/memchr builtins disabled. Disassembly checks confirm an indirect call inside the timed loop and calls to the underlying search functions; release adapters contain no counters.
  • A raw glibc adapter uses the same SDS buffers with an explicit length. Its timings closely track SDS + glibc.
  • Empty-adapter controls are about 1.87 ns/op; timestamp-pair controls about 33.6 ns. These costs are reported without subtraction.
  • Hot measurements use warmups and calibrated batches. Flushed measurements use CLFLUSH on text and pattern, MFENCE/LFENCE, then time the call; each sample averages eight individually flushed calls. Flushing cost is excluded, metadata is not explicitly flushed.
  • Host: AMD EPYC 7763, CPU 0, glibc 2.39. Flags: -O3 -DNDEBUG -march=x86-64 -fno-lto -fno-builtin-memmem -fno-builtin-memchr. Each group has 21 samples; p10/p90 and all fixture descriptions remain in the artifact.

Findings: the disputed hot-cache values reproduce: StringX's absent-terminal 128B case is about 0.683 us, the SDS common-33 case about 2.403 us, and SDS period-127 about 0.625 us (GCC). After flushing they are about 3.285, 4.799 and 3.304 us. With the same 128B needle and another miss, changing the text period to 126 raises SDS to about 97.704 us; period 125 gives 147.720 us. Rotating period-127 by one byte raises it to 3.251 us. No removed or hoisted search calls were found in the audited timing paths. The gaps are strongly corpus/cache dependent and are not a universal ranking.

The independent-random cutoff check also remains significant: StringX changes from 2.458 us at 32B to 41.056 us at 33B on the GCC seed-0 fixtures, while SDS stays near 2.5 us. Conversely, the rare-terminal tail near-miss favors bstrlib and penalizes glibc. Broad routing decisions should use these controls, rather than only the period-127 or terminal-absent fixtures.

Fresh cross-library tables: hot and flushed data

gcc-14, hot

Case, 64KiB text; all misses StringX µs SDS + glibc µs bstrlib µs raw glibc, same SDS buffer µs
8B miss; first byte absent 0.661 6.156 30.667 6.155
128B repeated prefix; terminal byte absent 0.683 292.094 30.847 292.198
8B common-byte miss 9.135 9.055 48.320 9.029
33B original common-byte miss 48.437 2.403 47.814 2.401
32B independent-random miss 2.458 2.480 48.640 2.483
33B independent-random miss 41.056 2.454 48.174 2.447
128B independent-random miss 41.226 1.783 47.886 1.781
128B needle; text period 125, phase 0 168.492 147.720 121.382 147.706
128B needle; text period 126, phase 0 168.390 97.704 121.982 97.810
128B needle; text period 127, phase 0 168.740 0.625 120.251 0.618
128B needle; text period 127, phase 1 168.364 3.251 121.583 3.253
128B near miss; rare terminal byte at tail 124.175 292.542 31.097 292.228

gcc-14, flushed

Case, 64KiB text; all misses StringX µs SDS + glibc µs bstrlib µs raw glibc, same SDS buffer µs
8B miss; first byte absent 3.238 6.855 30.628 6.700
128B repeated prefix; terminal byte absent 3.285 292.902 31.752 292.280
8B common-byte miss 9.453 9.377 49.675 9.285
33B original common-byte miss 49.257 4.799 48.937 4.862
32B independent-random miss 4.731 4.839 49.725 4.869
33B independent-random miss 42.314 4.808 49.244 4.878
128B independent-random miss 44.815 5.203 48.763 5.242
128B needle; text period 125, phase 0 168.900 148.066 120.519 148.057
128B needle; text period 126, phase 0 168.615 98.352 121.521 98.283
128B needle; text period 127, phase 0 168.554 3.304 119.883 3.291
128B needle; text period 127, phase 1 168.518 6.490 121.151 6.327
128B near miss; rare terminal byte at tail 126.571 292.269 32.095 292.441

clang-19, hot

Case, 64KiB text; all misses StringX µs SDS + glibc µs bstrlib µs raw glibc, same SDS buffer µs
8B miss; first byte absent 0.660 6.155 30.714 6.158
128B repeated prefix; terminal byte absent 0.682 292.291 30.915 292.246
8B common-byte miss 9.068 9.005 48.303 9.024
33B original common-byte miss 79.123 2.403 47.678 2.401
32B independent-random miss 2.464 2.484 47.806 2.482
33B independent-random miss 80.127 2.451 47.338 2.447
128B independent-random miss 80.167 1.781 47.085 1.778
128B needle; text period 125, phase 0 250.808 147.649 104.657 147.704
128B needle; text period 126, phase 0 250.710 97.806 103.046 97.738
128B needle; text period 127, phase 0 250.833 0.614 103.870 0.611
128B needle; text period 127, phase 1 250.742 3.250 102.758 3.253
128B near miss; rare terminal byte at tail 246.513 292.323 31.347 292.141

clang-19, flushed

Case, 64KiB text; all misses StringX µs SDS + glibc µs bstrlib µs raw glibc, same SDS buffer µs
8B miss; first byte absent 3.085 6.730 30.642 6.744
128B repeated prefix; terminal byte absent 3.136 292.481 30.712 292.244
8B common-byte miss 9.343 9.367 49.153 9.342
33B original common-byte miss 79.755 4.595 48.132 4.596
32B independent-random miss 4.502 4.820 48.923 4.889
33B independent-random miss 81.332 4.705 48.023 4.904
128B independent-random miss 80.877 5.108 47.865 5.165
128B needle; text period 125, phase 0 250.887 147.936 108.454 147.940
128B needle; text period 126, phase 0 250.938 98.398 101.657 98.237
128B needle; text period 127, phase 0 250.889 3.196 100.634 3.221
128B needle; text period 127, phase 1 250.734 6.478 100.658 6.306
128B near miss; rare terminal byte at tail 248.879 292.762 35.693 292.611

Raw CSV, full fixture manifests, compiler versions, source/binary hashes, exact release binaries and disassembly are retained as search-recheck-41466782569 for 14 days. Reproduction and measurement contracts. Shared-runner frequency/scheduling and address-layout variation remain limits; observed spreads are not confidence intervals.

Earlier baseline, diagnostics and original interpretation

Adds a reproducible benchmark suite against immutable SDS and bstrlib revisions without changing StringX's implementation. The 28 workloads cover 82 case/library combinations per compiler: creation/copy/free, reserved and growing append batches, lexicographic comparison, search hits/misses and repetitive prefixes, owned substrings, borrowed views and split ownership variants.

The suite also includes diagnostic-only search experiments: same-object allocator substitution, prefix/scan controls, logical traces, compiler remarks and disassembly. Production library code is unchanged.

Each compiler first runs an ASan/UBSan/leak-checked correctness binary, then a separate -O3 -DNDEBUG -march=x86-64 -fno-lto timing build. GCC 14 and Clang 19 run on the same pinned CPU with 11 shuffled samples per combination; a third untimed binary counts allocation calls and cumulative requested bytes. The workflow retains 1,804 raw timing samples, allocation counts, source hashes, compiler/CPU metadata, quantiles and command logs. See benchmarks/README.md for contracts and reproduction.

Validation: benchmark run, CI and CodeQL all passed for 7494b31. Measured implementation is StringX main at 452314a. Host: AMD EPYC 7763 Azure VM, CPU 0, glibc 2.39; GCC 14.2.0 and Clang 19.1.1. These are hot-input microbenchmarks on a shared runner, not a deployment-wide performance ranking.

Workload (GCC 14) StringX µs/op SDS µs/op bstrlib µs/op
Create/copy/free 16 B C string 0.0441 0.0240 0.0312
Append 1024 x 1024 B, automatic growth 31.9370 27.4454 30.1430
Compare equal 64 KiB strings 1.2967 1.2905 40.8838
Find one byte, 64 KiB miss 0.6537 0.6466 0.6501
Find 8 bytes, 64 KiB miss 40.9203 6.1571 30.6429
Find 128-byte repeated prefix, 64 KiB miss 122.9512 292.0412 30.7244
Owned split, comma, 4096 B 14.9948 5.5280 6.3309

SDS search rows use glibc memmem/memchr, not an SDS search algorithm. Owned StringX splitting explicitly materializes its native views; native view-only split is measured separately. One append operation builds and frees the whole 1 MiB output. Full tables below retain those contracts.

The clearest optimization targets are normal-input multi-byte search (6.65x / 9.97x the glibc time for the GCC/Clang 64 KiB miss case), small-string allocation (3 malloc calls versus SDS 1 and bstrlib 2), and growth above 64 KiB (22 realloc calls versus SDS 8 and bstrlib 7 in the 1 MiB automatic-growth batch). Reserve improved StringX's large append batch by 13–18%. Large comparisons and single-byte search are already close to the SDS/glibc baselines. The repeated-prefix search counterexample favors StringX over glibc, so replacing all search paths with one alternative is not justified by these data.

Raw CSV/JSON and metadata are attached to the benchmark run as string-benchmarks-41270928080 and retained for 14 days. Allocation requested-byte totals are traffic, not peak live memory. Sample p10/p90 are observed spread, not confidence intervals. All medians are preserved below for review.

All gcc-14 case medians (ns/op)
Case StringX SDS bstrlib
copy_cstr_16 44.07 24.00 31.16
copy_cstr_64 43.94 24.99 31.82
copy_cstr_1024 55.01 37.83 62.69
copy_cstr_65536 2054.85 2127.36 2037.52
copy_bytes_64 43.43 21.51 29.33
copy_bytes_65536 1749.46 1651.76 1399.34
append_1x4096_auto 46186.89 43068.41 30971.23
append_1x4096_reserved 46267.20 39739.85 30857.36
append_16x256_auto 2797.46 2658.37 1951.78
append_16x256_reserved 2668.15 2420.05 1805.03
append_1024x1024_auto 31937.02 27445.42 30143.02
append_1024x1024_reserved 26214.79 28004.07 23787.92
compare_equal_64 5.62 7.49 48.78
compare_equal_65536 1296.75 1290.51 40883.85
compare_last_diff_65536 1297.85 1291.25 40885.77
find_char_end_65536 653.65 646.75 650.41
find_char_miss_65536 653.66 646.57 650.09
find8_front_64 26.81 29.07 12.76
find8_end_1024 668.38 144.67 494.77
find8_end_65536 40915.84 6217.00 30623.25
find8_miss_65536 40920.30 6157.13 30642.90
find128_repeated_miss_65536 122951.23 292041.24 30724.36
substr_copy_64 41.51 20.29 28.73
substr_copy_4096 134.73 114.28 117.17
substr_view_64 22.74 2.34 3.74
split_owned_comma_4096 14994.82 5528.04 6330.91
split_owned_pair_4096 14834.34 17436.29 24668.04
split_views_comma_4096 12494.38 — —
All clang-19 case medians (ns/op)
Case StringX SDS bstrlib
copy_cstr_16 45.83 24.33 32.76
copy_cstr_64 46.25 24.62 33.38
copy_cstr_1024 53.72 43.37 54.87
copy_cstr_65536 2069.92 2228.61 2052.37
copy_bytes_64 44.38 20.94 29.97
copy_bytes_65536 1384.24 1558.12 1380.45
append_1x4096_auto 42418.64 45548.98 25818.53
append_1x4096_reserved 42357.02 42267.85 25655.20
append_16x256_auto 2670.34 2863.69 1647.15
append_16x256_reserved 2570.21 2534.02 1506.00
append_1024x1024_auto 29925.60 27390.80 30409.53
append_1024x1024_reserved 25967.41 26524.73 23409.76
compare_equal_64 5.94 6.56 43.97
compare_equal_65536 1292.94 1288.26 40846.38
compare_last_diff_65536 1294.94 1289.09 40839.57
find_char_end_65536 652.70 646.81 649.37
find_char_miss_65536 652.94 646.25 649.53
find8_front_64 29.61 29.00 12.82
find8_end_1024 988.06 142.78 336.73
find8_end_65536 61347.86 6205.13 20453.67
find8_miss_65536 61370.09 6156.56 20456.05
find128_repeated_miss_65536 245390.91 292353.29 20752.90
substr_copy_64 48.34 20.26 28.99
substr_copy_4096 131.16 114.48 117.54
substr_view_64 24.44 3.13 4.07
split_owned_comma_4096 16719.11 19785.07 7171.35
split_owned_pair_4096 16615.90 17557.01 21096.26
split_views_comma_4096 14195.89 — —
Search diagnosis: controlled experiments and generated code

Diagnostic run passed for ecf31d3, together with CI and CodeQL. This run used an AMD EPYC 7763, pinned CPU 0, GCC 14.2 / Clang 19.1, -O3 -fno-lto, and nine shuffled samples. A preceding run on EPYC 9V45 reproduced the allocation/scanning distinction; absolute times should be compared only within a run.

Allocation is not the dominant cost of these long-input searches. In the same wrapped executable, the same compiled sx_find code takes 61.21 us with normal allocation and 61.30 us with a preallocated prefix-table arena for the Clang 8-byte/64-KiB miss. The repeated-prefix case takes 245.38 vs 245.27 us. Prefix construction and the original scan remain enabled in arena mode. The standalone malloc/free control is about 13 ns; prefix construction is about 8 ns for length 8 and 0.52 us for length 128. These component controls are not an additive runtime decomposition. Short front hits do benefit from avoiding allocation: 29.29 to 21.21 ns in this run.

The 8-byte miss is a scalar traversal without candidate skipping. The original fixture searches eight Z bytes in text containing only a-y. KMP performs 65,536 logical text comparisons and no fallback. Both compilers report that the loop is not vectorized. glibc memmem 2.39 uses pair-hash/shift skipping for this needle length; bstrlib has a two-character unrolled scan. A resolved-libc memchr byte-probe control uses SIMD and takes about 0.65 us, but is only a necessary-condition probe, not a complete substring-search replacement. The added common-first-byte input still shows a gap: StringX 56.63/77.81 us versus glibc 9.08/9.07 us for GCC/Clang, so the result is not solely an absent-first-byte artifact.

The 128-byte repetitive miss adds a serial state-dependency chain. Searching 127 a bytes followed by b in an all-a text repeatedly transitions q=127 -> pi[126]=126 -> matching a -> q=127. The trace records 65,409 text fallbacks and 196,480 logical source comparisons for 65,536 text bytes, versus bstrlib's 65,536 comparisons and zero match-start resets. Logical comparisons are not retired instructions. In this case bstrlib's current character settles on b and its remaining work is an unrolled scan for that absent byte.

Clang's if-conversion is a material contributor in the repeated-input path. The actual forward-FIRST assembly retests equality after the fallback loop and updates q with cmp -> sete -> add. The diagnostic register-asm control preserves a conditional increment, emits no CPU instruction for the asm itself, and leaves the state transitions/results unchanged. This experiment is not a production patch.

Precomputed-table repeated-prefix scan GCC 14 us Clang 19 us
Original KMP loop shape 122.33 244.54
Conditional-branch control 122.33 123.73
Equivalent match-first C loop 143.42 127.01

The control supports a code-generation/loop-dependency explanation for a large part of Clang's extra cost. The C rewrite regresses GCC, so it is not a portable fix as-is. Dedicated KMP or table precomputation alone also does not produce a consistent improvement.

Neither comparison algorithm wins every input. For the repeated-prefix case, glibc's modified-Horspool shift table gives the all-aa text an effective advance of 127-126=1 byte, while still paying pair-hash/table-lookup work; this explains why its timing is poor despite its advantage on the 8-byte random-text miss. Conversely, on alternating ab text with an aaaaaaab needle, bstrlib restarts the match 10,921 times, and does 152,903 logical comparisons versus KMP's 98,304. In the wrapped GCC run StringX takes 41.03 us and bstrlib 119.31 us.

Hardware perf_event access was denied by the runner. No retired-instruction, cache-miss or branch-misprediction rates were measured, so those are not claimed. Raw timings, logical counters, vectorization remarks, source-interleaved disassembly and resolved libc disassembly are in the workflow artifact string-benchmarks-41321744887. See benchmarks/SEARCH_DIAGNOSIS.md for reproduction and limitations.

Patched StringX versus SDS and bstrlib: same-run library comparison

These rows use patched StringX at 40987da from #7, with peers measured in the same executable/processes and the same original benchmark run. Saved-results report reads the existing artifact without new timing measurements. Host: EPYC 7763, glibc 2.39, CPU 0, -O3 -march=x86-64 -fno-lto; three processes and eleven samples per process. SDS search here is glibc memmem/memchr.

gcc-14

Case, 64KiB text StringX µs SDS + glibc µs bstrlib µs
1B tail hit 0.653 0.647 0.650
8B miss; first byte absent 0.659 6.158 30.654
128B repeated-prefix miss; last byte absent 0.671 292.362 30.787
8B miss; common endpoint bytes 9.083 9.013 48.246
8B periodic miss 26.159 25.876 119.391
33B miss; common endpoint bytes 48.395 2.391 47.773
128B periodic miss; both endpoints present 168.312 0.621 120.363

clang-19

Case, 64KiB text StringX µs SDS + glibc µs bstrlib µs
1B tail hit 0.654 0.646 0.650
8B miss; first byte absent 0.661 6.161 20.506
128B repeated-prefix miss; last byte absent 0.672 292.554 20.836
8B miss; common endpoint bytes 9.067 9.113 40.116
8B periodic miss 26.167 25.983 86.559
33B miss; common endpoint bytes 77.280 2.395 39.730
128B periodic miss; both endpoints present 254.658 0.612 94.361

The common-byte 8B case is approximately tied with SDS/glibc and 4.4–5.3x faster than bstrlib. Endpoint-absent cases strongly favor the new filters. The 33B fixture is 20–32x slower than SDS/glibc and exposes the private 32B cutoff. The synthetic 128B periodic fixture is 271–416x slower than SDS/glibc and 1.4–2.7x slower than bstrlib, but its period is coupled to glibc's maximum skip and strongly favors that algorithm; this ratio is specific to this corpus, rather than an average long-pattern result. See the source-model analysis below.

Why 0.671 / 2.391 / 0.621 microseconds occur: exact fixture and search-path analysis

The 0.671 us StringX case is text containing only a bytes and a needle consisting of 127 a bytes followed by b. The front window fails, the first-byte probe returns immediately, and a bounded memchr scans 65,408 bytes for the absent b. It returns before KMP preprocessing or prefix-table allocation. The same run's single-byte full-buffer scan takes about 0.653 us. This is a hot-cache SIMD-scan measurement.

The glibc 2.39 memmem path for lengths 3..256 uses pair-hash modified Horspool. Replaying the exact deterministic fixtures against that source gives the following logical counts (source model, not measured hardware events):

Fixture Pair-hash probes 8-byte filter calls Full-prefix memcmp calls
Common-byte 8B miss 9,489 0 52
Common-byte 33B miss 2,221 9 0
128B, all-a text, final b absent 65,409 0 0
128B, text (a^126 b) repeated 516 0 0

For the 33B fixture, the maximum skip is 32; the observed source-model advance averages about 29.5 bytes per probe. The 8B counterpart averages 6.9 bytes. A longer needle can therefore search this text faster.

The 0.621 us fixture is unusually favorable to glibc: its text period is 127, exactly the 128B needle's maximum skip (m-1). Probes at offsets 127, 254, ... always inspect ba. The needle contains only aa and ab pairs, so ba hashes to a zero shift-table entry and each probe skips 127 bytes. It makes no candidate memcmp calls. The existing END_PRESENT generator couples the text period to m-1, creating this favorable alignment by construction.

Changing the text period independently from 127 to 126 (while keeping the same 128B needle and a miss) raises the source-model probe count from 516 to 21,813; period 125 gives 32,487. Those are logical controls, not new timing results. Rotating the period-127 text also changes filtering work. The model agrees with an independent naive oracle on the exact fixtures and 2,000 randomized cases.

The 271–416x dense-128 ratio is thus specific to this synthetic corpus. It is useful as an algorithm-sensitive counterexample, but cannot establish an average long-pattern ranking. A broader corpus should vary period, phase, terminal-byte frequency, and hit position independently of needle length. The separate common-33 fixture still exposes the fixed 32B routing cutoff.

Source: glibc 2.39 memmem.c. The harness reuses hot fixtures, compiles library and worker sources separately, disables LTO, and includes a memory-clobber barrier in each iteration. These checks support the source-path explanation; the logical model is not a replacement for auditing the exact timed binary.

minelogy-dev and others added 3 commits November 1, 2026 11:52
Add reproducible correctness-checked benchmarks, separate allocation counting, raw samples and runner metadata without modifying library implementations.

Co-authored-by: Codex <codex@openai.com>
Add diagnostic-only kernels, same-object allocator ablation, logical comparison traces and native disassembly. Keep production search code unchanged and expand input distributions before choosing optimizations.

Co-authored-by: Codex <codex@openai.com>
Add diagnostic-only equivalent scan loops and preserve all generated forward-loop snippets. Production code remains unchanged.

Co-authored-by: Codex <codex@openai.com>
@minelogy-dev minelogy-dev changed the title Add reproducible string benchmarks against SDS and bstrlib Add string benchmarks and search-cost diagnostics Nov 1, 2026
minelogy-dev and others added 3 commits November 1, 2026 09:02
Read existing result files without rerunning benchmarks, and publish
StringX, SDS and bstrlib statistics in logs and the job summary.

Co-authored-by: Codex <codex@openai.com>
Validate exact bytes and lengths with a naive oracle; use opaque separately
compiled adapters, call-count checks, and release disassembly. Reproduce old
fixtures alongside independent period, phase, length, seed and hit controls,
with hot and CLFLUSH-based conditions across three fresh processes.

Co-authored-by: Codex <codex@openai.com>
… checks

Keep warnings fatal for benchmark sources while avoiding formatting-only
warnings in immutable dependency headers. Verify Clang first.

Co-authored-by: Codex <codex@openai.com>
@minelogy-dev minelogy-dev changed the title Add string benchmarks and search-cost diagnostics Add audited cross-library benchmarks and search diagnostics Nov 2, 2026
@minelogy-dev
minelogy-dev marked this pull request as ready for review November 12, 2026 14:35
@minelogy-dev
minelogy-dev merged commit 9860724 into main Nov 12, 2026
9 checks passed
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