Skip to content

Code Lab

Fast-Anything: How Recorded Python Calls Check a Rust Port

By Miles Park·
Fast-Anything project preview from GitHub

CODE LAB · PROJECT SPOTLIGHT

Fast-Anything Rust and Python · MIT

Profile a hot function and verify its Rust port against recorded calls.

Project preview · EL4CTEO/fast-anything

Moving one slow Python function into Rust only helps if callers keep getting the right answer. Fast-Anything profiles a program, records inputs and outputs for a selected function, and uses those calls to check a candidate port. The maintainers report a 116× whole-program improvement for their Python fuzzy-deduplication example with 700 names. The number belongs to that workload and their test environment.

Fast-Anything maintainer demo shows a Rust port failing 148 of 500 recorded comparisons before a fix
In the maintainer’s demo, the first Rust implementation counts bytes where Python counts characters. The corrected implementation passes the shown replay. Maintainer demo and benchmark context.

Check the outcome of each recorded call

The excerpt is from the Python replay runner. It separates cases that should raise an error from cases that should return a value. A returned value must match the reference under the project’s comparison rules. This is the test that catches a port which successfully builds and runs but computes something different.

EL4CTEO/fast-anythingPython · source excerpt
PY runner.py
            raise        except BaseException as e:  # Rust panics arrive as PanicException (a BaseException)            got, err = None, e        if "raises" in case:            ok, expected = raises_like(err, case["raises"]), "raises " + case["raises"]        else:            want = dec(case["result"])            ok, expected = err is None and same(want, got), show(want)        if ok:            report["passed"] += 1            continue        report["failed"] += 1
Lines 1118–1129 · commit d9b39f5View original on GitHub ↗

Run a small comparison before porting anything

Save the pinned runner.py beside the snippet and run it with Python 3. The candidate in this exercise counts UTF-8 bytes. The original counts Python string characters. The third call checks a change in return type.

from runpy import run_path

same = run_path("runner.py")["same"]
text = "café"
print("ASCII length:", same(len("cafe"), len("cafe".encode())))
print("Unicode length:", same(len(text), len(text.encode())))
print("Return type:", same(3, 3.0))

Observed output

ASCII length: True
Unicode length: False
Return type: False

ASCII gives the same count by either method. With café, the four characters occupy five bytes, so the comparison fails. Python normally considers 3 == 3.0 true, but the project’s comparator flags the type change. This ran the upstream comparator directly. No Rust port, agent invocation or performance measurement was part of our run.

Use the replay result to decide what to time

Start with one representative workload and additional cases you can inspect before enabling a port. Then compare wall-clock times for the same complete program. A faster individual call can still leave startup, file reads or repeated runtime crossings as the dominant cost. Recordings can include real call arguments. Use a disposable sample dataset when trying the workflow with a coding agent.

Mindung’s introduction · Another example of testing a small part of a larger program

Source excerpt © 2026 EL4CTEO. MIT license.

Join the discussion

Your email address will not be published. Required fields are marked *