Repository navigation
Add audited cross-library benchmarks and search diagnostics - #6
Merged
Merged
Conversation
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>
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
marked this pull request as ready for review
November 12, 2026 14:35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Audited rerun of the disputed search timings
Fresh benchmark run passed at benchmark commit
7f8a624af4809c9dc606c41533991acd1b51917d, measuring the unchanged patched StringX implementation40987da6ddaa2f31ca04d5d7f4545fa0704b7eb4. This rerun is the current validation of the unusually large gaps discussed below.91aaca138a68b3dbagrees across compilers/processes.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
gcc-14, flushed
clang-19, hot
clang-19, flushed
Raw CSV, full fixture manifests, compiler versions, source/binary hashes, exact release binaries and disassembly are retained as
search-recheck-41466782569for 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.
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)
All clang-19 case medians (ns/op)
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.
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
clang-19
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):
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.