Fix #13950: tokenizer issue: special spans cause every subsequent span to not be cached#13951
Open
jberg5 wants to merge 2 commits intoexplosion:masterfrom
Open
Fix #13950: tokenizer issue: special spans cause every subsequent span to not be cached#13951jberg5 wants to merge 2 commits intoexplosion:masterfrom
jberg5 wants to merge 2 commits intoexplosion:masterfrom
Conversation
jberg5
commented
Mar 31, 2026
| cdef class Tokenizer: | ||
| cdef Pool mem | ||
| cdef PreshMap _cache | ||
| cdef readonly PreshMap _cache # readonly so tests can check state |
Author
There was a problem hiding this comment.
I don't love exposing private state so that tests can read it, but couldn't think of an easy alternative for testing the behavior I wanted to test. Open to suggestions!
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.
Description
Fixes #13950
Basically once a span with special cases appeared (like "can't" or any nonstandard contraction),
has_specialwould be set and remain set for any subsequent spans, even if they weren't special. These spans wouldn't get cached, and performance suffered as a result. Fortunately the fix is pretty straightforward, and we immediately see some decent tokenizer speedups.To illustrate the worst case scenario: if you stick "can't" at the beginning of Huckleberry Finn, it takes 691ms to tokenize on my macbook pro. Reusing the same tokenizer on the same text is another 637ms. After the fix, the first pass takes 230ms, and reusing (everything cached now) takes 87ms. So a 3x speedup on the cold run, and a 7x speedup on the warm run.
Important note: in the average case of tokenizing a diverse, real-world corpora (like a bunch of tweets or something), the impact is more modest because eventually most words will get cached because they appear in a document where they don't follow a special span.
Types of change
Bug fix.
Checklist