-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Vec::retain()
is significantly slower than into_iter().filter().collect()
#91497
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
Comments
i7 8700k windows 10
with lto = true
rustc 1.59.0-nightly (acbe444) |
Setting
|
It looks like Since the test-case here drops every second element it's the worst possible case for Edit: Hrm, the benchmarks look like they're optimizing for high/low retain probabilities; but the code doesn't, it still moves one element at a time when previous ones have been dropped. |
Edit: Doh, but at least now we now that #88060 caused some major slowdown. |
You're not copying the most recent version of retain. rust/library/alloc/src/vec/mod.rs Lines 1524 to 1565 in ff2439b
|
I tried a few scenarios that should heavily favor
i7 8700k w10 rustc 1.59.0-nightly (acbe444) |
I'll take a stab at this. @rustbot claim |
Submitted #91527, if anyone wants to confirm my benchmark results. |
I noticed today that using
Vec::retain
is much slower than filtering and allocating a new Vector.I realize the former is probably more memory efficient, but I still found it surprising that it would be that much slower (or really slower at all).
When testing with this code:
on 1.59.0-nightly (48a5999 2021-12-01), I get these benchmark results:
on a Ryzen 5900X running Linux. Testing on a different machine (Xeon E3-1271 v3), I get similar numbers:
Vec::retain
seemed like the obvious choice to me, so it being slower is either a bug or should be documented somewhere.Godbolt
The text was updated successfully, but these errors were encountered: