Zig PR proposes new float formatter and parser, said to be ~2x faster than ryu

Pull request #37793 in the Zig repository on Codeberg, titled "std.fmt.float: new floating-point formatting/parsing algorithm", proposes replacing the current float formatting and parsing code in the standard library. The author, tiehuis, implements the algorithms described at research.swtch.com/fp and calls the result "fp". He extended them from the f64 variant provided there to arbitrary float types, and says the code should be API-compatible, asking to be told if he missed anything. The history of the implementation lives in his zig-fp repository, which also holds more detailed measurements.
On speed, the author says fp is generally about 2x faster than ryu for formatting and 10-20% faster than eisel-lemire for parsing. He also compared it with a Zig implementation of zmij, which he describes as generally considered the fastest formatting implementation, and found fp to be generally about the same speed. He prefers fp to zmij because he finds its primitives more adaptable, and because the same primitives serve both formatting and parsing. As before, there are two backing engines: floats of 64 bits or smaller use a 64-bit engine, and larger ones use a 128-bit engine.
The benchmarks ran on an AMD Ryzen 5 5600X, built with ReleaseFast, seed 0. For shortest-mode f64 output (scientific, 10000 iterations), fp took 14.49ns to format and 16.50ns to parse. The current std took 28.64ns and 19.09ns, and zmij took 16.51ns to format (no parse figure). With fixed precision 10 on f64, fp took 14.34ns to format and 14.02ns to parse, against 31.76ns and 16.93ns for std.
The biggest gap is for floats larger than f64. Std always parsed these through its slow big-int fallback, which the author calls a serious performance regression. fp adds a faster parse path. For f128 (1 iteration, 1000 samples), fp took 149.48ns to format and 120.47ns to parse, while std took 220.73ns to format and 1117267.61ns to parse, as printed by the benchmark. The PR also has a binary-size section for the f64 and f128 engines. It notes that the f64 engine uses compact tables only for small optimization modes and the f128 engine always does.
Two features are new. Full-precision output now works: formatting 123.1 as an f64 with 50 decimals gives 123.10000000000000000000000000000000000000000000000000 in std, but 123.09999999999999431565811391919851303100585937500000 in fp, which is the exact binary value. This relies on a new representation of the fallback big-integer library. The author looked at table-driven approaches such as floff, but found the performance gain generally marginal and preferred the simpler approach. Tables work well for f64 but are still costly for f128, and the big-int code can be reused for fallback parsing. Case is also now an option when formatting; for now it applies only to special values such as inf and nan, and the author notes it could be extended to exponent specifiers.
The PR lists cases where the old output was wrong or suboptimal. 1.00001e5 should have rounded down to even. 0x0.0p0 should not print an unnecessary decimal part. 0x0.004p-14 printed an unnormalized form, which is not strictly wrong but worse for fixed-precision output. 0.01563 should have rounded down to even. 18014400656965632 was zero-padded in std when the exact value is ...632 (2^54 + 2^31), though both round-trip. 3.1415 is not exact, since its exact value is 3.14149996, so it should round down rather than to even. Non-hex float output now also supports upper-case inf and nan, and stage3 now builds.
The PR says it fixes issues #20619, #22779, #37103, #37379, #36208 and #37789. Issue #23942 is still open, and the author calls exact rounding for whole values a simple follow-up.
For testing, the author points to his fp-testcases repository and programs in zig-fp that run against it. fp was fuzz-tested against the reference uscale C implementation, f16 and f32 were exhaustively tested against std, and f64, f80 and f128 were fuzz-tested against std. He notes that std was originally fuzz-tested against glibc, and that he would expect external testing against a wider range of data for any major change anyway. He also rewrote the old tests, which he found inconsistent and often testing the same thing several ways.
In review, a commenter did not like an existing alias and preferred users call float.parse directly; the author agreed and said it could be marked deprecated in a follow-up. Another remark preferred the name format over render, and the author again said API changes could come in a follow-up. Someone suggested using digits2 from std.fmt. A CI error (a float Inf stored in a comptime_int, from @log10 on floatMax) showed up only on targets with a 128-bit c_longdouble using musl; the author will compute the constant without the builtin, and a reviewer asked that the workaround carry a comment linking a relevant issue or patch. Finally, andrewrk asked whether he could copy the author's writeup into a devlog on ziglang.org/devlog/2026/, or whether the author would write one. The author said he is happy to write something over the weekend.
Key facts
- PR #37793 to Zig's std.fmt.float implements the algorithms from research.swtch.com/fp, called fp, extended by author tiehuis to arbitrary float types.
- The author says fp is generally ~2x faster than ryu for formatting and 10-20% faster than eisel-lemire for parsing, and about the same speed as zmij.
- On a Ryzen 5 5600X, shortest-mode f64 formatting took 14.49ns with fp against 28.64ns for current std; f128 parsing took 120.47ns against 1117267.61ns, as printed.
- New features: full-precision output and a case option for inf and nan; the PR lists several wrongly rounded outputs in the old code and says it fixes six linked issues.
- Testing includes fuzzing against the uscale C reference and exhaustive f16 and f32 checks against std; andrewrk asked to turn the writeup into a devlog.
Why it matters
Float printing and parsing sit under almost every program that handles numbers. This PR would replace Zig's standard-library implementation with one that the author says roughly halves format time against ryu and does about as well as zmij, the implementation he calls generally considered the fastest. It also repairs a gap: floats larger than f64 were always parsed through the slow big-int fallback, and the f128 benchmark shows the difference. It also corrects several outputs where the old code rounded the wrong way, and it adds full-precision printing.
Who it affects
Zig users who format or parse floats through std.fmt, especially anyone working with f80 or f128 values, where parsing was slow. Maintainers of the standard library are affected too: the PR touches the API surface only lightly, with renames and deprecations deferred to follow-ups, and andrewrk wants the work written up as a devlog on ziglang.org.
How to use it
Nothing to install yet. The implementation and its history are in the author's zig-fp repository, and the PR's benchmarks run from there with commands such as zig build perf -Doptimize=ReleaseFast -- --seed=0 --zmij=true, with --precision=10 or --type=f128 for the other cases. Test data is in the fp-testcases repository. Once merged, full-precision output would work through the format string, for example {d.50}. The case option currently affects only inf and nan.
How solid is it
The primary source is the PR itself, with benchmark output pasted in. The 2x and 10-20% figures are the author's own "generally" summaries; the PR shows no table for ryu or eisel-lemire, only the fp, std and zmij runs. All runs used one CPU (Ryzen 5 5600X) and one seed. Testing is broad: fuzzing against the uscale reference, exhaustive f16 and f32 checks against std, fuzzing for f64, f80 and f128. The source does not say whether the PR has been merged or is still open, and it states no formal approval or rejection of the design by a Zig maintainer.
Risks and caveats
The author hedges on API compatibility ("should be") and asks to be told about anything missed. f64, f80 and f128 were fuzz-tested rather than exhaustively tested. A CI failure on musl targets with a 128-bit c_longdouble was still to be fixed by computing a constant without the builtin. Issue #23942 remains open after this change. The 1117267.61ns std f128 parse figure is as printed by the benchmark, and the source explains it only as the slow big-int fallback. The author also chose a simpler big-int approach over table-driven techniques such as floff, accepting what he describes as a marginal performance difference.
“Generally, fp is ~2x faster than ryu for formatting, and 10-20% faster than eisel-lemire.”
— tiehuis, author of Zig PR #37793