]> git.lizzy.rs Git - rust.git/commit - src/tools/rustfmt
Auto merge of #81126 - oxalica:retain-early-drop, r=m-ou-se
authorbors <bors@rust-lang.org>
Thu, 11 Feb 2021 04:40:57 +0000 (04:40 +0000)
committerbors <bors@rust-lang.org>
Thu, 11 Feb 2021 04:40:57 +0000 (04:40 +0000)
commit1efd8049837aec86f21da1486e8a459a7efd7806
tree8f8c3ebfed31260b12470da6431a96231e39f7aa
parent9ce7268bcfc17265bd05e4c08713d170d39618ad
parent2a11c57fb04d68c261dc14f6cf1aa3f8f14f0a2e
Auto merge of #81126 - oxalica:retain-early-drop, r=m-ou-se

Optimize Vec::retain

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](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 (#52267) complains about it before.
library/alloc/src/vec/mod.rs
library/alloc/tests/vec.rs