DEV Community

Cover image for Rust port: 177/177 match, 0.62x slower, 1 crash
frank chu
frank chu

Posted on

Rust port: 177/177 match, 0.62x slower, 1 crash

Rust port: 177/177 match, 0.62x slower, 1 crash

The leaf port matches 177 out of 177 recorded cases, runs slower than the Python it replaces, and enabling it as shipped crashes the app on launch. The tool knows about two of those three problems and says so in its own output. The third one it does not.

Yesterday I ran fast-anything on a fuzzy dedupe script: 10.06 seconds became 129 milliseconds, 78x faster, 34 out of 34 recorded calls matching. The same author shipped the bigger tool a week earlier — rust-anything, created 2026-11-16, 8,502 stars — and its pitch is the whole app, not the 5%: record what the app does, port it one piece at a time, prove each piece against the recording, and end with a pure Rust binary. I ran it on the exact same code to see what changes when the goal is the whole app instead of one hot function.

The setup

Same app as yesterday: 700 names, near-duplicates with typos, collapse within Levenshtein distance 2. Pure standard library, deliberately naive quadratic loop. Yesterday's clean baseline was 10.06s for dedupe(), with levenshtein called 151,315 times.

rust-anything 3.3.0, installed from npm. Its loop is: analyze -> bridge -> record -> port src/port.rs -> build -> diff -> gate, plus an app track (record / front-door / wire / lift) for moving the whole CLI.

go refuses the entry point

My first try was the one-command path, rust-anything go -- python3 dedupe.py. It ran the workload and then failed: no CPU profile was written: is the workload a node process? The profile step wants the repository root passed explicitly. With go . --lang python -- python3 dedupe.py it profiled fine — dedupe at 99% of the run, levenshtein at 98%, 151,315 calls, same shape as yesterday — and then refused:

error: every export of `dedupe.py` must stay in Python: levenshtein (not hot
in the profiled run; `go` ports one function), typo (not hot in the profiled
run; `go` ports one function), make_names (not hot in the profiled run;
`go` ports one function), dedupe (a command-line entry point: it stays in
Python, and `app front-door` takes the command line over when the whole app
moves)
Enter fullscreen mode Exit fullscreen mode

This is the first real difference from fast-anything. The 5% tool ports whatever is hot, entry point or not. The whole-app tool will not port the function that owns __main__ — that belongs to the app track. So I ran the app track instead.

The front door: a Rust binary that runs Python

app record ran the CLI twice and stored 1 case (no-args), noting the timing line differs between runs and wildcarding it. app front-door scaffolded rust/app, a Rust binary with a clap parser that owns the entry point, and every command still runs the original Python underneath. Build and replay: 1/1 cases passed, fallback rate 100%.

That is the honest starting state of a whole-app migration: a Rust binary that is, for now, a launcher for the Python it will replace. The rest of the flow is supposed to drive the fallback rate to zero, one wired unit at a time.

analyze tripped over the tool's own install directory

One detour worth recording: analyze first reported the repo as TypeScript/JavaScript with 0 units. The cause was my own npm install rust-anything — the package.json and node_modules in the same directory made the language detector pick JS. Moving those out of the repo fixed it: the unit is dedupe.py, 69 lines, readiness 89, PyO3 bridge. The risk list it printed was accurate: string offsets and slicing, dynamic typing (3 places), clock and random numbers (make_names uses random.Random).

bridge ports the leaves, not the loop

bridge created rust/dedupe-rs for levenshtein and typo. dedupe and make_names stay in Python — again, entry points. So the unit to port is the two leaf functions, and the 151,315-call loop stays in Python, crossing into Rust once per call. The bridge has a --batch option for exactly this shape ("runs function over many param values kept in Rust"), which the profile output had hinted at, but the default bridge run does not apply it.

record: 177 cases, 143 of them throws

Recorded 177 cases (143 threw) in rust/dedupe-rs/cases.jsonl
  levenshtein: 88
  typo: 89
Enter fullscreen mode Exit fullscreen mode

The recorder feeds generated JSON values as arguments. For levenshtein that produces a genuinely interesting set: 34 cases return a value, 54 throw. The values include things like levenshtein(["a", 1], {}) == 2, levenshtein(null, null) == 0, levenshtein("x", []) == 1 — Python's duck typing doing its thing on lists and dicts. The throws are TypeError: object of type 'int' has no len(), KeyError: 0 (dict indexed by int), and friends.

For typo, all 89 cases threw. Every single one. The reason is structural: typo takes a random.Random object, and the recorder can only pass JSON values. All 89 errors are AttributeError: '<type>' object has no attribute 'choice' — dict, list, str, int, NoneType, bool. Not one recorded case exercises what typo actually does, because the input it needs cannot be expressed in the recording format. The tool's own docs suggest --pin-clock for random-dependent functions, but pinning the clock does not conjure a Random object into a JSON case either.

The port: Python-faithful, error for error

I wrote the port by hand (no agent is configured on this box, so go stops at TASK.md — same as yesterday). levenshtein reproduces CPython's semantics over JSON values: True == 1, len() TypeErrors with the exact message, dict KeyError: 0, "not subscriptable" for numbers and None, strings indexed by code point. typo reproduces the only behavior the recording covers: the AttributeError, with the type name filled in from the argument. That is a faithful port of the recording, and it is worth saying plainly what it is not: a port of typo.

$ rust-anything build rust/dedupe-rs
Built .../rust/dedupe-rs/python/dedupe_rs
$ rust-anything diff rust/dedupe-rs
177/177 cases match
Enter fullscreen mode Exit fullscreen mode

fuzz — fresh inputs from a new seed, the check that is supposed to catch what the recording missed — was thin: 1/1 fresh cases match (1 round), and again 1/1 over two rounds with --cases 200 --time 60. One fresh case per round is not much of a net.

bench: the Rust is slower, and the tool says so

function                  cases orig ns/call Rust ns/call   speedup
levenshtein                  88         1936         3446     0.56x
typo                         89         2349         3408     0.69x

Timed as callers call it, crossing included. Overall 0.62x (geometric mean);
0 of 2 functions at least 1.1x faster.
Do not ship this unit for speed: calling across costs more than the work
these functions do. Port a bigger unit (these functions together with their
callers), so one call does more work.
Enter fullscreen mode Exit fullscreen mode

That is the tool being honest, and it is right — about the recorded cases. On those tiny inputs, mostly garbage, the PyO3 crossing costs more than the work. But the recorded cases are not the real workload. The real workload calls levenshtein 151,315 times on ~15-character names, where Python runs a 225-iteration bytecode loop per call and Rust runs a native one. So I measured the end-to-end run instead, 5 times interleaved, after fixing the crash below:

original (median of 5):        10.21s
Rust levenshtein (median of 5): 2.13s   -> 4.8x faster end to end
Enter fullscreen mode Exit fullscreen mode

Same output both ways — 700 names -> 478 unique, same sample. So the bench (0.56x) and the workload (4.8x) disagree, and both are correct: they measure different inputs. The bench measures the recorded distribution; the workload measures the real one. The lesson is not that the bench lies — it is that a port proved on generated garbage tells you nothing about the calls the app actually makes, in either direction. And 4.8x is a long way from yesterday's 78x, for the reason the tool itself gave: the loop still runs in Python, crossing into Rust 151,315 times. Porting the leaf is worth 4.8x; porting the loop was worth 78x.

enable as shipped crashes the app

$ python3 dedupe.py
Traceback (most recent call last):
  File "dedupe.py", line 53, in make_names
    name = typo(name, rng)
TypeError: unsupported argument type Random
Enter fullscreen mode Exit fullscreen mode

enable rebinds both levenshtein and typo to the Rust module in dedupe.py. The first thing the app does is make_names(), which calls typo(name, rng) with a real random.Random — and a real Random cannot cross the serde_json::Value bridge. The app dies before it reaches dedupe at all. The 177/177 diff never saw this coming, because the recording could not express the call the app actually makes. The kill switch works — RUST_ANYTHING_UNIT_DEDUPE=original runs the original — but a green diff that ships a crash on the first real run is exactly the failure mode the recording is supposed to prevent.

I narrowed the rebind by hand to levenshtein only. With that, the app runs and produces the same output: 700 names -> 478 unique, same sample — and the 4.8x number above.

gate fails, wire refuses, lift never happens

FAIL  fmt        run `cargo fmt`      (cargo-fmt is not installed for this toolchain)
FAIL  clippy     (cargo-clippy is not installed for this toolchain)
pass  cargo test / unsafe / lockfile / ported / size / error cases / panics
pass  build / api / behavior  (177/177 recorded cases match)
Gate failed.
Enter fullscreen mode Exit fullscreen mode

Two of the failures are environmental: this box's Rust toolchain has neither rustfmt nor clippy installed, and gate will not pass without them. The third failure mode I met along the way was recording — after I hand-edited dedupe.py to narrow the rebind, the gate noticed the source had changed since it was recorded and demanded a re-record. That check works as designed, and the re-record produced the identical 177 cases.

But wire requires a green gate, so the unit cannot be wired into the front-door binary, the fallback rate stays at 100%, and lift — the pure-Rust binary the whole pitch is about — is unreachable from here. On this box, the whole-app path ends at a Rust launcher that runs Python, with a leaf port that is slower per call and crashes the app if you enable all of it.

The take

fast-anything and rust-anything are the same idea at two scopes, and the same code exposes the difference. Porting the hot function as one unit, with the loop inside it, gave me 78x yesterday. Porting the leaves, with the loop left in Python crossing the boundary 151,315 times, gives me a bench that says 0.62x and a tool that tells me not to ship it for speed. The recording is the load-bearing part of the promise, and on this app it is thinnest exactly where the app is most real: the one function that takes an object the recorder cannot construct, and the one call path the app actually runs first.

That is not a verdict on the tool — the Prettier and json-server ports in its README are real, measured, and much bigger than my 69-line script. It is a verdict on what its proof covers at small scale: 177/177 is a statement about the recording, and the recording is a statement about JSON-shaped inputs. The app's first real call is not JSON-shaped, and that is where it fell over.

Sources

  • rust-anything, https://github.com/erkamyaman/rust-anything — created 2026-11-16, 8,502 stars, v3.3.0, MIT. README results table (Prettier 19x, json-server) quoted from the repo.
  • fast-anything, https://github.com/EL4CTEO/fast-anything — same author, created 2026-11-21. Yesterday's numbers from my own runs: baseline 10.06s, 129ms median end to end, 78x, 34/34 verification calls matched.
  • All rust-anything numbers are my own runs on 2026-11-23: AMD EPYC VM, Python 3.12.3, rustc 1.100.0, rust-anything 3.3.0 via npm. Profile, record, diff, bench, gate, fuzz and enable outputs quoted verbatim from the tool.

Top comments (0)