Fast-Anything: Don't rewrite it in Rust. Rewrite the 5% that's slow
Don’t rewrite it in Rust. Rewrite the 5% that’s slow.
不要用 Rust 重写整个项目。只重写那 5% 缓慢的部分。
Rewriting a whole app in Rust costs months, minute-long compiles and gigabytes of target/. Most of the runtime sits in a few functions anyway.
用 Rust 重写整个应用程序需要耗费数月时间,编译过程长达数分钟,且会占用数 GB 的 target/ 空间。况且,大部分运行时间其实都集中在少数几个函数中。
fast-anything finds those functions, has your coding agent port only them to Rust, proves the port gives the same results on real recorded calls, and swaps it in. One command, fully reversible.
fast-anything 可以找到这些函数,让你的编程助手仅将它们移植到 Rust,通过真实记录的调用验证移植后的结果是否一致,然后进行替换。只需一条命令,且完全可逆。
Ports are compiled with your Rust toolchain (rustup).
移植代码使用你的 Rust 工具链 (rustup) 进行编译。
—agent takes claude, codex, or any command that reads a prompt on stdin. Without it, hand TASK.md to whatever you use.
—agent 参数支持 claude、codex 或任何能从标准输入读取提示词的命令。如果不使用该参数,你可以将 TASK.md 交给你所使用的任何工具。
That port looked right but compared bytes instead of characters. 148 of 500 real calls caught it. On top of the recorded calls, fast-anything derives extra cases and lets the original answer them: edge cases (NaN, ±infinity, -0.0, zero and ±1, non-ASCII text, empty and up to 16× bigger inputs) and shrunk inputs (tiny and random slices). Nothing is enabled until every case matches: same values (floats within 1e-9), errors where the original errors. A port that is right but slower per call than the original (calling into Rust costs more than the work saved) isn’t enabled either. FAST_ANYTHING_DISABLE=1 runs the original any time.
那个移植版本看起来没问题,但它比较的是字节而非字符。在 500 次真实调用中,有 148 次发现了错误。除了记录的调用外,fast-anything 还会衍生出额外的测试用例,并让原始代码给出答案:包括边界情况(NaN、±无穷大、-0.0、零和 ±1、非 ASCII 文本、空输入以及大至 16 倍的输入)和缩减后的输入(微小及随机切片)。只有在所有情况都匹配时才会启用移植版本:数值相同(浮点数误差在 1e-9 以内),且在原始代码报错的地方同样报错。如果移植版本虽然正确但单次调用比原始代码慢(调用 Rust 的开销超过了节省的时间),它也不会被启用。设置 FAST_ANYTHING_DISABLE=1 可随时运行原始代码。
Real runs on an i5-10500, Windows 11 (C and C++: a 4-core GitHub Actions Linux runner). Reproduce them from examples/.
实际运行环境为 i5-10500、Windows 11(C 和 C++ 为 4 核 GitHub Actions Linux 运行器)。你可以从 examples/ 中复现这些结果。
fast-anything ports the outermost loop it can, not the tiny function called inside it, so the app crosses into Rust once instead of millions of times. Porting only the inner levenshtein gave 35× and 1.9×.
fast-anything 会移植它所能找到的最外层循环,而不是循环内部调用的微小函数,这样应用程序只需进入 Rust 一次,而不是数百万次。仅移植内部的 levenshtein 函数分别带来了 35 倍和 1.9 倍的性能提升。
It ports top-level, deterministic functions that take plain data (numbers, strings, lists, dicts, typed arrays…), including ones in your entry script when its main code sits under if name == “main”: (or if FILE == $0, or if (import.meta.main) / if (require.main === module)). In Python that means module-level functions and @staticmethods (dataclasses, NamedTuples and numeric NumPy arrays count as plain data), in Java, C# and PHP static methods (and PHP functions), in Go plain functions, in C and C++ free functions and static methods. scan says why anything else can’t move yet.
它移植的是那些接收普通数据(数字、字符串、列表、字典、类型化数组等)的顶级确定性函数,包括当主代码位于 if name == “main”:(或 if FILE == $0,或 if (import.meta.main) / if (require.main === module))下的入口脚本中的函数。在 Python 中,这意味着模块级函数和 @staticmethods(dataclasses、NamedTuples 和 NumPy 数值数组被视为普通数据);在 Java、C# 和 PHP 中指静态方法(以及 PHP 函数);在 Go 中指普通函数;在 C 和 C++ 中指自由函数和静态方法。scan 命令会说明其他内容为何暂时无法迁移。
With Intel oneAPI on Windows, run fast-anything from a shell where setvars.bat ran (it sets up the linker). SYCL kernels run on the device and aren’t ported; with -fsycl, only host functions are profiled and ported.
在 Windows 上使用 Intel oneAPI 时,请在运行过 setvars.bat 的 shell 中执行 fast-anything(它会配置链接器)。SYCL 内核在设备上运行,不会被移植;使用 -fsycl 时,仅会对宿主函数进行性能分析和移植。
Ruby, Java, Go, PHP, C, C++, C# and R need no headers, bindings or extra tools: the port is a plain Rust library with one C entry point taking and returning JSON, and fast-anything replays the recorded calls against it directly. Any language that can call C can use that bridge.
Ruby、Java、Go、PHP、C、C++、C# 和 R 不需要头文件、绑定或额外工具:移植版本是一个普通的 Rust 库,带有一个接收并返回 JSON 的 C 入口点,fast-anything 会直接针对该库重放记录的调用。任何能够调用 C 的语言都可以使用这个桥接器。
Don’t rewrite it in Rust. Rewrite the 5% that’s slow: profile Python, Node or Ruby apps, port the hot functions with your coding agent, prove they behave the same.
不要用 Rust 重写整个项目。只重写那 5% 缓慢的部分:对 Python、Node 或 Ruby 应用程序进行性能分析,用你的编程助手移植热点函数,并验证它们的行为是否一致。