perf: drop redundant per-token regex re-validation in tokenize_and_stem#78
Merged
Merged
Conversation
_WORD_RE.findall() already returns only maximal [a-z][a-z0-9_-]+ runs (letter-led, length >= 2), so the follow-up 'if _TOKEN_RE.match(t)' filter re-checked the exact same shape and always returned True. It was a redundant regex match per token on the index-time and query-time hot path. Remove the filter (and the now-unused _TOKEN_RE) so tokenization does one regex pass instead of two. Output is byte-for-byte identical; verified by a 200k-string fuzz over the two patterns (0 mismatches). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ECr44xGUy4SDEJRSmDPNZb
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.
Problem
retrieval/stemmer.pytokenized text in two regex passes per token:_WORD_RE.findall()returns only maximal runs matching[a-z][a-z0-9_-]+— every returned token is already letter-led and ≥ 2 chars._TOKEN_RE(^[a-z][a-z0-9_-]{1,}$) describes that exact same shape, so_TOKEN_RE.match(t)is always true for findall output. The filter (and the regex it uses) is dead work: a redundant regex match executed for every token, on both the index-time and query-time hot path.Change
Drop the
_TOKEN_REfilter and the now-unused_TOKEN_REpattern. Tokenization now does one regex pass:Benefit
match()per token across every query and every chunk indexed._WORD_RE) instead of being asserted twice.Verification
Equivalence fuzz-tested over 200,000 random strings (mixed letters, digits,
_ - . @ #, whitespace, empty): the oldif _TOKEN_RE.match(t)filter and the new unfiltered list produced identical output in every case (0 mismatches). Existingtests/test_stemmer.py(TestTokenizeAndStem, includingtest_numeric_ignored) continues to hold because numeric-led tokens are excluded by_WORD_REitself, not by the removed filter.🤖 Generated with Claude Code
Generated by Claude Code