0189. Bound native substring search on repetitive input
- Status: Accepted
- Date: 2026-11-08
- Plan: M8.5,
R07 - Review: Compiler and backend
Context
The endpoint skips in ADR-0062 work for normal text. With repeated bytes at both endpoints and a different byte inside the needle, each candidate can require a full-length comparison.
Decision
Supersede the long-needle search clause of ADR-0062. Keep the bounded libc path for single bytes and short needles. For long needles, keep the endpoint skips and reject mismatching short prefixes cheaply. After a fixed number of unsuccessful full comparisons, use Two-Way matching for the remaining candidates. Search work is linear in the combined input lengths and uses constant extra storage. The algorithm is described in the Crochemore and Perrin paper.
Alternatives considered
- Use Two-Way on all input — pays preprocessing on short and ordinary searches that finish quickly with libc scans.
- Allocate a prefix table — adds auxiliary storage proportional to needle length in a runtime operation that currently needs none.
- Use platform
memmem— outside the portable C11 runtime and did not improve the audit's repetitive case on macOS.
Consequences
Bytewise first-match offsets and empty-needle behavior stay fixed. The native reference test and the search benchmark exercise the fallback, including the repetitive case. The interpreter keeps the same bytewise semantics.