Zig gets a faster float parser and formatter based on new algorithms

A pull request to the Zig standard library replaces its floating-point formatting and parsing with a new implementation that its author reports is faster in most cases.

Zig gets a faster float parser and formatter based on new algorithms
Source: Codeberg.org

The change, submitted to the Zig project on Codeberg, adds a new floating-point formatting and parsing algorithm to std.fmt.float. The implementation follows techniques described by Russ Cox, and its author, tiehuis, extended them to support arbitrary float types beyond the standard f64 variant. The author says it is designed to be API-compatible with the existing code.

According to the benchmarks in the pull request, the new code formats floats roughly twice as fast as Ryu and is 10 to 20 percent faster than Eisel-Lemire. Compared with a Zig implementation of zmij, a formatter generally regarded as very fast, the speeds were about the same. For large floats above f64, the parse path is also reported to be much faster than the previous big integer fallback, which the author describes as a serious performance regression.

The update also adds full-precision output, so values can be printed with many more digits, and a case option for special values such as inf and nan. The author tested the code against a reference implementation, exhaustively for f16 and f32, and with fuzzing for f64, f80 and f128. The pull request also lists several fixes for previously reported issues.

During continuous integration, a build error appeared on targets with 128-bit long double types using musl. The author planned to compute the constant without the builtin that caused it. The Zig team also asked for a write-up of the work for its development log.

Source: Codeberg.org


Posted

in

by

Tags: