var x = (4 * y) + "px";
floating-point constant
float-to-string conversion
floating-point multiplication
string concatenation
Faster Float to String in OTP 24
0.3
0
sign
exponent
mantissa
(-1)^s * (1.mantissa)2 * 2^(exponent - bias)
01101
0011001101
0.300048828125
exactly
0
sign
exponent
mantissa
01101
0011001101
0.300048828125
exactly
0.2999267578125
0.3001708984375
0.3
0.300048828125
0.3?
-
Information preservation: a correct parser must return the original floating point value from the output.
-
Minimum-length output: the output string must be as short as possible.
- Correct rounding: the output string must be as close to the input as possible
Rules
-
Dragon4 (Steele and White, 1990)
- Slow (uses bignums), complete
- Improved by Gay (1990), Burger and Dybvig(1996)
-
Grisu2 (Loitsch, 2010 –intermediate result)
- Incorrect output –don't use!
- Grisu3 (Loitsch, 2010)Fast, incomplete (needs fallback algorithm)
-
Errol3 (Andrysco, Jhala, and Lerner, 2016)
- Moderate speed, complete
-
Ryu (Ulf Adams, 2018)
- Fastest, complete
Solutions
Most libc and stdlib
SUPER FAST
-
Float_to_*/1,2 BIFs
- Do not do shortest, only %f, %e
- Use libc sprintf and not interested in changing
- io_lib_format:fwrite_g/1
- Private API for io_lib:format, shell, pretty printer
-
But used! Poison, Jason, jsx,
- DO NOT USE JSONE, it is wrong there
- Implemented in Erlang!
- Easy to change !
- PR 2960, funded by the EEF
On the BEAM ?
Sources
-
Stephan T. Lavavej, “Floating-Point <charconv>: Making Your Code 10x Faster With C++17's Final Boss”
-
Ulf Adams, "Ryū: Fast Float-to-String Conversion"
- https://github.com/ulfjack/ryu
- https://github.com/erlang/otp/pull/2960
-
C to Erlang is not obvious and not pretty
-
Hopefully the BIF version will help clean this up
-
-
Special case for (small) integers
-
Tests tests tests
-
The erlang version was old and probably written at the same time as the tests
-
The tests did not fail properly in case of failure, the failure was swallowed
-
Saved by compiler test suite....
-
Hopefully better now, we added a lot of tests
-
-
OTP build and tests systems are really really not user friendly
Bonus 1
The Pain points
3000488281250.
2999267578125.
3001708984375.
Bonus 2
3000488281250
2999267578125
3001708984375
Bonus 2
300
299
300
Bonus 2
0.3
Bonus 2
Faster Float to String in OTP 24
By di4nao
Faster Float to String in OTP 24
Erlang Paris March 21
- 582