Skip to content

Avoid quadratic slicing when aligning words to text - #14080

Open
b84330808 wants to merge 1 commit into
explosion:masterfrom
b84330808:perf-get-words-and-spaces-linear
Open

b84330808 wants to merge 1 commit into
explosion:masterfrom
b84330808:perf-get-words-and-spaces-linear

Conversation

@b84330808

@b84330808 b84330808 commented Nov 15, 2026 •

Copy link
Copy Markdown

Description

util.get_words_and_spaces finds each word with text[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, and Doc.from_json and Tokenizer.explain use it too.

This changes the lookup to text.index(word, text_pos) - text_pos, which returns the same offset without copying. The Japanese get_dtokens_and_spaces and _guess_spaces in training/example.pyx use 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 the examples.py sentences of each language.

pipeline text before after
zh, char 10k chars 10.3 ms [10.0-11.1] 6.2 ms [6.1-6.3]
zh, char 50k chars 94.7 ms [94.0-95.8] 31.5 ms [30.8-32.1]
zh, jieba 50k chars 141.4 ms [140.2-143.5] 106.8 ms [105.9-109.7]
zh, jieba 200k chars 1109.8 ms [1092.4-1124.3] 427.9 ms [426.7-432.4]
zh, pkuseg (spacy_ontonotes) 200k chars 3475.2 ms [3455.8-3498.9] 2777.5 ms [2737.6-2787.2]
vi, use_pyvi = false 20k chars 6.2 ms [6.1-6.3] 3.4 ms [3.3-3.5]
vi, use_pyvi = false 100k chars 74.2 ms [72.5-77.2] 17.6 ms [17.0-18.6]

For 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_spaces on base and patched for 50,000 random inputs (including ones that raise E194), and the old and new _guess_spaces bodies 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 .pyx change is compiled there: pytest spacy/tests/training spacy/tests/doc/test_creation.py spacy/tests/lang/zh spacy/tests/lang/vi spacy/tests/tokenizer gives 288 passed, 500 skipped, 4 xfailed, 5 xpassed. On the same runner, alternating base/patched util.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/tokenizer and spacy/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

  • I confirm that I have the right to submit this contribution under the project's MIT license.
  • I ran the tests, and all new and existing tests passed.
  • My changes don't require a change to the documentation, or if they do, I've added all required information.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant