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

Speed up float parsing #110

Merged
merged 1 commit into from Jul 11, 2016
Merged

Speed up float parsing #110

merged 1 commit into from Jul 11, 2016

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jul 11, 2016

Fixes #108 and fixes #109.

Parse floats as a u64 until the end, then use a lookup table to scale by the correct power of 10. This is faster and also more accurate. The old code used repeated division by 10 which is so inaccurate that 1.0 / 10.0 / 10.0 / 10.0 / 10.0 / 10.0 / 10.0 != 1e-6.

This is about 40% improvement on the Canada benchmark (almost twice as fast).

}
None => {
if exponent >= 0 && significand != 0 {
f64::INFINITY
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should default to infinity. Not only does it break round tripping, it's also very confusing to get an infinity from a non-infinite number

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches the current behavior (as of 0.7.4). I filed #112 to change the behavior.

assert_eq!(f64::INFINITY, serde_json::from_str("1e1000").unwrap());

@oli-obk oli-obk merged commit 4cc85e5 into master Jul 11, 2016
@oli-obk oli-obk deleted the float branch July 11, 2016 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Long integer causes the following byte to be lost Parsing floats is slow
2 participants