perf: skip grapheme regex for ASCII in utf8_clusters#38
Closed
AmitMY wants to merge 1 commit into
Closed
Conversation
utf8_clusters splits every word with regex.findall(r'\X', ...) to find grapheme clusters. ASCII text has no multi-codepoint graphemes (no combining marks), so for ASCII strings list(s) is identical and skips the regex. Non-ASCII still uses \X, so output is unchanged. ~1-3% faster on text-heavy training; memory unchanged. 137 tests pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Author
|
Closing. The original change was subtly incorrect: ASCII does contain one multi-codepoint grapheme — CR LF (UAX#29 GB3) — so Adding a correct guard ( |
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.
utf8_clusterssplits every pretokenized word into grapheme clusters withregex.findall(r'\X', s). ASCII text has no multi-codepoint grapheme clusters (no combining marks), solist(s)is identical there and skips the regex entirely:Non-ASCII strings still use
\X, so output is unchanged — Hebrew/Chinese tests pass unchanged.Output identical, 137 tests pass. Measured back-to-back (50 texts / 200 merges; best of 3):
Memory unchanged. (Build is ~4% of a training run, so this is most of that fraction.)
🤖 Generated with Claude Code