Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the faster ryu code for converting double to string #255

Open
johnynek opened this issue May 14, 2020 · 11 comments
Open

use the faster ryu code for converting double to string #255

johnynek opened this issue May 14, 2020 · 11 comments

Comments

@johnynek
Copy link

https://github.com/ulfjack/ryu

@plokhotnyuk
Copy link

Here is an efficient port of this algorithm to Scala.

Feel free to pick and adopt it in your codebase.

@johnynek
Copy link
Author

PS: I copied the java from ryu into my codebase that was writing CSVs (several GBs worth) and the whole end-to-end time got 2x faster, so even in a real system doing other real work, the win is very significant.

@rossabaker
Copy link
Member

Do we do a lot of Double => String in this project? I think this is a wonderful idea, but more applicable on the rendering side, which mostly happens in the downstream libraries. We might get the most benefit implementing this in circe.

That said, it could speed up rendering in jawn-ast.

@johnynek
Copy link
Author

I was thinking of rendering in jawn-ast, I don't know if circe uses that code, but I thought it might. If not, we'd have to do it in both places.

@rossabaker
Copy link
Member

It looks like jar distribution is stuck (ulfjack/ryu#93). It would make this a lot easier for both projects if they could just add a one-liner to the build and a one-liner to the source.

Yes, I realize I'm talking to Scala's foremost advocate of source dependencies. 😄

@plokhotnyuk
Copy link

Have you considered an option to do it in JDK immediately?

@johnynek
Copy link
Author

I think we should just copy the code. That’s what I did.

@johnynek
Copy link
Author

PS: yes it would be good if someone could send this as a PR to the JDK but that would still take years to get deployed and also not help scalajs or native.

@plokhotnyuk
Copy link

plokhotnyuk commented May 15, 2020

Scala.js will not change its implementation of toString, and moreover currently it is impossible to make it run on par with JS implementation in browsers and Node.js.

Scala Native already uses Ryu, but it just a copy of the original Java version from the author of algorithm which is slower than current JDK's toString for whole numbers and numbers with a short mantissa.

You are right that implementation in JDK can take years and almost impossible to back-port it into 8 and 11 versions. But using Long.multiplyHigh intrinsics and internal API of String it could be the most efficient implementation.

Please, peek the latest version of jsoniter-scala and using this implementation of stringify: Double => String function compare it with other implementations for different kind of numbers.

@plokhotnyuk
Copy link

plokhotnyuk commented May 26, 2020

More efficient the Schubfach algorithm is coming to OpenJDK 15 and probably will be ported back to JDK 11. It works almost as Ryū but without rounding loops which affects the speed of serialization for numbers with a short decimal mantissa in latter. Results with the currently proposed patch are quite promising: plokhotnyuk/jsoniter-scala#531

@plokhotnyuk
Copy link

plokhotnyuk commented May 30, 2020

JFYK, the Schubfach way to render doubles (and floats) have been adopted by jsoniter-scala and is available since the v2.2.5 release.

Ironically the best speed up (about 2x times comparing with the previous implementation based on the Ryu algorithm) was got in the Scala.JS version of the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants