| 1 | fast-float2 |
| 2 | =========== |
| 3 | |
| 4 | [](https://github.com/Alexhuszagh/fast-float-rust/actions?query=branch%3Amaster) |
| 5 | [](https://crates.io/crates/fast-float2) |
| 6 | [](https://docs.rs/fast-float2) |
| 7 | [](https://opensource.org/licenses/Apache-2.0) |
| 8 | [](https://opensource.org/licenses/MIT) |
| 9 | [](https://blog.rust-lang.org/2019/08/15/Rust-1.37.0.html) |
| 10 | |
| 11 | This crate provides a super-fast decimal number parser from strings into floats. |
| 12 | |
| 13 | ```toml |
| 14 | [dependencies] |
| 15 | fast-float2 = "0.2.3" |
| 16 | ``` |
| 17 | |
| 18 | There are no dependencies and the crate can be used in a no_std context by disabling the "std" feature. |
| 19 | |
| 20 | *Compiler support: rustc 1.37+.* |
| 21 | |
| 22 | This crate is in maintenance mode for bug fixes (especially security patches): minimal feature enhancements will be accepted. This implementation has been adopted by the Rust standard library: if you do not need parsing directly from bytes and/or partial parsers, you should use [FromStr](https://doc.rust-lang.org/std/str/trait.FromStr.html) for [f32](https://doc.rust-lang.org/std/primitive.f32.html) or [f64](https://doc.rust-lang.org/std/primitive.f64.html) instead. |
| 23 | |
| 24 | ## Usage |
| 25 | |
| 26 | There's two top-level functions provided: |
| 27 | [`parse()`](https://docs.rs/fast-float/latest/fast_float/fn.parse.html) and |
| 28 | [`parse_partial()`](https://docs.rs/fast-float/latest/fast_float/fn.parse_partial.html), both taking |
| 29 | either a string or a bytes slice and parsing the input into either `f32` or `f64`: |
| 30 | |
| 31 | - `parse()` treats the whole string as a decimal number and returns an error if there are |
| 32 | invalid characters or if the string is empty. |
| 33 | - `parse_partial()` tries to find the longest substring at the beginning of the given input |
| 34 | string that can be parsed as a decimal number and, in the case of success, returns the parsed |
| 35 | value along the number of characters processed; an error is returned if the string doesn't |
| 36 | start with a decimal number or if it is empty. This function is most useful as a building |
| 37 | block when constructing more complex parsers, or when parsing streams of data. |
| 38 | |
| 39 | Example: |
| 40 | |
| 41 | ```rust |
| 42 | // Parse the entire string as a decimal number. |
| 43 | let s = "1.23e-02"; |
| 44 | let x: f32 = fast_float2::parse(s).unwrap(); |
| 45 | assert_eq!(x, 0.0123); |
| 46 | |
| 47 | // Parse as many characters as possible as a decimal number. |
| 48 | let s = "1.23e-02foo"; |
| 49 | let (x, n) = fast_float2::parse_partial::<f32, _>(s).unwrap(); |
| 50 | assert_eq!(x, 0.0123); |
| 51 | assert_eq!(n, 8); |
| 52 | assert_eq!(&s[n..], "foo"); |
| 53 | ``` |
| 54 | |
| 55 | ## Details |
| 56 | |
| 57 | This crate is a direct port of Daniel Lemire's [`fast_float`](https://github.com/fastfloat/fast_float) |
| 58 | C++ library (valuable discussions with Daniel while porting it helped shape the crate and get it to |
| 59 | the performance level it's at now), with some Rust-specific tweaks. Please see the original |
| 60 | repository for many useful details regarding the algorithm and the implementation. |
| 61 | |
| 62 | The parser is locale-independent. The resulting value is the closest floating-point values (using either |
| 63 | `f32` or `f64`), using the "round to even" convention for values that would otherwise fall right in-between |
| 64 | two values. That is, we provide exact parsing according to the IEEE standard. |
| 65 | |
| 66 | Infinity and NaN values can be parsed, along with scientific notation. |
| 67 | |
| 68 | Both little-endian and big-endian platforms are equally supported, with extra optimizations enabled |
| 69 | on little-endian architectures. |
| 70 | |
| 71 | Since [fast-float-rust](https://github.com/aldanor/fast-float-rust) is unmaintained, this is a fork |
| 72 | containing the patches and security updates. |
| 73 | |
| 74 | ## Testing |
| 75 | |
| 76 | There are a few ways this crate is tested: |
| 77 | |
| 78 | - A suite of explicit tests (taken from the original library) covering lots of edge cases. |
| 79 | - A file-based test suite (taken from the original library; credits to Nigel Tao), ~5M tests. |
| 80 | - All 4B float32 numbers are exhaustively roundtripped via ryu formatter. |
| 81 | - Roundtripping a large quantity of random float64 numbers via ryu formatter. |
| 82 | - Roundtripping float64 numbers and fuzzing random input strings via cargo-fuzz. |
| 83 | - All explicit test suites run on CI; roundtripping and fuzzing are run manually. |
| 84 | |
| 85 | ## Performance |
| 86 | |
| 87 | The presented parser seems to beat all of the existing C/C++/Rust float parsers known to us at the |
| 88 | moment by a large margin, in all of the datasets we tested it on so far – see detailed benchmarks |
| 89 | below (the only exception being the original fast_float C++ library, of course – performance of |
| 90 | which is within noise bounds of this crate). On modern machines like Apple M1, parsing throughput |
| 91 | can reach up to 1.5 GB/s. |
| 92 | |
| 93 | In particular, it is faster than Rust standard library's `FromStr::from_str()` by a factor of 2-8x |
| 94 | (larger factor for longer float strings), and is typically 2-3x faster than the nearest competitors. |
| 95 | |
| 96 | While various details regarding the algorithm can be found in the repository for the original |
| 97 | C++ library, here are few brief notes: |
| 98 | |
| 99 | - The parser is specialized to work lightning-fast on inputs with at most 19 significant digits |
| 100 | (which constitutes the so called "fast-path"). We believe that most real-life inputs should |
| 101 | fall under this category, and we treat longer inputs as "degenerate" edge cases since it |
| 102 | inevitable causes overflows and loss of precision. |
| 103 | - If the significand happens to be longer than 19 digits, the parser falls back to the "slow path", |
| 104 | in which case its performance roughly matches that of the top Rust/C++ libraries (and still |
| 105 | beats them most of the time, although not by a lot). |
| 106 | - On little-endian systems, there's additional optimizations for numbers with more than 8 digits |
| 107 | after the decimal point. |
| 108 | |
| 109 | ## Benchmarks |
| 110 | |
| 111 | Below are tables of best timings in nanoseconds for parsing a single number |
| 112 | into a 64-bit float. |
| 113 | |
| 114 | #### Intel i7-4771 |
| 115 | |
| 116 | Intel i7-4771 3.5GHz, macOS, Rust 1.49. |
| 117 | |
| 118 | | | `canada` | `mesh` | `uniform` | `iidi` | `iei` | `rec32` | |
| 119 | | ---------------- | -------- | -------- | --------- | ------ | ------ | ------- | |
| 120 | | fast-float | 21.58 | 10.70 | 19.36 | 40.50 | 26.07 | 29.13 | |
| 121 | | lexical | 65.90 | 23.28 | 54.75 | 75.80 | 52.18 | 75.36 | |
| 122 | | from_str | 174.43 | 22.30 | 99.93 | 227.76 | 111.31 | 204.46 | |
| 123 | | fast_float (C++) | 22.78 | 10.99 | 20.05 | 41.12 | 27.51 | 30.85 | |
| 124 | | abseil (C++) | 42.66 | 32.88 | 46.01 | 50.83 | 46.33 | 49.95 | |
| 125 | | netlib (C) | 57.53 | 24.86 | 64.72 | 56.63 | 36.20 | 67.29 | |
| 126 | | strtod (C) | 286.10 | 31.15 | 258.73 | 295.73 | 205.72 | 315.95 | |
| 127 | |
| 128 | #### Apple M1 |
| 129 | |
| 130 | Apple M1, macOS, Rust 1.49. |
| 131 | |
| 132 | | | `canada` | `mesh` | `uniform` | `iidi` | `iei` | `rec32` | |
| 133 | | ---------------- | -------- | -------- | --------- | ------ | ------ | ------- | |
| 134 | | fast-float | 14.84 | 5.98 | 11.24 | 33.24 | 21.30 | 17.86 | |
| 135 | | lexical | 47.09 | 16.51 | 43.46 | 56.06 | 36.68 | 55.48 | |
| 136 | | from_str | 136.00 | 13.84 | 74.64 | 179.87 | 77.91 | 154.53 | |
| 137 | | fast_float (C++) | 13.71 | 7.28 | 11.71 | 32.94 | 20.64 | 18.30 | |
| 138 | | abseil (C++) | 36.55 | 24.20 | 38.48 | 40.86 | 35.46 | 40.09 | |
| 139 | | netlib (C) | 47.19 | 14.12 | 48.85 | 52.28 | 33.70 | 48.79 | |
| 140 | | strtod (C) | 176.13 | 21.48 | 165.43 | 187.98 | 132.19 | 190.63 | |
| 141 | |
| 142 | #### AMD Rome |
| 143 | |
| 144 | AMD Rome, Linux, Rust 1.49. |
| 145 | |
| 146 | | | `canada` | `mesh` | `uniform` | `iidi` | `iei` | `rec32` | |
| 147 | | ---------------- | -------- | -------- | --------- | ------ | ------ | ------- | |
| 148 | | fast-float | 25.90 | 12.12 | 20.54 | 47.01 | 29.23 | 32.36 | |
| 149 | | lexical | 63.18 | 22.13 | 54.78 | 81.23 | 55.06 | 79.14 | |
| 150 | | from_str | 190.06 | 26.10 | 102.44 | 239.87 | 119.04 | 211.73 | |
| 151 | | fast_float (C++) | 21.29 | 10.47 | 18.31 | 42.33 | 24.56 | 29.76 | |
| 152 | | abseil (C++) | 44.54 | 34.13 | 47.38 | 52.64 | 43.77 | 53.03 | |
| 153 | | netlib (C) | 69.43 | 23.31 | 79.98 | 72.17 | 35.81 | 86.91 | |
| 154 | | strtod (C) | 123.37 | 65.68 | 101.58 | 118.36 | 118.61 | 123.72 | |
| 155 | |
| 156 | #### Parsers |
| 157 | |
| 158 | - `fast-float` - this very crate |
| 159 | - `lexical` – `lexical_core`, v0.7 (non-lossy; same performance as lossy) |
| 160 | - `from_str` – Rust standard library, `FromStr` trait |
| 161 | - `fast_float (C++)` – original C++ implementation of 'fast-float' method |
| 162 | - `abseil (C++)` – Abseil C++ Common Libraries |
| 163 | - `netlib (C++)` – C++ Network Library |
| 164 | - `strtod (C)` – C standard library |
| 165 | |
| 166 | #### Datasets |
| 167 | |
| 168 | - `canada` – numbers in `canada.txt` file |
| 169 | - `mesh` – numbers in `mesh.txt` file |
| 170 | - `uniform` – uniform random numbers from 0 to 1 |
| 171 | - `iidi` – random numbers of format `%d%d.%d` |
| 172 | - `iei` – random numbers of format `%de%d` |
| 173 | - `rec32` – reciprocals of random 32-bit integers |
| 174 | |
| 175 | #### Notes |
| 176 | |
| 177 | - The two test files referred above can be found in |
| 178 | [this](https://github.com/lemire/simple_fastfloat_benchmark) repository. |
| 179 | - The Rust part of the table (along with a few other benchmarks) can be generated via |
| 180 | the benchmark tool that can be found under `extras/simple-bench` of this repo. |
| 181 | - The C/C++ part of the table (along with a few other benchmarks and parsers) can be |
| 182 | generated via a C++ utility that can be found in |
| 183 | [this](https://github.com/lemire/simple_fastfloat_benchmark) repository. |
| 184 | |
| 185 | <br> |
| 186 | |
| 187 | #### References |
| 188 | |
| 189 | - Daniel Lemire, [Number Parsing at a Gigabyte per Second](https://arxiv.org/abs/2101.11408), Software: Practice and Experience 51 (8), 2021. |
| 190 | |
| 191 | #### License |
| 192 | |
| 193 | <sup> |
| 194 | Licensed under either of <a href="LICENSE-APACHE">Apache License, Version |
| 195 | 2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. |
| 196 | </sup> |
| 197 | |
| 198 | <br> |
| 199 | |
| 200 | <sub> |
| 201 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 202 | for inclusion in this crate by you, as defined in the Apache-2.0 license, shall |
| 203 | be dual licensed as above, without any additional terms or conditions. |
| 204 | </sub> |