0062. Bounded libc scans and decimal parse fast paths
- Status: Accepted. Long-needle search refined by ADR-0189.
- Date: 2026-10-31
- Plan: M8.5
- Review: Compiler and backend
Context
The streaming CSV profile spends most CPU in line/field scans and decimal parsing. String searches with frequent first-byte decoys lose to Python. The existing reader already scans LF with memchr; materialized and lazy in-memory lines do not. Streaming and retained-view ownership must survive. Measurements are in the profile and builtin results.
Decision
Use bounded C11 memchr for in-memory LF scans and single-byte splits/searches. For longer needles, retain first-byte scanning and the full memcmp. If a candidate's final byte differs, scan for the next matching final byte before comparing: skipped starts provably cannot match. Never read beyond either view.
Parse at most 18 digits without per-digit overflow checks after optional sign removal. All longer inputs, including arbitrary leading zeros, use the constant quotient 922337203685477580 and sign-dependent final digit 7/8. Keep strict ASCII, empty/sign-only rejection, both i64 bounds and the existing safe MIN conversion.
Rely on the platform libc's dispatched scans, which can use SIMD where supplied; the runtime itself uses only portable C11. Keep the 64 KiB line reader and 1 MiB tool chunks, promotion rules, allocation policy and map representation intact.
Alternatives considered
- Hand-written SIMD/intrinsics — libc already supplies accelerated bounded scanning. No measurement here establishes an additional win worth dispatch, architecture-specific code and a duplicate scalar fallback.
- Read whole files or specialize the corpus vocabulary — would change the streaming/memory contract or benchmark generality rather than tune builtins.
- Reject decimal strings longer than 19 bytes — would reject valid arbitrary leading zeros. The checked long path preserves exact existing semantics.
- Change hashing or fuse map updates now — belongs to M8.6; record measured costs and an index recommendation without changing maps here.
Consequences
No source-language or diagnostic changes. Differential boundary goldens and an independent byte-search oracle cover both paths; the benchmark runner checks byte-identical outputs before timing. Search remains potentially quadratic for needles whose two endpoints match at every position but whose interior does not; this is not a general two-way substring-search implementation. M8.6 owns the remaining map-heavy work. No dependency is added.
The guarded ASCII whitespace bitmask is retained after 21 interleaved pairs: word-freq improves 2.9% best / 3.2% median and trim 6.7% / 13.5%, wc/tok stay within noise or improve, all 256 bytes match the old predicate, and the release emitted-C digest is unchanged.