Live data from Hacker News

Ryū: Fast Float-To-String Conversion

pldi18.sigplan.org

31–40 of 45 posts

Re: Ryū: Fast Float-To-String Conversion

#31

Does anyone know of a good decimal float to string converter, preferably in c?

sprintf

Then main drawbacks of sprintf are that it doesn't allow to find the shortest decimal representation and that its output depends on the current locale, so you often need something like sprintf_l which isn't portable.

Re: Ryū: Fast Float-To-String Conversion

#32

Does anyone know of a good decimal float to string converter, preferably in c?

C code for Ryu is available under the Apache2 and Boost licenses: https://github.com/ulfjack/ryu/tree/master/ryu

Also have a look at Grisu3, Dragon4, dtoa, and the printf implementations of glibc and musl.

Re: Ryū: Fast Float-To-String Conversion

#33
post #7

(De-)Serialization being such a common task both in Servers and clients nowadays I wonder if special instructions would make sense in CPUs

https://stackoverflow.com/questions/50966676/why-do-arm-chip...

(not quite the same thing but heading in that direction)

Re: Ryū: Fast Float-To-String Conversion

#34

Earlier quoted context omitted.

sprintf

Then main drawbacks of sprintf are that it doesn't allow to find the shortest decimal representation and that its output depends on the current locale, so you often need something like sprintf_l which isn't portable.

Why is it not portable?

Re: Ryū: Fast Float-To-String Conversion

#35
post #26

Earlier quoted context omitted.

At that point just base-64 for your IEEE floats.... Because anywhere where you need that speed you can better handle it without using strings...

But that's not necessarily interoperable. E.g., some JSON parsers might produce IEEE754 doubles, but others might use arbitrary precision representations -- what then? One answer is to make JSON support only IEEE754 doubles, but it's a bit late for that, so at most one can recommend that implementors stick to IEEE754. If you are starting from scratch, and want to support only platforms that support IEEE754 in hardwar…

You've just given me the idea that interop should be done with "hexponential" notation: 0x123ABC*0x10^0xDEF can be an exact representation of a floating point number.

Re: Ryū: Fast Float-To-String Conversion

#36

Does anyone know of a good decimal float to string converter, preferably in c?

C code for Ryu is available under the Apache2 and Boost licenses: https://github.com/ulfjack/ryu/tree/master/ryu Also have a look at Grisu3, Dragon4, dtoa, and the printf implementations of glibc and musl.

The two fastest options that I'm currently aware of, by a pretty wide margin, are Ryu and Swift's implementation (single C file, it's only in a C++ file to satisfy a build system quirk in Swift on linux: https://github.com/apple/swift/blob/master/stdlib/public/run..., Apache2 + run time exception).

Ryu is somewhat faster for doubles, though SwiftDtoa has support for 80-bit and float out of the box, which is nice, and we have some further perf improvements planned. Both are considerably faster than any of the alternatives (some perf data in the original Swift PR: https://github.com/apple/swift/pull/15474), and both pass our fairly intensive test suite.

musl's implementation is also interesting for reasons of simplicity.

Re: Ryū: Fast Float-To-String Conversion

#37
post #35

Earlier quoted context omitted.

But that's not necessarily interoperable. E.g., some JSON parsers might produce IEEE754 doubles, but others might use arbitrary precision representations -- what then? One answer is to make JSON support only IEEE754 doubles, but it's a bit late for that, so at most one can recommend that implementors stick to IEEE754. If you are starting from scratch, and want to support only platforms that support IEEE754 in hardwar…

You've just given me the idea that interop should be done with "hexponential" notation: 0x123ABC*0x10^0xDEF can be an exact representation of a floating point number.

That’s basically hexadecimal floating point strings, which are in C since C99 via the %a format specifier (they use a base-two exponent written in decimal instead of a base-16 exponent in hex, but that doesn’t really change the complexity of parsing or formatting.)

Re: Ryū: Fast Float-To-String Conversion

#39
post #2

The code is here: https://github.com/ulfjack/ryu

Jacob Quinn has a translation to Julia here: https://github.com/quinnj/Ryu.jl

I really enjoy seeing Julia devs so invested and up-to-date with projects and experiments like this, because isn't this the bread and butter of technical computing? The nitty gritty of making all these bits and bytes do work! So cool.

Is Ryu.jl significantly faster than what's in Base? Is there already an open issue somewhere?

Re: Ryū: Fast Float-To-String Conversion

#40
post #39

Earlier quoted context omitted.

Jacob Quinn has a translation to Julia here: https://github.com/quinnj/Ryu.jl

I really enjoy seeing Julia devs so invested and up-to-date with projects and experiments like this, because isn't this the bread and butter of technical computing? The nitty gritty of making all these bits and bytes do work! So cool. Is Ryu.jl significantly faster than what's in Base? Is there already an open issue somewhere?

Yes, I think preliminary tests showed that it was faster than Grisu (which is what we're using in Base currently). It also seems significantly simpler (Grisu is generally fairly simple but has a custom BigInt implementation to handle some corner cases, which makes things complicated). Not a good time right now to swap the float print algorithm, but a good project for post-1.0.

I don't see an issue about it at the moment. I guess we just expect Jacob to PR it at some point. IIRC he also wrote the Grisu implementation that's in Base.

Post reply on HN