-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Change an instance of .collect::<Vec<_>>().join("")
to .collect::<String>()
#95216
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
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @wesleywiser (or someone else) soon. Please see the contribution instructions for more information. |
This runs afoul of the no-merge policy, you'll have to rebase. |
@the8472 Should be fine now |
.collect::<Vec<_>>().join("");
=> .collect::<String>();
.collect::<Vec<_>>().join("")
to .collect::<String>()
Unless I'm reading this wrong, doesn't this suggest the opposite? That collecting into a vec and joining is faster? |
@jackh726 you're right, I misread the results in this case. All other instances of Thanks! |
For instance if you take a similar case that Deno had: criterion.bench_function("1", |bencher| {
bencher.iter(|| {
let url = "https://google.com".to_owned();
let split_specifier = url.as_str().split(':');
split_specifier.skip(1).collect::<String>();
})
});
criterion.bench_function("2", |bencher| {
bencher.iter(|| {
let url = "https://google.com".to_owned();
let split_specifier = url.as_str().split(':');
split_specifier.skip(1).collect::<Vec<_>>().join("");
})
});
|
And my own benchmark: fn criterion_benchmark(criterion: &mut Criterion) {
criterion.bench_function("1", |bencher| {
bencher.iter(|| {
vec!["hello", "world"]
.iter()
.map(|item| item.to_uppercase())
.collect::<Vec<String>>()
.join("")
})
});
criterion.bench_function("2", |bencher| {
bencher.iter(|| {
vec!["hello", "world"]
.iter()
.map(|item| item.to_uppercase())
.collect::<String>()
})
});
}
|
Compiler Explorer shows that although the generated assembly is simpler it doesn't unroll the loop as it does with |
Hey,
I've noticed that
compiler/rustc_resolve/src/late/diagnostics.rs
uses.collect::<Vec<_>>().join("");
over.collect::<String>();
, the latter being shorter, clearer and more performant:I'm currently working on a clippy PR to lint this issue but thought to create a manual PR for Deno
Thanks for your consideration!