Repository navigation
Conversation
get_words_and_spaces looked up each word with text[text_pos:].index(word), which copies the rest of the text once per word. For long texts the alignment step becomes quadratic in the text length. The Chinese and Vietnamese tokenizers call it on every doc, as do Doc.from_json and Tokenizer.explain. Use text.index(word, text_pos) - text_pos instead, which finds the same position without the copy. The Japanese get_dtokens_and_spaces and _guess_spaces in training/example.pyx used the same pattern and get the same change.
This branch has not been deployed
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
util.get_words_and_spacesfinds each word withtext[text_pos:].index(word). The slice copies the rest of the text once per word, so aligning a long text is quadratic in its length. The Chinese tokenizer (char, jieba and pkuseg) and the Vietnamese tokenizer call it on every doc, andDoc.from_jsonandTokenizer.explainuse it too.This changes the lookup to
text.index(word, text_pos) - text_pos, which returns the same offset without copying. The Japaneseget_dtokens_and_spacesand_guess_spacesintraining/example.pyxuse the same pattern, so they get the same one-line change.Timings of
nlp(text)on blank pipelines, median of 5 alternating base/patched runs (each run is the median of 5 calls), with the range in brackets. Windows 11, i7-9700, Python 3.11. The texts are built from theexamples.pysentences of each language.use_pyvi = falseuse_pyvi = falseFor Japanese there is no measurable change, because Sudachi rejects input over 49149 bytes and its own tokenization dominates at that size. The change there is only for consistency.
The output is unchanged. I compared
get_words_and_spaceson base and patched for 50,000 random inputs (including ones that raise E194), and the old and new_guess_spacesbodies for 200,000 random inputs; the results matched. I also built this branch from source on Linux (GitHub Actions ubuntu-latest, Python 3.12), so the.pyxchange is compiled there:pytest spacy/tests/training spacy/tests/doc/test_creation.py spacy/tests/lang/zh spacy/tests/lang/vi spacy/tests/tokenizergives 288 passed, 500 skipped, 4 xfailed, 5 xpassed. On the same runner, alternating base/patchedutil.py(3 rounds, median of 5 calls each): zh char 50k chars 73-74 ms -> 22 ms, zh jieba 200k chars 759-762 ms -> 300-303 ms, vi 100k chars 58.8 ms -> 13.2 ms.Tests run:
spacy/tests/doc/test_creation.py,spacy/tests/doc/test_json_doc_conversion.py,spacy/tests/lang/{zh,ja,vi},spacy/tests/tokenizerandspacy/tests/training(with jieba, spacy-pkuseg, pyvi and SudachiPy installed): 520 passed, the same as on master.Types of change
Performance fix, no behaviour change.
Checklist