Skip to content
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

Optimize Vec::retain #81126

Merged
merged 4 commits into from
Feb 11, 2021
Merged

Optimize Vec::retain #81126

merged 4 commits into from
Feb 11, 2021

Conversation

oxalica
Copy link
Contributor

@oxalica oxalica commented Jan 17, 2021

Use copy_non_overlapping instead of swap to reduce memory writes, like what we've done in #44355 and String::retain.
#48065 already tried to do this optimization but it is reverted in #67300 due to bad codegen of DrainFilter::drop.

This PR re-implement the drop-then-move approach. I did a benchmark 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 (#52267) complains about it before.

@rust-highfive
Copy link
Collaborator

r? @kennytm

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 17, 2021
@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
configure: rust.channel         := nightly
configure: rust.debug-assertions := True
configure: llvm.assertions      := True
configure: dist.missing-tools   := True
configure: build.configure-args := ['--enable-sccache', '--disable-manage-submodu ...
configure: writing `config.toml` in current directory
configure: 
configure: run `python /checkout/x.py --help`
configure: 
---
Diff in /checkout/library/alloc/src/vec/mod.rs at line 1411:
             }
         }
 
-        let mut guard = BackshiftOnDrop {
-            v: self,
-            processed_len: 0,
-            deleted_cnt: 0,
-            original_len: len,
+        let mut guard =
+        let mut guard =
Running `"/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/rustfmt" "--config-path" "/checkout" "--edition" "2018" "--unstable-features" "--skip-children" "--check" "/checkout/library/alloc/src/vec/mod.rs"` failed.
+            BackshiftOnDrop { v: self, processed_len: 0, deleted_cnt: 0, original_len: len };
 
         let mut del = 0usize;
         for i in 0..len {
If you're running `tidy`, try again with `--bless`. Or, if you just want to format code, run `./x.py fmt` instead.
Build completed unsuccessfully in 0:00:17

@m-ou-se m-ou-se assigned m-ou-se and unassigned kennytm Jan 21, 2021
@m-ou-se m-ou-se added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jan 21, 2021
Copy link
Member

@m-ou-se m-ou-se left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

I have a few comments:

library/alloc/src/vec/mod.rs Outdated Show resolved Hide resolved
library/alloc/src/vec/mod.rs Outdated Show resolved Hide resolved
library/alloc/src/vec/mod.rs Outdated Show resolved Hide resolved
library/alloc/src/vec/mod.rs Outdated Show resolved Hide resolved
library/alloc/tests/vec.rs Outdated Show resolved Hide resolved
@oxalica
Copy link
Contributor Author

oxalica commented Jan 23, 2021

@m-ou-se Fixed. Also updated the benchmark result.

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 7, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Feb 9, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Feb 9, 2021

📌 Commit 2a11c57 has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 9, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Feb 9, 2021

Thanks for working on this!

@bors
Copy link
Contributor

bors commented Feb 11, 2021

⌛ Testing commit 2a11c57 with merge 1efd804...

@bors
Copy link
Contributor

bors commented Feb 11, 2021

☀️ Test successful - checks-actions
Approved by: m-ou-se
Pushing 1efd804 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 11, 2021
@bors bors merged commit 1efd804 into rust-lang:master Feb 11, 2021
@rustbot rustbot added this to the 1.52.0 milestone Feb 11, 2021
niklasf added a commit to niklasf/arrayvec that referenced this pull request Feb 19, 2021
bluss pushed a commit to niklasf/arrayvec that referenced this pull request Mar 29, 2021
bluss added a commit to bluss/arrayvec that referenced this pull request Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants