-
Notifications
You must be signed in to change notification settings - Fork 136
RFC: Use Rust's string-to-number conversions, instead of home-grown ones #156
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
Conversation
How is the performance of |
CC @bzbarsky |
I, too, would want to see some performance numbers. I would also like to know what the rounding behavior is. That is, it sounds like the Note also that serialization of floats in Rust via the |
I've started https://github.com/federicomenaquintero/float-tests to see what the various parsers generate. The Cargo.toml has URLs for the stock rust-cssparser and my version. I'm printing ToCss::to_css_string()'s result out of a parsed token, and so far both versions produce the same results. I'm not sure yet why rust-cssparser's tests work in the stock version, nor why the differences happen in that string for 0.67. I'll look into writing a benchmark for both versions. |
OK, found it. Summary: the values in the expected JSON fixture have been divided and multiplied by 100, for Percentage values, and this causes loss of precision from the original parsed values. I put some println!()s right where the float value is built, and then when it is serialized to JSON.
And later,
where The original multiply-by-ten code outputs this:
(That's the string slice, the f32 value, and then the value divided/multiplied by 100.) My version with f32::from_str():
The test program from my [float-tests] prints this at the end, when used with the stock rust-cssparser:
And when run with my patched version:
The f32's differ by a single bit, so maybe the *100 /100 roundtrip is not too bad :) |
I'll get some benchmarks. I don't think the loss of precision is worth considering; it's way deep in the fractional part. |
There are two ways the loss of precision could in theory manifest itself:
|
☔ The latest upstream changes (presumably #159) made this pull request unmergeable. Please resolve the merge conflicts. |
7ccbd97
to
58d733a
Compare
Discarding this for now; I haven't had time to investigate it further. |
Rust's standard library contains pretty sophisticated code for parsing numbers from strings. This pull request modifies Tokenizer to that, instead of using a trivial "multiply by 10 and add the digit" algorithm.
However, this makes the tests fail!
For example, one of the tests parses a "
.67%
" token out of a bigger component-value-list, and the token has a spec likeand it fails with
Some things to note:
write!(writer, "{}", float)
. In the example above, this is what generates the"0.67"
in the first result line, but that is different from the test fixture's expected"0.66999996"
. I am not sure which side to trust :)I'm fine with this pull request not being merged, since the tests fail - but I'm not sure how those test values were generated in the first place?
Comments on the above are appreciated.
This change is