| 1 | ## fast_float number parsing library: 4x faster than strtod |
| 2 | |
| 3 | The fast_float library provides fast header-only implementations for the C++ from_chars |
| 4 | functions for `float` and `double` types. These functions convert ASCII strings representing |
| 5 | decimal values (e.g., `1.3e10`) into binary types. We provide exact rounding (including |
| 6 | round to even). In our experience, these `fast_float` functions many times faster than comparable number-parsing functions from existing C++ standard libraries. |
| 7 | |
| 8 | Specifically, `fast_float` provides the following two functions with a C++17-like syntax (the library itself only requires C++11): |
| 9 | |
| 10 | ```C++ |
| 11 | from_chars_result from_chars(const char* first, const char* last, float& value, ...); |
| 12 | from_chars_result from_chars(const char* first, const char* last, double& value, ...); |
| 13 | ``` |
| 14 | |
| 15 | The return type (`from_chars_result`) is defined as the struct: |
| 16 | ```C++ |
| 17 | struct from_chars_result { |
| 18 | const char* ptr; |
| 19 | std::errc ec; |
| 20 | }; |
| 21 | ``` |
| 22 | |
| 23 | It parses the character sequence [first,last) for a number. It parses floating-point numbers expecting |
| 24 | a locale-independent format equivalent to the C++17 from_chars function. |
| 25 | The resulting floating-point value is the closest floating-point values (using either float or double), |
| 26 | using the "round to even" convention for values that would otherwise fall right in-between two values. |
| 27 | That is, we provide exact parsing according to the IEEE standard. |
| 28 | |
| 29 | |
| 30 | Given a successful parse, the pointer (`ptr`) in the returned value is set to point right after the |
| 31 | parsed number, and the `value` referenced is set to the parsed value. In case of error, the returned |
| 32 | `ec` contains a representative error, otherwise the default (`std::errc()`) value is stored. |
| 33 | |
| 34 | The implementation does not throw and does not allocate memory (e.g., with `new` or `malloc`). |
| 35 | |
| 36 | It will parse infinity and nan values. |
| 37 | |
| 38 | Example: |
| 39 | |
| 40 | ``` C++ |
| 41 | #include "fast_float/fast_float.h" |
| 42 | #include <iostream> |
| 43 | |
| 44 | int main() { |
| 45 | const std::string input = "3.1416 xyz "; |
| 46 | double result; |
| 47 | auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result); |
| 48 | if(answer.ec != std::errc()) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; } |
| 49 | std::cout << "parsed the number " << result << std::endl; |
| 50 | return EXIT_SUCCESS; |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | |
| 55 | Like the C++17 standard, the `fast_float::from_chars` functions take an optional last argument of |
| 56 | the type `fast_float::chars_format`. It is a bitset value: we check whether |
| 57 | `fmt & fast_float::chars_format::fixed` and `fmt & fast_float::chars_format::scientific` are set |
| 58 | to determine whether we allow the fixed point and scientific notation respectively. |
| 59 | The default is `fast_float::chars_format::general` which allows both `fixed` and `scientific`. |
| 60 | |
| 61 | The library seeks to follow the C++17 (see [20.19.3](http://eel.is/c++draft/charconv.from.chars).(7.1)) specification. |
| 62 | * The `from_chars` function does not skip leading white-space characters. |
| 63 | * [A leading `+` sign](https://en.cppreference.com/w/cpp/utility/from_chars) is forbidden. |
| 64 | * It is generally impossible to represent a decimal value exactly as binary floating-point number (`float` and `double` types). We seek the nearest value. We round to an even mantissa when we are in-between two binary floating-point numbers. |
| 65 | |
| 66 | Furthermore, we have the following restrictions: |
| 67 | * We only support `float` and `double` types at this time. |
| 68 | * We only support the decimal format: we do not support hexadecimal strings. |
| 69 | * For values that are either very large or very small (e.g., `1e9999`), we represent it using the infinity or negative infinity value. |
| 70 | |
| 71 | We support Visual Studio, macOS, Linux, freeBSD. We support big and little endian. We support 32-bit and 64-bit systems. |
| 72 | |
| 73 | We assume that the rounding mode is set to nearest (`std::fegetround() == FE_TONEAREST`). |
| 74 | |
| 75 | ## Using commas as decimal separator |
| 76 | |
| 77 | |
| 78 | The C++ standard stipulate that `from_chars` has to be locale-independent. In |
| 79 | particular, the decimal separator has to be the period (`.`). However, |
| 80 | some users still want to use the `fast_float` library with in a locale-dependent |
| 81 | manner. Using a separate function called `from_chars_advanced`, we allow the users |
| 82 | to pass a `parse_options` instance which contains a custom decimal separator (e.g., |
| 83 | the comma). You may use it as follows. |
| 84 | |
| 85 | ```C++ |
| 86 | #include "fast_float/fast_float.h" |
| 87 | #include <iostream> |
| 88 | |
| 89 | int main() { |
| 90 | const std::string input = "3,1416 xyz "; |
| 91 | double result; |
| 92 | fast_float::parse_options options{fast_float::chars_format::general, ','}; |
| 93 | auto answer = fast_float::from_chars_advanced(input.data(), input.data()+input.size(), result, options); |
| 94 | if((answer.ec != std::errc()) || ((result != 3.1416))) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; } |
| 95 | std::cout << "parsed the number " << result << std::endl; |
| 96 | return EXIT_SUCCESS; |
| 97 | } |
| 98 | ``` |
| 99 | |
| 100 | You can parse delimited numbers: |
| 101 | ```C++ |
| 102 | const std::string input = "234532.3426362,7869234.9823,324562.645"; |
| 103 | double result; |
| 104 | auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result); |
| 105 | if(answer.ec != std::errc()) { |
| 106 | // check error |
| 107 | } |
| 108 | // we have result == 234532.3426362. |
| 109 | if(answer.ptr[0] != ',') { |
| 110 | // unexpected delimiter |
| 111 | } |
| 112 | answer = fast_float::from_chars(answer.ptr + 1, input.data()+input.size(), result); |
| 113 | if(answer.ec != std::errc()) { |
| 114 | // check error |
| 115 | } |
| 116 | // we have result == 7869234.9823. |
| 117 | if(answer.ptr[0] != ',') { |
| 118 | // unexpected delimiter |
| 119 | } |
| 120 | answer = fast_float::from_chars(answer.ptr + 1, input.data()+input.size(), result); |
| 121 | if(answer.ec != std::errc()) { |
| 122 | // check error |
| 123 | } |
| 124 | // we have result == 324562.645. |
| 125 | ``` |
| 126 | |
| 127 | ## Reference |
| 128 | |
| 129 | - Daniel Lemire, [Number Parsing at a Gigabyte per Second](https://arxiv.org/abs/2101.11408), Software: Practice and Experience 51 (8), 2021. |
| 130 | |
| 131 | ## Other programming languages |
| 132 | |
| 133 | - [There is an R binding](https://github.com/eddelbuettel/rcppfastfloat) called `rcppfastfloat`. |
| 134 | - [There is a Rust port of the fast_float library](https://github.com/aldanor/fast-float-rust/) called `fast-float-rust`. |
| 135 | - [There is a Java port of the fast_float library](https://github.com/wrandelshofer/FastDoubleParser) called `FastDoubleParser`. It used for important systems such as [Jackson](https://github.com/FasterXML/jackson-core). |
| 136 | - [There is a C# port of the fast_float library](https://github.com/CarlVerret/csFastFloat) called `csFastFloat`. |
| 137 | |
| 138 | |
| 139 | ## Relation With Other Work |
| 140 | |
| 141 | The fast_float library is part of GCC (as of version 12): the `from_chars` function in GCC relies on fast_float. |
| 142 | |
| 143 | The fastfloat algorithm is part of the [LLVM standard libraries](https://github.com/llvm/llvm-project/commit/87c016078ad72c46505461e4ff8bfa04819fe7ba). |
| 144 | |
| 145 | The fast_float library provides a performance similar to that of the [fast_double_parser](https://github.com/lemire/fast_double_parser) library but using an updated algorithm reworked from the ground up, and while offering an API more in line with the expectations of C++ programmers. The fast_double_parser library is part of the [Microsoft LightGBM machine-learning framework](https://github.com/microsoft/LightGBM). |
| 146 | |
| 147 | There is a [derived implementation part of AdaCore](https://github.com/AdaCore/VSS). |
| 148 | |
| 149 | ## Users |
| 150 | |
| 151 | The fast_float library is used by [Apache Arrow](https://github.com/apache/arrow/pull/8494) where it multiplied the number parsing speed by two or three times. It is also used by [Yandex ClickHouse](https://github.com/ClickHouse/ClickHouse) and by [Google Jsonnet](https://github.com/google/jsonnet). |
| 152 | |
| 153 | |
| 154 | ## How fast is it? |
| 155 | |
| 156 | It can parse random floating-point numbers at a speed of 1 GB/s on some systems. We find that it is often twice as fast as the best available competitor, and many times faster than many standard-library implementations. |
| 157 | |
| 158 | <img src="http://lemire.me/blog/wp-content/uploads/2020/11/fastfloat_speed.png" width="400"> |
| 159 | |
| 160 | ``` |
| 161 | $ ./build/benchmarks/benchmark |
| 162 | # parsing random integers in the range [0,1) |
| 163 | volume = 2.09808 MB |
| 164 | netlib : 271.18 MB/s (+/- 1.2 %) 12.93 Mfloat/s |
| 165 | doubleconversion : 225.35 MB/s (+/- 1.2 %) 10.74 Mfloat/s |
| 166 | strtod : 190.94 MB/s (+/- 1.6 %) 9.10 Mfloat/s |
| 167 | abseil : 430.45 MB/s (+/- 2.2 %) 20.52 Mfloat/s |
| 168 | fastfloat : 1042.38 MB/s (+/- 9.9 %) 49.68 Mfloat/s |
| 169 | ``` |
| 170 | |
| 171 | See https://github.com/lemire/simple_fastfloat_benchmark for our benchmarking code. |
| 172 | |
| 173 | |
| 174 | ## Video |
| 175 | |
| 176 | [](http://www.youtube.com/watch?v=AVXgvlMeIm4)<br /> |
| 177 | |
| 178 | ## Using as a CMake dependency |
| 179 | |
| 180 | This library is header-only by design. The CMake file provides the `fast_float` target |
| 181 | which is merely a pointer to the `include` directory. |
| 182 | |
| 183 | If you drop the `fast_float` repository in your CMake project, you should be able to use |
| 184 | it in this manner: |
| 185 | |
| 186 | ```cmake |
| 187 | add_subdirectory(fast_float) |
| 188 | target_link_libraries(myprogram PUBLIC fast_float) |
| 189 | ``` |
| 190 | |
| 191 | Or you may want to retrieve the dependency automatically if you have a sufficiently recent version of CMake (3.11 or better at least): |
| 192 | |
| 193 | ```cmake |
| 194 | FetchContent_Declare( |
| 195 | fast_float |
| 196 | GIT_REPOSITORY https://github.com/lemire/fast_float.git |
| 197 | GIT_TAG tags/v1.1.2 |
| 198 | GIT_SHALLOW TRUE) |
| 199 | |
| 200 | FetchContent_MakeAvailable(fast_float) |
| 201 | target_link_libraries(myprogram PUBLIC fast_float) |
| 202 | |
| 203 | ``` |
| 204 | |
| 205 | You should change the `GIT_TAG` line so that you recover the version you wish to use. |
| 206 | |
| 207 | ## Using as single header |
| 208 | |
| 209 | The script `script/amalgamate.py` may be used to generate a single header |
| 210 | version of the library if so desired. |
| 211 | Just run the script from the root directory of this repository. |
| 212 | You can customize the license type and output file if desired as described in |
| 213 | the command line help. |
| 214 | |
| 215 | You may directly download automatically generated single-header files: |
| 216 | |
| 217 | https://github.com/fastfloat/fast_float/releases/download/v3.4.0/fast_float.h |
| 218 | |
| 219 | ## Credit |
| 220 | |
| 221 | Though this work is inspired by many different people, this work benefited especially from exchanges with |
| 222 | Michael Eisel, who motivated the original research with his key insights, and with Nigel Tao who provided |
| 223 | invaluable feedback. Rémy Oudompheng first implemented a fast path we use in the case of long digits. |
| 224 | |
| 225 | The library includes code adapted from Google Wuffs (written by Nigel Tao) which was originally published |
| 226 | under the Apache 2.0 license. |
| 227 | |
| 228 | ## License |
| 229 | |
| 230 | <sup> |
| 231 | Licensed under either of <a href="LICENSE-APACHE">Apache License, Version |
| 232 | 2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. |
| 233 | </sup> |
| 234 | |
| 235 | <br> |
| 236 | |
| 237 | <sub> |
| 238 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 239 | for inclusion in this repository by you, as defined in the Apache-2.0 license, |
| 240 | shall be dual licensed as above, without any additional terms or conditions. |
| 241 | </sub> |