perf: tokenize each corpus chunk once during indexing#77
Merged
Conversation
build_index() called tokenize_and_stem() twice on every chunk: once for the [:20] stem_tags metadata and again to build the full BM25 tokenized_corpus. Both calls ran on the same sanitized chunk, so the second pass re-ran the regex tokenizer and stemmer over the entire corpus for no benefit. Tokenize each chunk once, reuse the result for both the BM25 corpus and the stem_tags slice. Output is byte-for-byte identical; index-time tokenization work is halved.
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/indexer.pybuild_index()tokenized every chunk twice:Both calls run
tokenize_and_stem()on the same sanitized chunk text. The second pass re-runs the regex tokenizer and Porter stemmer across the entire corpus purely to rebuild a token list that the first pass already computed (and then threw away after slicing[:20]).Change
Tokenize each chunk exactly once in the build loop, store the full token list in a parallel
tokenized_corpus, and reuse it for both:tokenized_corpus(full tokens), andstem_tagsmetadata (tokens[:20]).Benefit
stem_token()islru_cached, so the stemmer cost of the old second pass was partly hidden — but the regexfindall+ cache lookups still ran over every chunk again. This removes that redundant pass entirely.🤖 Generated with Claude Code
Generated by Claude Code