← Research
Dec 2026

Is it always worth rewriting Python in Rust?

I took a RAG pipeline and its MCP server from a master's exercise, ported them line by line from Python to Rust, and measured what changed. Short answer, no. Rust wins where the program runs Python code, not where Python already calls native code.

Repository ↗

Motivation

One of the first exercises in my master’s in AI was a small RAG pipeline in Python: chunk some documents, turn them into embeddings, retrieve the right chunks for a question, and serve it all through an MCP server. The point was to get familiar with the vocabulary.

I write Rust every day, so the obvious question popped up. What happens if I rewrite the exact same thing in Rust? Is it faster? And more interesting to me: is it always worth migrating Python to Rust, and if not, when is it?

The setup

The use case is an assistant for a fictional hospital: 20 Markdown documents, three different encoders, cosine retrieval, and an MCP server with six tools. One tool searches the documents, and the other five look up things like free beds or who’s on call today.

I ported it line by line. In Python it runs on sentence-transformers with PyTorch and the official FastMCP server. In Rust it runs on candle, Hugging Face’s pure Rust ML library, and the official rmcp SDK. Both use the same model files, the same tokenizer and the same machine, an Apple M3 Pro on CPU only.

First, do they compute the same thing?

A speed comparison means nothing if the two versions don’t produce the same results. So before timing anything, I checked that they match: the same 41 chunks, the same 34-configuration results table, the same 680 retrieved chunks, embeddings that differ by less than a millionth, and the same answers from the 8 MCP tool calls I compared. Only then did I start measuring.

What happened

How many times faster Rust was than Python in each test, on a log scale. Rust wins big on the MCP server and on startup, and loses on computing embeddings. same speedMCP get_beds, 32 clients88×MCP get_beds, 1 client17×MCP server startup6.6×Load the E5 model3.8×MCP search, 8 clients2.3×Whole RAG process1.2×MCP search, 1 client0.9×One E5 query0.7×Full experiment grid0.7×Index with E50.5×← Python fasterRust faster →
How many times faster Rust (with Apple Accelerate) was than Python, on a log scale. Green is Rust faster; red is Python faster.

The RAG side surprised me. Computing embeddings is most of the work, and on the Python side PyTorch does it in C++. PyTorch was 1.5 to 2.4 times faster than candle, even with Apple’s Accelerate library on both sides. Even chunking, which I expected Rust to win easily, didn’t, because Python’s string functions are written in C. The whole RAG process ended up only 1.2 times faster in Rust.

The MCP server was the opposite. A simple tool call is almost all framework code: parsing JSON, validating, dispatching. Rust served 17 times more requests with one client and 88 times more with 32 clients. It also starts in 1.1 seconds instead of 7.1, and deploys at about a third of the size once you count the model files.

But part of that gap isn’t the language. FastMCP runs sync tools right on the event loop, so the Python server handled one request at a time. Making the tools async or adding workers would close a lot of it without rewriting anything. I haven’t measured that yet.

One more thing that stuck with me: precomputing word sets made a naive lexical baseline 48 times faster in Python. Porting the naive version to Rust only gave 1.4 times. The algorithm mattered way more than the language.

The rule I ended up with

Migrate to Rust only when all three are true:

  1. A well-configured Python service misses a measured target for latency, throughput, startup or memory.
  2. Profiling says the problem is in pure Python code, in startup, or in deployment. If it’s inside a native kernel, change the engine, not the language.
  3. The gain pays for the rewrite and for maintaining two stacks. If it’s one hot function, a Python extension written in Rust is cheaper than a full rewrite.

Caveats and what’s next

This is one machine, CPU only, and one run per configuration, with a small corpus. Two AI reviewers went over the first version and caught real mistakes, like calling library imports “interpreter startup” and forgetting the model in the deploy size, and those are fixed in the repo. Next I want to repeat every run several times, give the Python server a fair setup with async tools and more workers, and run both sides on the same inference engine. If those tie, the encoder was never a language question at all.