fast-anything 實測:作者將 Python 去重程式的一個函數改寫為 Rust,端到端快 78 倍
78 faster: my Python dedupe, 5% rewritten in Rust
作者以 fast-anything 分析 Python 模糊去重程式,再手動將 dedupe 函數改寫為 Rust,端到端耗時由 10.06 秒降至 129 毫秒,快 78 倍。Rust 版本通過 34/34 個等價性測試,但驗證只涵蓋已記錄及衍生案例,未涵蓋的程式分支仍可能出錯;工具生成的 Rust 樣板亦需手動修正才能編譯。
78× faster: my Python dedupe, 5% rewritten in Rust
10.06 seconds became 129 milliseconds. I changed exactly one function — 48 lines of Rust, zero changes to the rest of the app — and the tool that did it was released yesterday. It found the hot function, recorded its real calls, generated the port task, and then proved the port gives identical results on 34 test cases before it would let me switch it on.
The tool is fast-anything. It was created on 2026-11-21, has 1,845 stars already, and its pitch is the one every Python developer has heard and ignored: don't rewrite it in Rust, rewrite the 5% that's slow. I ran it on my own code to see how much of that pitch survives contact with a real machine.
The app
A fuzzy dedupe: 700 names, some with typos, collapse near-duplicates within Levenshtein distance 2. Pure standard library, deliberately naive — the quadratic pairwise loop is the whole point, because that's what real "it's just a script" code looks like:
def levenshtein(a, b):
if a == b:
return 0
la, lb = len(a), len(b)
if la == 0:
return lb
if lb == 0:
return la
prev = list(range(lb + 1))
for i in range(1, la + 1):
cur = [i] + [0] * lb
ca = a[i - 1]
for j in range(1, lb + 1):
cur[j] = min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (ca != b[j - 1]))
prev = cur
return prev[lb]
def dedupe(names, threshold=2):
kept = []
for name in names:
dup = False
for k in kept:
if levenshtein(name, k) <= threshold:
dup = True
break
if not dup:
kept.append(name)
return kept
Baseline, before touching anything:
700 names -> 478 unique in 10.066s
Scan: it found the 5% in one pass
$ fast-anything scan -- python3 dedupe.py
# function time calls verdict
1 dedupe.py:dedupe █████████ 99.9% 1 ✓ port it
2 dedupe.py:typo 0.0% 226 · too small to matter
3 dedupe.py:levenshtein █████████ 99.5% 151,315 ↳ ported inside dedupe
4 dedupe.py:make_names 0.0% 1 ✗ not deterministic: random.Random
Two things worth noting. First, the 5% claim undersells it here: dedupe is 99.9% of runtime, and it drags levenshtein along — 151,315 calls. Second, the profiler is honest about what it can't port: make_names uses random.Random, so it's flagged non-deterministic and left alone. A tool that tells you what it won't do is a tool I trust more.
Port: the tool writes the spec, I wrote the Rust
fast-anything port re-ran the app, recorded the one real call to dedupe, and generated a crate plus a TASK.md containing the original Python, the helper, the recorded call, and rules: same result on every input including edge cases, Python strings index by code point so use .chars(), floats within 1e-9, raise where Python raises.
Normally you'd hand TASK.md to Claude Code or Codex with --agent. This box has neither configured for the job (the doctor check found codex-cli but no Claude Code), so I wrote the Rust by hand — two-row DP, Vec<char> per string, same early exits as the original:
fn levenshtein(a: &[char], b: &[char]) -> usize {
if a == b { return 0; }
let (la, lb) = (a.len(), b.len());
if la == 0 { return lb; }
if lb == 0 { return la; }
let mut prev: Vec<usize> = (0..=lb).collect();
let mut cur: Vec<usize> = vec![0; lb + 1];
for i in 1..=la {
cur[0] = i;
let ca = a[i - 1];
for j in 1..=lb {
let cost = if ca == b[j - 1] { 0 } else { 1 };
cur[j] = (prev[j] + 1).min(cur[j - 1] + 1).min(prev[j - 1] + cost);
}
std::mem::swap(&mut prev, &mut cur);
}
prev[lb]
}
And here is the one genuine wart: the generated stub didn't compile. The template emits the function with &Bound<'_, PyAny> parameters and a signature default of threshold = 2 — and PyO3 can't turn the integer literal 2 into a &Bound. The fix was mine, not the tool's: change the parameters to concrete types (Vec<String>, i64) and let PyO3 do the conversion. One line of real friction in an otherwise smooth loop, and exactly the kind of thing that would have cost a first-time user half an hour of staring at a macro error. The tool's verify step can't catch a bug in its own template, because verify only runs after the crate compiles.
Verify: 34/34, then the real number
$ fast-anything verify dedupe.py:dedupe
✓ 34/34 calls give the same result (1 recorded + 8 edge cases + 25 derived)
✓ dedupe.py:dedupe per call: 10069.1ms in Python, 81.8ms in Rust: 123× faster
34 cases: the 1 recorded call, 8 edge cases (empty input, non-ASCII, very long input, and the rest), and 25 derived cases the tool generates by shrinking and mutating the recorded input. That's the part of the pitch that matters most to me. A speedup you can't trust is a bug with good marketing, and the replay step is what separates this from "I rewrote it in Rust and it seemed fine."
Then the whole-app bench, 5 interleaved runs each:
Python ████████████████████████ 10.06s
Rust ▎ 129ms
✓ python3 dedupe.py is 78× faster end to end (median of 5, ±81ms)
Same output both ways — 700 names -> 478 unique — with only the timing line differing, which the tool itself flagged and correctly dismissed as run-to-run variation.
How it compares to the README's claims
The README reports its own runs on an i5-10500: Python fuzzy dedupe (700 names) 4.91s → 42ms, 116×. Mine: 10.06s → 129ms, 78×. My baseline is slower — this VM's Python is slower than their Windows box, and my Levenshtein is the naive textbook version — and my Rust result is slower too. The shape holds, the absolute numbers don't transfer, and that's the honest way to read any benchmark table: same workload class, different machine, different constant factors.
What I'd actually trust this with
The loop works: profile, port one function, prove equivalence on recorded plus derived calls, swap it in reversibly (FAST_ANYTHING_DISABLE=1 runs the original any time). The verification is only as strong as the recorded behavior — if your function has a branch the recorded calls never hit, the port can be wrong there and nothing in this loop will notice. That's the real limit, and it's a limit of the method, not this implementation.
But for the job it advertises — a hot, deterministic, plain-data function eating 99% of a script's runtime — it did the whole thing in about ten minutes, including the stub bug I had to fix by hand. The 5% rule works when the 5% is really 99.9% of the runtime. The profiler will tell you whether that's true before you write a line of Rust, which is the part I'd keep even if I threw the rest away.
Sources
- fast-anything, https://github.com/EL4CTEO/fast-anything — created 2026-11-21, 1,845 stars, v0.7.0, MIT. README results table quoted from the repo.
- All timings are my own runs on 2026-11-22: AMD EPYC VM, Python 3.12.3, rustc 1.100.0 (installed via rustup for this test), fast-anything 0.7.0 via npm. Baseline
python3 dedupe.py: 10.066s; afterfast-anything enable: 129ms median end to end.
來源:DEV Community · dev.to