Repository navigation
Accelerate forward substring search with bounded fast paths - #7
Merged
Merged
Conversation
Use bounded endpoint probes and native short-pattern search while retaining portable KMP for longer needles. Exercise native and fallback paths with binary differential tests and compare against the baseline on one runner. Co-authored-by: Codex <codex@openai.com>
Use memchr only when the prefix state has fallen back to zero, preserving linear scanning and the existing all-match behavior. Co-authored-by: Codex <codex@openai.com>
Compare the portable search build alongside native before/after results. Co-authored-by: Codex <codex@openai.com>
Stop batch skipping after eight nearby candidates and resume scalar KMP at the first unconsumed candidate. This bounds setup work on periodic inputs and keeps sparse-candidate acceleration portable. Co-authored-by: Codex <codex@openai.com>
Keep the original KMP kernel unchanged. Apply front-window and endpoint filters in a glibc-only forward-search adapter, using native memmem for short patterns. Focus final measurements on the sixteen search cases, including the forced-portable build. Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
Endpoint checks can reject a missing long needle without allocation. If both endpoints exist, enter KMP with the original boundary so the fallback retains its existing compiler specialization. Co-authored-by: Codex <codex@openai.com>
Keep the optional native decision in a non-inlined private function and retain the original KMP call context. Measure six representative search cases across three fresh processes per compiler and variant; sanitizer validation continues to cover all fifteen search cases. Co-authored-by: Codex <codex@openai.com>
Keep sx_find_char on its original memchr specialization and include it in the repeated-process performance comparison. Co-authored-by: Codex <codex@openai.com>
minelogy-dev
marked this pull request as ready for review
November 1, 2026 16:47
This was referenced Nov 1, 2026
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.
Forward substring searches currently allocate a KMP prefix table for every multi-byte needle and scan even when a required endpoint byte is absent. The glibc forward path now checks the front window, probes the bounded first/last-byte ranges, and uses native memmem for needles up to 32 bytes. These paths avoid the prefix-table allocation.
The original KMP kernel is unchanged. A private non-inlined native decision function preserves its compiler optimization context; unresolved long needles enter KMP with their original boundary. Other platforms and the forced-portable configuration use the original implementation directly. The single-byte API retains its direct memchr specialization. Public signatures, binary matching, reverse search, overlapping all-match results, ownership and growth policy retain their semantics.
Validation at
40987da6ddaa2f31ca04d5d7f4545fa0704b7eb4:452314a7ca51f90a7a8fa66930947164af8fc4f6; immutable harness:ecf31d3d65a45466222dfb0f358e176a22d1bc1cfrom Add audited cross-library benchmarks and search diagnostics #6. Allocation counting uses a separate untimed executable.The endpoint-absent cases improve about 62–93x and 183–365x; the common-byte 8B miss improves about 6–9x. Native fast-path cases perform zero prefix-table malloc calls versus one before. Dense long needles still pay KMP's existing fallback-chain cost; the Clang dense 128B case is about 2% slower in this run, with a similar difference in the unchanged portable control. These are hot-input microbenchmarks on a shared runner; small differences require further evidence before attribution. Raw per-process CSV, p10/p90 summaries, generated fixture source and environment metadata are retained as workflow artifacts for 14 days.
This addresses the search diagnosis in #6.
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.
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.