Moving a CPU-bound Python hot path to Rust with PyO3

· rust, python, pyo3, performance

Problem

A Python web service spends most of each request in a CPU-bound function: a loop over records doing string normalization, hashing, a lookup in a large reference table, some arithmetic and a fuzzy string match. Under Gunicorn with threads, that kind of endpoint hits two limits:

The question: if only the hot function moves to Rust, with the GIL released while it runs, what changes in latency, throughput, CPU and memory, with the rest of the service still in Python?

Approach

I built a small synthetic version of that service, with random data from a fixed seed. POST /transform takes 64 records. For each record it:

  1. normalizes a name (lowercase, ASCII letters and digits only)
  2. computes a 64-bit FNV-1a hash of it
  3. looks the record’s id up in a 1,000,000-row reference table
  4. scores it: a 16-value dot product with the row’s weights, plus a hash term
  5. fuzzy-matches the name against 4 candidate category labels (Levenshtein distance) and keeps the closest

There are two implementations of transform(records), chosen by an environment variable. Everything else (Flask, JSON parsing, Gunicorn config) is identical.

Python. Each worker loads the table at startup into a dict from id to row index, plus compact array('I') and array('f') columns. That’s already the memory-friendly way to hold it in pure Python.

Rust via PyO3. The table is one binary file, memory-mapped and searched with binary search, so all workers share one copy in the page cache. Each call has three steps:

// 1. Copy inputs out of Python objects (GIL held).
let inputs = extract(records)?;
// 2. Compute with the GIL released: other request threads keep running Python.
let outputs = py.detach(|| compute(table, &inputs)); // `detach` was `allow_threads`
// 3. Build the Python result dicts (GIL held again).
to_py_list(py, outputs)

A test checks that both versions return identical results, float scores included, on the benchmark payload and on random inputs.

Setup: AMD Ryzen 5 9600X (6 cores, SMT), Linux 7.0, Python 3.14.4, PyO3 0.30, Flask 3.1, Gunicorn 23 (gthread). The server is pinned to 4 physical cores (8 hardware threads); the load generator, oha 1.16, is pinned to the other 2. Each point runs at a fixed request rate (open loop, with latency correction, so slow responses don’t hide queueing) for 20 s after a 5 s warm-up, over 64 connections.

Results

On one thread, one call with 64 records takes 3.06 ms in Python and 89 µs in Rust, about 34× faster. Under load the gap is smaller, because Flask, JSON and converting objects in and out still run in Python on every request.

p99 latency vs offered load. Both Python configurations climb from single-digit milliseconds to seconds between 500 and 1,500 requests/s; the Rust version stays between 0.6 and 2.6 ms up to 8,000 requests/s.

Achieved vs offered requests/s. Python flattens at about 880 (4 workers) and 1,140 (8 workers); Rust tracks the offered load to 8,000 and flattens near 9,750.

Python, 4 workers × 8 threads Python, 8 workers × 4 threads Rust via PyO3, 4 × 8
Max sustained throughput ~880 req/s ~1,140 req/s ~9,750 req/s
p50 / p99 at 500 req/s 3.7 / 9.6 ms 3.7 / 9.5 ms 0.48 / 0.62 ms
p50 / p99 at 1,000 req/s saturated (p99 2.5 s) 9.3 / 34 ms 0.38 / 0.74 ms
CPU at 1,000 req/s 4.0 cores (capped) 5.4 cores 0.43 cores
p99 at 3,000 / 8,000 req/s saturated saturated 1.1 / 2.6 ms
Memory (PSS, all processes) 917 MiB 1.8 GiB 127–138 MiB
Worker startup (load table) 0.57 s 0.57 s 0.04 ms (mmap)

What the numbers show:

Disadvantages

Learnings

What I’d do next

Reproduce it: https://github.com/chaitu-19-03/pyo3-hotpath-bench