Restore original implementation of Vec::retain#67300
Merged
bors merged 1 commit intorust-lang:masterfrom Dec 15, 2019
Merged
Conversation
This PR reverts rust-lang#48065, which aimed to optimize `Vec::retain` by making use of `Vec::drain_filter`. Unfortunately at that time, `drain_filter` was unsound. The soundness hole in `Vec::drain_filter` was fixed in rust-lang#61224 by guaranteeing that cleanup logic runs via a nested `Drop`, even in the event of a panic. Implementing this nested drop affects codegen (apparently?) and results in slower code. Fixes rust-lang#65970
Contributor
|
(rust_highfive has picked a reviewer for you, use r? to override) |
Contributor
|
r? @Gankra cc @rkruppe @alexcrichton |
Contributor
|
r? @rkruppe This is unfortunate, but it does seem to generate better code (I haven't measured performance but the assembly is a lot closer to what it was 1.38) and the correctness of the old implementation was never in question so I can rubber-stamp it. @bors r+ |
Collaborator
|
📌 Commit 7ea6c46 has been approved by |
Centril
added a commit
to Centril/rust
that referenced
this pull request
Dec 15, 2019
Restore original implementation of Vec::retain This PR reverts rust-lang#48065, which aimed to optimize `Vec::retain` by making use of `Vec::drain_filter`. Unfortunately at that time, `drain_filter` was unsound. The soundness hole in `Vec::drain_filter` was fixed in rust-lang#61224 by guaranteeing that cleanup logic runs via a nested `Drop`, even in the event of a panic. Implementing this nested drop affects codegen (apparently?) and results in slower code. Fixes rust-lang#65970
bors
added a commit
that referenced
this pull request
Dec 15, 2019
Rollup of 6 pull requests Successful merges: - #67255 (Remove i686-unknown-dragonfly target) - #67267 (Fix signature of `__wasilibc_find_relpath`) - #67282 (Fix example code of OpenOptions::open) - #67289 (Do not ICE on unnamed future) - #67300 (Restore original implementation of Vec::retain) - #67305 (Doc typo) Failed merges: r? @ghost
Merged
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Feb 11, 2021
Optimize Vec::retain Use `copy_non_overlapping` instead of `swap` to reduce memory writes, like what we've done in rust-lang#44355 and `String::retain`. rust-lang#48065 already tried to do this optimization but it is reverted in rust-lang#67300 due to bad codegen of `DrainFilter::drop`. This PR re-implement the drop-then-move approach. I did a [benchmark](https://gist.github.com/oxalica/3360eec9376f22533fcecff02798b698) on small-no-drop, small-need-drop, large-no-drop elements with different predicate functions. It turns out that the new implementation is >20% faster in average for almost all cases. Only 2/24 cases are slower by 3% and 5%. See the link above for more detail. I think regression in may-panic cases is due to drop-guard preventing some optimization. If it's permitted to leak elements when predicate function of element's `drop` panic, the new implementation should be almost always faster than current one. I'm not sure if we should leak on panic, since there is indeed an issue (rust-lang#52267) complains about it before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR reverts #48065, which aimed to optimize
Vec::retainby making use ofVec::drain_filter. Unfortunately at that time,drain_filterwas unsound.The soundness hole in
Vec::drain_filterwas fixed in #61224 by guaranteeing that cleanup logic runs via a nestedDrop, even in the event of a panic. Implementing this nested drop affects codegen (apparently?) and results in slower code.Fixes #65970