Free · Daily brief + weekly playbookDaily 60-sec AI Voice Brief + Weekly Production AI Agent PlaybookGet the brief →
ReleaseInsight · On RadarХабр: NLP→ original

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
pyrusmorph: a C++ core from libmorph sped up Russian lemmatization in Python 30×
Source: Хабр: NLP. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceХабр: NLP ↗
Editor-in-Chief
Key metric #1The dictionary occupies a single block of about 2 MB and keeps the CPU cache efficient.
Key metric #2Word parsing runs in stack-allocated buffers, with no dynamic allocations.
Key metric #3A megabyte of text triggers nearly 150,000 lemmatization calls.

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.

Story

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.

ZK
Zhemal Khamidun — Head of AI at Alpina Digital · MIPT (academic supervisor of the Tech Entrepreneurship department). Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Unified AI Workspace · Nomad

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.

Try Nomad AI →Launch Nomad Agent
Jemal Hamidun

Editor-in-chief · Cite this analysis

Jemal Hamidun

Head of AI at Alpina Digital · Academic Supervisor of the Tech Entrepreneurship Department at MIPT

Head of AI & CPO of AlpinaGPT at Alpina Digital, Academic Lead of Tech Entrepreneurship at MIPT. Founder of Hamidun News and Vibe Academy.

Ready-to-paste citation for journalists, blogs and Telegram channels

Source: Hamidun News — pyrusmorph: a C++ core from libmorph sped up Russian lemmatization in Python 30× https://news.hamidun.com/en/news/16875/pyrusmorph-a-c-core-from-libmorph-sped-up-russian-lemmatizat

What do you think?
Loading comments…