Skip to content

strings: vectorized ASCII fast paths for length and isvalid - #21

Open
adienes wants to merge 1 commit into
masterfrom
pr-48887-dfa-rebased
Open

strings: vectorized ASCII fast paths for length and isvalid#21
adienes wants to merge 1 commit into
masterfrom
pr-48887-dfa-rebased

Conversation

@adienes

@adienes adienes commented Jul 22, 2026

Copy link
Copy Markdown
Owner

length (1- and 3-arg) now skips ASCII runs using cascading vectorized _isascii probes (256 -> 32 bytes, so a failed probe rescans at most 32 bytes) and counts the remaining text with the existing bytewise loop in bounded windows around non-ASCII bytes; strings under 32 bytes use the bytewise loop directly. byte_string_classify locates the exact first non-ASCII byte with 1024-wide probes (replacing the chunk-granular _find_nonascii_chunk), so validation's DFA starts exactly there. Probe guards use subtraction so index arithmetic cannot overflow for a StringView over huge virtual data.

isvalid(::String) is now constant-foldable and removable-if-unused: _UTF8_DFA_TABLE becomes a tuple so lookups infer :consistent, the bounded scan loops carry :terminates_locally, _utf_dfa_step asserts :noub for its always-in-bounds table index, and only :nothrow is asserted at the String boundary.

On Apple Silicon: length is 3-28x faster on ASCII, 1.2-3x on mostly- ASCII, and 1.1-1.6x on dense non-ASCII text; isvalid is up to 3x faster on mostly-ASCII strings with trailing non-ASCII. No measurable regressions. Derived from the length portion of JuliaLang#48887; that PR's GUTF-8 DFA index functions were benchmarked in rebased form and dropped (slower than the existing branch chains once isvalid's inlineability is preserved). Partially addresses JuliaLang#33988.

length (1- and 3-arg) now skips ASCII runs using cascading vectorized
_isascii probes (256 -> 32 bytes, so a failed probe rescans at most 32
bytes) and counts the remaining text with the existing bytewise loop in
bounded windows around non-ASCII bytes; strings under 32 bytes use the
bytewise loop directly. byte_string_classify locates the exact first
non-ASCII byte with 1024-wide probes (replacing the chunk-granular
_find_nonascii_chunk), so validation's DFA starts exactly there. Probe
guards use subtraction so index arithmetic cannot overflow for a
StringView over huge virtual data.

isvalid(::String) is now constant-foldable and removable-if-unused:
_UTF8_DFA_TABLE becomes a tuple so lookups infer :consistent, the
bounded scan loops carry :terminates_locally, _utf_dfa_step asserts
:noub for its always-in-bounds table index, and only :nothrow is
asserted at the String boundary.

On Apple Silicon: length is 3-28x faster on ASCII, 1.2-3x on mostly-
ASCII, and 1.1-1.6x on dense non-ASCII text; isvalid is up to 3x
faster on mostly-ASCII strings with trailing non-ASCII. No measurable
regressions. Derived from the length portion of JuliaLang#48887; that PR's
GUTF-8 DFA index functions were benchmarked in rebased form and
dropped (slower than the existing branch chains once isvalid's
inlineability is preserved). Partially addresses JuliaLang#33988.

Co-authored-by: Codex <noreply@openai.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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