-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
bad performance of char literal to_string() vs str literal to_string() #73462
Comments
…ince str to String conversion is cheaper. See rust-lang#73462
The problem seems to be that let mut s = String::new();
s.push(self);
s is probably the fix |
godbolt link: https://rust.godbolt.org/z/aYEaZr |
Hm. |
Add specialization of `ToString for char` Closes rust-lang#73462
Looks like byte and integer runs as slow. test tests::bench_byte_to_string ... bench: 51,257 ns/iter (+/- 1,623)
test tests::bench_char_to_string ... bench: 43,246 ns/iter (+/- 7,624)
test tests::bench_int_to_string ... bench: 52,218 ns/iter (+/- 3,077)
test tests::bench_str_to_string ... bench: 15,701 ns/iter (+/- 380) Benchmark code https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=a440f60fb09e7e6e4c60eef0a1ebfac1 |
Should we open another issue for byte and int? |
yeah, do it. |
I was quite surprised when I found out that
generates several times the instruction count of
(according to godbold).
Checking the (debug) build size of
println!("{}", "a".to_string());
vsprintln!("{}", 'a'.to_string());
revealed thestr
variant being ~20kb lighter.When doing some quick-and-dirty benchmarks, the
str
variant was more than 2x faster than the char-variant. benchmark codeIs this something that could be optimized?
The text was updated successfully, but these errors were encountered: