Skip to content

Commit 5d51099

Browse files
committed
Optimize empty case in Vec::retain
1 parent a152820 commit 5d51099

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

alloc/src/vec/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,12 @@ impl<T, A: Allocator> Vec<T, A> {
17111711
F: FnMut(&mut T) -> bool,
17121712
{
17131713
let original_len = self.len();
1714+
1715+
if original_len == 0 {
1716+
// Empty case: explicit return allows better optimization, vs letting compiler infer it
1717+
return;
1718+
}
1719+
17141720
// Avoid double drop if the drop guard is not executed,
17151721
// since we may make some holes during the process.
17161722
unsafe { self.set_len(0) };

0 commit comments

Comments
 (0)