Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 5 min read

Rewrite 5% in Rust or the whole app? One number in your profile decides

I profiled the demo app that ships with fast-anything, the tool behind this weekend's "Don't rewrite it in Rust" thread. One function took 99.95% of the run. It called an edit-distance helper 244,650 times. That number

I profiled the demo app that ships with fast-anything, the tool behind this weekend's "Don't rewrite it in Rust" thread.

One function took 99.95% of the run. It called an edit-distance helper 244,650 times.

That number is the whole story of the 116x speedup in its README, and it's the number you should look for in your own app before you pick a side.

Two taglines, one engine

fast-anything hit the Hacker News front page this weekend (134 points, 96 comments) and collected about 1,500 GitHub stars in its first day. Its pitch: profile your Python, Node or Ruby app, have your coding agent port only the hottest function to Rust, prove the port matches real recorded calls, swap it in.

rust-anything went up five days earlier and sits near 7,900 stars. Its pitch sounds like the opposite: rewrite the whole app in Rust, one unit at a time, each one checked against what the original did, until app lift leaves a pure Rust binary.

Read the code and they're closer than the taglines. Both record the original's behavior, both have an agent (or you) write the Rust, both replay the recording before anything goes live, and both keep an off switch. rust-anything has profiled hot paths since its first commits on Nov 16. It shipped a one-command go for "only the slow part" at 20:38 UTC on Nov 21, about six hours after fast-anything's first commit. Both demo it on a fuzzy name dedupe in Python.

So the choice isn't Rust or no Rust. It's where your time actually goes.

What I ran

I copied fast-anything's Python example (700 names, every pair compared by edit distance) into a scratch folder and ran Python's own profiler on it (output trimmed):

$ python3 -m cProfile -s cumulative app.py
700 names, 822 likely duplicate pairs
   ncalls  tottime  cumtime  filename:lineno(function)
        1    0.048   13.287  fuzzy.py:21(find_duplicates)
   244650    8.331   13.238  fuzzy.py:4(levenshtein)
 32733077    2.529    2.529  {built-in method builtins.min}

13.287 of 13.293 seconds sit under find_duplicates. fast-anything's scanner reads the same kind of profile (it runs cProfile under the hood for Python) and ranks functions by share of the run times how portable they are, preferring the outermost one so the app crosses into Rust once, not 244,650 times.

I don't have a Rust toolchain on this machine and didn't install one, so I didn't build or verify a port. I did check how much of the 116x needs Rust at all. The repo's reference port does two clever things besides being Rust: it skips pairs whose lengths differ by more than the limit, and it trims shared prefixes and suffixes before the edit-distance loop. It also spreads the rows over every core. I wrote those same tricks in plain Python and timed them, best of three:

original      822 pairs   3.620s
length skip   822 pairs   2.668s   1.4x
skip + trim   822 pairs   2.472s   1.5x
10 processes  822 pairs   0.434s   8.3x

Same 822 pairs every time, on an M1 Max. Algorithm tweaks buy 1.5x; all ten cores get plain Python to 8.3x. The README's 116x was measured on a different machine, so this is a loose comparison, but the gap that's left is real and it's Rust's. For this app, porting the hot function is the right call.

The number that decides

Amdahl's law, which tialaramex raised in the HN thread, is the whole decision. If the function you port takes a share s of the run and gets k times faster, the app gets 1 / ((1 - s) + s / k) faster. Give the port a generous 50x:

Hot function's share Whole-app speedup
99.95% (the demo) about 49x
90% 8.5x
50% 2x
10% 1.1x
2% 1.02x

That last row isn't hypothetical. fast-anything's cutoff for "worth porting" is 2% of the run. A port that clears the bar can still buy you two percent.

ThouYS put the counterpoint in the same thread: in their experience the real cost is spread "like sand" across the system. If that's your profile, no single-function port will save you, however good it is.

When the 5% fits

Your codebase is a fast-anything (or rust-anything go) candidate when the profile has one tall spike and the function under it is pure. fast-anything's Python scanner refuses, by name, anything that's a method on a class, async, a generator, does I/O, writes global state or isn't deterministic. It also warns when the function already leans on NumPy, since there's less to win.

Read that list against a typical web app. Your view code is methods. Your hot paths wait on a database. Batch jobs, parsers, scoring and matching loops, report generators, data cleanup scripts: that's where a single pure function owns most of the clock.

When the whole app fits

Some costs live in the runtime itself, and no function port touches them. rust-anything's json-server case is the clearest one: idle memory went from 74 MB to 3.5 MB and start to first response from about 80 ms to about 9 ms, as one 5.3 MB binary. If you ship a CLI people start a hundred times a day, or a service you pay for by the megabyte, that's the trade you want. The project's ffmpeg experiment makes the other case: against C the gain is memory safety, at roughly C's speed.

The price is in its own docs. json-server took nine units, 514 recorded HTTP cases, two adapters and a hand-ported command line, and the hard parts were quirks nobody wrote down, like the router re-running routes after every next(). A recording is also the ceiling: inputs nobody recorded, regex behavior on unusual text, timing and memory under load all pass silently. And record runs your code many times, so the README tells you to use a copy with no production credentials around.

Rough edges

fast-anything is two days old, at version 0.7.0 after 56 commits, and one HN commenter flagged it as AI-generated. Its Java result counts JVM startup, which the thread called out. And the repo's reference port, the one its CI uses, runs on every core, so a number like 116x compares one Python core with all of them. Keep that in mind when you read the results table.

How I'd choose

  1. Profile a real workload, not the test suite.
  2. Find the top function you could actually port and read its share.
  3. Most of the run in one pure function: port that function. Either tool will do it.
  4. A flat profile, but you're paying in memory, startup time or deploys: that's the whole-app case.
  5. A flat profile and the time is in the database: neither tool. Fix the queries.

You can do step 2 right now, no install:

python3 -m cProfile -s cumulative your_app.py | head -25

What share of your app's runtime does its single hottest function take, and did you measure it before the last time someone said "rewrite it in Rust"?

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.