-
Notifications
You must be signed in to change notification settings - Fork 13.3k
to_digit simplification (less jumps) #85630
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
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
There bultin diff function in godbolt: https://godbolt.org/z/jMsv6dKh8 |
This comment has been minimized.
This comment has been minimized.
Good point. Have flicked the code around so the red / green works in the diff and optimised the to upper case: |
This comment has been minimized.
This comment has been minimized.
I don't believe this implementation is correct. E.g. it will return Some other issues: A is Some(17) in base 36, a is None in base 36, even though both should be returning a (Can you add cases like these as tests, please?) |
Sorry jumped strate to the bench skipping tests - too much excitement. |
This comment has been minimized.
This comment has been minimized.
Ok am really happy with that: https://godbolt.org/z/n77njGz3T |
@nagisa is this all looking good now from your perspective? |
Squash the commits, please. Otherwise looks pretty nice. |
Can you update benches in first message with latest version? |
r? @nagisa |
will do |
Have updated the benchmarks. I'm not really happy with the benchmark definitions though I have failed to come up with an alternative that has less benchmark noise. |
bcea845
to
9c3d81e
Compare
Ok @nagisa think it is good to go. |
@bors r+ |
📌 Commit 9c3d81e has been approved by |
@gilescope You can use cargo-benchcmp to compare bench results in more simple way. |
☀️ Test successful - checks-actions |
to_digit simplification (less jumps) I just realised we might be able to make use of the fact that changing case in ascii is easy to help simplify to_digit some more. It looks a bit cleaner and it looks like it's less jumps and there's less instructions in the generated assembly: https://godbolt.org/z/84Erh5dhz The benchmarks don't really tell me much. Maybe a slight improvement on the var radix. Before: ``` test char::methods::bench_to_digit_radix_10 ... bench: 53,819 ns/iter (+/- 8,314) test char::methods::bench_to_digit_radix_16 ... bench: 57,265 ns/iter (+/- 10,730) test char::methods::bench_to_digit_radix_2 ... bench: 55,077 ns/iter (+/- 5,431) test char::methods::bench_to_digit_radix_36 ... bench: 56,549 ns/iter (+/- 3,248) test char::methods::bench_to_digit_radix_var ... bench: 43,848 ns/iter (+/- 3,189) test char::methods::bench_to_digit_radix_10 ... bench: 51,707 ns/iter (+/- 10,946) test char::methods::bench_to_digit_radix_16 ... bench: 52,835 ns/iter (+/- 2,689) test char::methods::bench_to_digit_radix_2 ... bench: 51,012 ns/iter (+/- 2,746) test char::methods::bench_to_digit_radix_36 ... bench: 53,210 ns/iter (+/- 8,645) test char::methods::bench_to_digit_radix_var ... bench: 40,386 ns/iter (+/- 4,711) test char::methods::bench_to_digit_radix_10 ... bench: 54,088 ns/iter (+/- 5,677) test char::methods::bench_to_digit_radix_16 ... bench: 55,972 ns/iter (+/- 17,229) test char::methods::bench_to_digit_radix_2 ... bench: 52,083 ns/iter (+/- 2,425) test char::methods::bench_to_digit_radix_36 ... bench: 54,132 ns/iter (+/- 1,548) test char::methods::bench_to_digit_radix_var ... bench: 41,250 ns/iter (+/- 5,299) ``` After: ``` test char::methods::bench_to_digit_radix_10 ... bench: 48,907 ns/iter (+/- 19,449) test char::methods::bench_to_digit_radix_16 ... bench: 52,673 ns/iter (+/- 8,122) test char::methods::bench_to_digit_radix_2 ... bench: 48,509 ns/iter (+/- 2,885) test char::methods::bench_to_digit_radix_36 ... bench: 50,526 ns/iter (+/- 4,610) test char::methods::bench_to_digit_radix_var ... bench: 38,618 ns/iter (+/- 3,180) test char::methods::bench_to_digit_radix_10 ... bench: 54,202 ns/iter (+/- 6,994) test char::methods::bench_to_digit_radix_16 ... bench: 56,585 ns/iter (+/- 8,448) test char::methods::bench_to_digit_radix_2 ... bench: 50,548 ns/iter (+/- 1,674) test char::methods::bench_to_digit_radix_36 ... bench: 52,749 ns/iter (+/- 2,576) test char::methods::bench_to_digit_radix_var ... bench: 40,215 ns/iter (+/- 3,327) test char::methods::bench_to_digit_radix_10 ... bench: 50,233 ns/iter (+/- 22,272) test char::methods::bench_to_digit_radix_16 ... bench: 50,841 ns/iter (+/- 19,981) test char::methods::bench_to_digit_radix_2 ... bench: 50,386 ns/iter (+/- 4,555) test char::methods::bench_to_digit_radix_36 ... bench: 52,369 ns/iter (+/- 2,737) test char::methods::bench_to_digit_radix_var ... bench: 40,417 ns/iter (+/- 2,766) ``` I removed the likely as it resulted in a few less instructions. (It's not been in there long - I added it in the last to_digit iteration).
I just realised we might be able to make use of the fact that changing case in ascii is easy to help simplify to_digit some more.
It looks a bit cleaner and it looks like it's less jumps and there's less instructions in the generated assembly:
https://godbolt.org/z/84Erh5dhz
The benchmarks don't really tell me much. Maybe a slight improvement on the var radix.
Before:
After:
I removed the likely as it resulted in a few less instructions. (It's not been in there long - I added it in the last to_digit iteration).