strings: vectorized ASCII fast paths for length and isvalid - #21
Open
adienes wants to merge 1 commit into
Open
Conversation
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>
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.
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.