pyrusmorph: a C++ core from libmorph sped up Russian lemmatization in Python 30×
Russian text lemmatization in Python often slows down the entire NLP pipeline: PyMorphy runs in the interpreter and allocates memory for every word. The author of pyrusmorph has connected a C++ core, libmorph/rus, built on the Zaliznyak dictionary, to Python and claims a 30× speedup. If lemmatization consumes resources in a high-load service, moving it into a native extension is cheaper than buying more servers.
AI-processed from Хабр: NLP; edited by Hamidun News
If you process Russian text in Python, you almost certainly use PyMorphy2 or PyMorphy3. These libraries have become the de facto standard: they are easy to install, straightforward to use, and many ML projects are built on them. But this popularity comes at a cost. Lemmatization, that is, reducing words to their base form, becomes the biggest bottleneck in the pipeline when text volumes are large. Teams then have to scale their infrastructure where a single machine could have sufficed. A recent post on Habr proposes a different route: pyrusmorph, a Python extension over the native C++ library libmorph/rus. The author claims a speedup of roughly 30×.
Why PyMorphy is slow
There are two reasons, and both will be familiar to anyone who has profiled Python code. First, the logic for parsing each word runs in the interpreter. Second, every word triggers many memory allocations for intermediate structures and results. One word is a trifle. But the core is called very often: according to the author's estimate, there is one call for every 6–7 bytes of plain text. That comes to nearly 150,000 lemmatization calls per megabyte. Multiply that by a corpus of news, reviews, or support logs, and the per-word overhead adds up to hours of CPU time.
The usual fix for cases like this is to move the linguistic core into a compiled language. That is exactly what pyrusmorph does.
What is inside libmorph/rus
The library has a long history. It appeared in the late 1990s and is built on the Great Dictionary of A. A. Zaliznyak. It was first used for spelling and grammar checking, and it worked with Microsoft Word and other office suites. Over roughly 30 years it was developed further at Aport, Rambler, and the Ukrainian Meta: it was improved technically, and the dictionary was proofread and expanded with modern vocabulary.
Architecturally, the authors contrast two approaches. A typical implementation uses a directed acyclic graph of words, many heap allocations (including for results), and string or dynamic representations of word forms. libmorph/rus takes a different path:
- The dictionary is stored as serialized radix trees in one contiguous block of about 2 MB, which makes it more compact and more cache-friendly.
- Lookup and parsing follow a stack-only, zero-allocation scheme: all operations run in fixed-size buffers on the stack.
- Lexemes and word forms are represented by numeric identifiers rather than strings.
For low-latency systems, these are classic techniques. The less data you touch and the fewer calls you make to the memory manager, the more stable your response times. The Python wrapper adds only the cost of the call across the language boundary.
What it means in practice
For developers of NLP services, the takeaway is simple. If lemmatization sits at the start of your pipeline, for example in search indexing, corpus preparation for training, or filtering message streams, measure how much time it takes. If the share is significant, replacing the analyzer may yield more than optimizing everything else. A 30× speedup meaningfully reduces compute costs at large volumes.
There are caveats too. The 30× figure is the author's claim for his own test set. On your corpus, the result depends on text length, the share of rare words, and how you call the library: batch processing and one-by-one calls from Python have different overheads. Before changing your stack, run both analyzers on your own data.
Compatibility deserves a check as well. PyMorphy returns tags in the OpenCorpora format, while libmorph/rus uses its own grammatical information scheme and form identifiers. If your code depends on specific tags, you will need a conversion layer. A native extension also complicates building and deployment: you need prebuilt wheels for your platforms or a compiler in your image.
Still, the core idea is useful. For high-load Russian-language systems, morphology is no longer a "free" minor step, and a native core with a compact dictionary and no allocations can well replace horizontal scaling.
- Dec 2, 2026MyLLM moves Claude Code, Grok and GPT to a server: a laptop needs only one token
- Dec 2, 2026Qwen3.8-27B at 4-Bit Quantization Weighs 16.6 GB Instead of 55 GB: How to Read the Filename
- Dec 2, 2026T-Decision-1 with 9B Parameters Beats Copernicus-57B on Russian: 71.6 vs. 71.2 Points
- Dec 2, 2026Meta Launches Muse Charm AI Gadget in the US for $299, With an Agent but No Calls
Key Questions Answered
Why PyMorphy is slow?
There are two reasons, and both will be familiar to anyone who has profiled Python code. First, the logic for parsing each word runs in the interpreter. Second, every word triggers many memory allocations for intermediate structures and results. One word is a trifle. But the core is called very often: according to the author's estimate, there is one call for every 6–7 bytes of plain text. That comes to nearly 150,000 lemmatization calls per megabyte.
What is inside libmorph/rus?
The library has a long history. It appeared in the late 1990s and is built on the Great Dictionary of A. A. Zaliznyak. It was first used for spelling and grammar checking, and it worked with Microsoft Word and other office suites. Over roughly 30 years it was developed further at Aport, Rambler, and the Ukrainian Meta: it was improved technically, and the dictionary was proofread and expanded with modern vocabulary. Architecturally, the authors contrast two approaches.
What it means in practice?
For developers of NLP services, the takeaway is simple. If lemmatization sits at the start of your pipeline, for example in search indexing, corpus preparation for training, or filtering message streams, measure how much time it takes. If the share is significant, replacing the analyzer may yield more than optimizing everything else. A 30× speedup meaningfully reduces compute costs at large volumes. There are caveats too. The 30× figure is the author's claim for his own test set.
Test Frontier LLMs & Autonomous Agents in One Workspace
Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.
Daily 60-sec AI Voice Brief + Weekly Production AI Agent Playbook
Listen to a 60-second AI news brief every morning and get a weekly hands-on playbook for shipping AI agents to production — tools, MCP servers and patterns that work. Curated by Jemal Hamidun, Head of AI at Alpina Digital.
