1efd804983
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. |
||
---|---|---|
.. | ||
arc.rs | ||
binary_heap.rs | ||
borrow.rs | ||
boxed.rs | ||
btree_set_hash.rs | ||
cow_str.rs | ||
fmt.rs | ||
heap.rs | ||
lib.rs | ||
linked_list.rs | ||
rc.rs | ||
slice.rs | ||
str.rs | ||
string.rs | ||
vec_deque.rs | ||
vec.rs |