Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Super-fast float parser in Rust (now part of Rust core)

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
NotificationsYou must be signed in to change notification settings

aldanor/fast-float-rust

Repository files navigation

BuildLatest VersionDocumentationApache 2.0MITRustc 1.37+

This crate provides a super-fast decimal number parser from strings into floats.

[dependencies]fast-float ="0.2"

There are no dependencies and the crate can be used in a no_std context by disabling the "std" feature.

Compiler support: rustc 1.37+.

Usage

There's two top-level functions provided:parse() andparse_partial(), both takingeither a string or a bytes slice and parsing the input into eitherf32 orf64:

  • parse() treats the whole string as a decimal number and returns an error if there areinvalid characters or if the string is empty.
  • parse_partial() tries to find the longest substring at the beginning of the given inputstring that can be parsed as a decimal number and, in the case of success, returns the parsedvalue along the number of characters processed; an error is returned if the string doesn'tstart with a decimal number or if it is empty. This function is most useful as a buildingblock when constructing more complex parsers, or when parsing streams of data.

Example:

// Parse the entire string as a decimal number.let s ="1.23e-02";let x:f32 = fast_float::parse(s).unwrap();assert_eq!(x,0.0123);// Parse as many characters as possible as a decimal number.let s ="1.23e-02foo";let(x, n) = fast_float::parse_partial::<f32,_>(s).unwrap();assert_eq!(x,0.0123);assert_eq!(n,8);assert_eq!(&s[n..],"foo");

Details

This crate is a direct port of Daniel Lemire'sfast_floatC++ library (valuable discussions with Daniel while porting it helped shape the crate and get it tothe performance level it's at now), with some Rust-specific tweaks. Please see the originalrepository for many useful details regarding the algorithm and the implementation.

The parser is locale-independent. The resulting value is the closest floating-point values (using eitherf32 orf64), using the "round to even" convention for values that would otherwise fall right in-betweentwo values. That is, we provide exact parsing according to the IEEE standard.

Infinity and NaN values can be parsed, along with scientific notation.

Both little-endian and big-endian platforms are equally supported, with extra optimizations enabledon little-endian architectures.

Testing

There are a few ways this crate is tested:

  • A suite of explicit tests (taken from the original library) covering lots of edge cases.
  • A file-based test suite (taken from the original library; credits to Nigel Tao), ~5M tests.
  • All 4B float32 numbers are exhaustively roundtripped via ryu formatter.
  • Roundtripping a large quantity of random float64 numbers via ryu formatter.
  • Roundtripping float64 numbers and fuzzing random input strings via cargo-fuzz.
  • All explicit test suites run on CI; roundtripping and fuzzing are run manually.

Performance

The presented parser seems to beat all of the existing C/C++/Rust float parsers known to us at themoment by a large margin, in all of the datasets we tested it on so far – see detailed benchmarksbelow (the only exception being the original fast_float C++ library, of course – performance ofwhich is within noise bounds of this crate). On modern machines like Apple M1, parsing throughputcan reach up to 1.5 GB/s.

In particular, it is faster than Rust standard library'sFromStr::from_str() by a factor of 2-8x(larger factor for longer float strings), and is typically 2-3x faster than the nearest competitors.

While various details regarding the algorithm can be found in the repository for the originalC++ library, here are few brief notes:

  • The parser is specialized to work lightning-fast on inputs with at most 19 significant digits(which constitutes the so called "fast-path"). We believe that most real-life inputs shouldfall under this category, and we treat longer inputs as "degenerate" edge cases since itinevitable causes overflows and loss of precision.
  • If the significand happens to be longer than 19 digits, the parser falls back to the "slow path",in which case its performance roughly matches that of the top Rust/C++ libraries (and stillbeats them most of the time, although not by a lot).
  • On little-endian systems, there's additional optimizations for numbers with more than 8 digitsafter the decimal point.

Benchmarks

Below are tables of best timings in nanoseconds for parsing a single numberinto a 64-bit float.

Intel i7-4771

Intel i7-4771 3.5GHz, macOS, Rust 1.49.

canadameshuniformiidiieirec32
fast-float21.5810.7019.3640.5026.0729.13
lexical65.9023.2854.7575.8052.1875.36
from_str174.4322.3099.93227.76111.31204.46
fast_float (C++)22.7810.9920.0541.1227.5130.85
abseil (C++)42.6632.8846.0150.8346.3349.95
netlib (C)57.5324.8664.7256.6336.2067.29
strtod (C)286.1031.15258.73295.73205.72315.95

Apple M1

Apple M1, macOS, Rust 1.49.

canadameshuniformiidiieirec32
fast-float14.845.9811.2433.2421.3017.86
lexical47.0916.5143.4656.0636.6855.48
from_str136.0013.8474.64179.8777.91154.53
fast_float (C++)13.717.2811.7132.9420.6418.30
abseil (C++)36.5524.2038.4840.8635.4640.09
netlib (C)47.1914.1248.8552.2833.7048.79
strtod (C)176.1321.48165.43187.98132.19190.63

AMD Rome

AMD Rome, Linux, Rust 1.49.

canadameshuniformiidiieirec32
fast-float25.9012.1220.5447.0129.2332.36
lexical63.1822.1354.7881.2355.0679.14
from_str190.0626.10102.44239.87119.04211.73
fast_float (C++)21.2910.4718.3142.3324.5629.76
abseil (C++)44.5434.1347.3852.6443.7753.03
netlib (C)69.4323.3179.9872.1735.8186.91
strtod (C)123.3765.68101.58118.36118.61123.72

Parsers

  • fast-float - this very crate
  • lexicallexical_core, v0.7 (non-lossy; same performance as lossy)
  • from_str – Rust standard library,FromStr trait
  • fast_float (C++) – original C++ implementation of 'fast-float' method
  • abseil (C++) – Abseil C++ Common Libraries
  • netlib (C++) – C++ Network Library
  • strtod (C) – C standard library

Datasets

  • canada – numbers incanada.txt file
  • mesh – numbers inmesh.txt file
  • uniform – uniform random numbers from 0 to 1
  • iidi – random numbers of format%d%d.%d
  • iei – random numbers of format%de%d
  • rec32 – reciprocals of random 32-bit integers

Notes

  • The two test files referred above can be found inthis repository.
  • The Rust part of the table (along with a few other benchmarks) can be generated viathe benchmark tool that can be found underextras/simple-bench of this repo.
  • The C/C++ part of the table (along with a few other benchmarks and parsers) can begenerated via a C++ utility that can be found inthis repository.

References

License

Licensed under either ofApache License, Version2.0 orMIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submittedfor inclusion in this crate by you, as defined in the Apache-2.0 license, shallbe dual licensed as above, without any additional terms or conditions.

About

Super-fast float parser in Rust (now part of Rust core)

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Contributors4

  •  
  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp