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 unnecessary check in VecDeque::retain #88075

Merged
merged 2 commits into from
Aug 22, 2021

Conversation

Xuanwo
Copy link
Contributor

@Xuanwo Xuanwo commented Aug 16, 2021

This pr is highly inspired by #88060 which shared the same idea: we can split the for loop into stages so that we can remove unnecessary checks like del > 0.

Benchmarks

Before

test collections::vec_deque::tests::bench_retain_half_10000  ... bench:     290,125 ns/iter (+/- 8,717)
test collections::vec_deque::tests::bench_retain_odd_10000   ... bench:     291,588 ns/iter (+/- 9,621)
test collections::vec_deque::tests::bench_retain_whole_10000 ... bench:     287,426 ns/iter (+/- 9,009)

After

test collections::vec_deque::tests::bench_retain_half_10000  ... bench:     243,940 ns/iter (+/- 8,563)
test collections::vec_deque::tests::bench_retain_odd_10000   ... bench:     242,768 ns/iter (+/- 3,903)
test collections::vec_deque::tests::bench_retain_whole_10000 ... bench:     202,926 ns/iter (+/- 6,332)

Based on the current benchmark, this PR will improve the perf of VecDeque::retain by around 16%. For special cases, the improvement will be up to 30%.

Signed-off-by: Xuanwo github@xuanwo.io

Signed-off-by: Xuanwo <github@xuanwo.io>
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dtolnay (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 16, 2021
Signed-off-by: Xuanwo <github@xuanwo.io>
@TennyZhuang
Copy link
Contributor

I ran the benchmark on my Mac

sysctl -n machdep.cpu.brand_string
Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz

Before optimization:

test collections::vec_deque::tests::bench_retain_half_10000  ... bench:     397,713 ns/iter (+/- 19,098)
test collections::vec_deque::tests::bench_retain_odd_10000   ... bench:     382,615 ns/iter (+/- 17,406)
test collections::vec_deque::tests::bench_retain_whole_10000 ... bench:     386,554 ns/iter (+/- 11,450)

After optimization:

test collections::vec_deque::tests::bench_retain_half_10000  ... bench:     318,491 ns/iter (+/- 34,193)
test collections::vec_deque::tests::bench_retain_odd_10000   ... bench:     354,253 ns/iter (+/- 21,848)
test collections::vec_deque::tests::bench_retain_whole_10000 ... bench:     288,971 ns/iter (+/- 16,016)

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Nice!

@dtolnay
Copy link
Member

dtolnay commented Aug 21, 2021

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Aug 21, 2021

📌 Commit e32f4c0 has been approved by dtolnay

@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 Aug 21, 2021
@bors
Copy link
Contributor

bors commented Aug 21, 2021

⌛ Testing commit e32f4c0 with merge 9faa714...

@bors
Copy link
Contributor

bors commented Aug 22, 2021

☀️ Test successful - checks-actions
Approved by: dtolnay
Pushing 9faa714 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 22, 2021
@bors bors merged commit 9faa714 into rust-lang:master Aug 22, 2021
@rustbot rustbot added this to the 1.56.0 milestone Aug 22, 2021
@Xuanwo Xuanwo deleted the vec_deque_retain branch August 22, 2021 04:28
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants