-
Notifications
You must be signed in to change notification settings - Fork 13k
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
performance
#91527
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1520,49 +1520,46 @@ impl<T, A: Allocator> Vec<T, A> { | |
|
||
let mut g = BackshiftOnDrop { v: self, processed_len: 0, deleted_cnt: 0, original_len }; | ||
|
||
// process_one return a bool indicates whether the processing element should be retained. | ||
#[inline(always)] | ||
fn process_one<F, T, A: Allocator, const DELETED: bool>( | ||
fn process_loop<F, T, A: Allocator, const DELETED: bool>( | ||
original_len: usize, | ||
f: &mut F, | ||
g: &mut BackshiftOnDrop<'_, T, A>, | ||
) -> bool | ||
where | ||
) where | ||
F: FnMut(&mut T) -> bool, | ||
{ | ||
// SAFETY: Unchecked element must be valid. | ||
let cur = unsafe { &mut *g.v.as_mut_ptr().add(g.processed_len) }; | ||
if !f(cur) { | ||
// Advance early to avoid double drop if `drop_in_place` panicked. | ||
g.processed_len += 1; | ||
g.deleted_cnt += 1; | ||
// SAFETY: We never touch this element again after dropped. | ||
unsafe { ptr::drop_in_place(cur) }; | ||
// We already advanced the counter. | ||
return false; | ||
} | ||
if DELETED { | ||
// SAFETY: `deleted_cnt` > 0, so the hole slot must not overlap with current element. | ||
// We use copy for move, and never touch this element again. | ||
unsafe { | ||
let hole_slot = g.v.as_mut_ptr().add(g.processed_len - g.deleted_cnt); | ||
ptr::copy_nonoverlapping(cur, hole_slot, 1); | ||
while g.processed_len != original_len { | ||
// SAFETY: Unchecked element must be valid. | ||
let cur = unsafe { &mut *g.v.as_mut_ptr().add(g.processed_len) }; | ||
if !f(cur) { | ||
// Advance early to avoid double drop if `drop_in_place` panicked. | ||
g.processed_len += 1; | ||
g.deleted_cnt += 1; | ||
// SAFETY: We never touch this element again after dropped. | ||
unsafe { ptr::drop_in_place(cur) }; | ||
// We already advanced the counter. | ||
if DELETED { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we write this more clearly? It is a quite complex, imho. Maybe this would more readable:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // SAFETY: We never touch this element again after dropped.
g.deleted_cnt += 1;
unsafe { ptr::drop_in_place(cur) };
// SAFETY: We never touch this element again after dropped.
// We already advanced the counter.
unsafe { ptr::drop_in_place(cur) }; This code block would happen on two branches and thus would have to be duplicated. Well, I guess There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really think that duplicate code is a big issue because it is very short. For me, control flow is not easy to follow but maybe it is something wrong with me and the code is OK. |
||
continue; | ||
} else { | ||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
if DELETED { | ||
// SAFETY: `deleted_cnt` > 0, so the hole slot must not overlap with current element. | ||
// We use copy for move, and never touch this element again. | ||
unsafe { | ||
let hole_slot = g.v.as_mut_ptr().add(g.processed_len - g.deleted_cnt); | ||
ptr::copy_nonoverlapping(cur, hole_slot, 1); | ||
} | ||
} | ||
g.processed_len += 1; | ||
} | ||
g.processed_len += 1; | ||
return true; | ||
} | ||
|
||
// Stage 1: Nothing was deleted. | ||
while g.processed_len != original_len { | ||
if !process_one::<F, T, A, false>(&mut f, &mut g) { | ||
break; | ||
} | ||
} | ||
process_loop::<F, T, A, false>(original_len, &mut f, &mut g); | ||
|
||
// Stage 2: Some elements were deleted. | ||
while g.processed_len != original_len { | ||
process_one::<F, T, A, true>(&mut f, &mut g); | ||
} | ||
process_loop::<F, T, A, true>(original_len, &mut f, &mut g); | ||
|
||
// All item are processed. This can be optimized to `set_len` by LLVM. | ||
drop(g); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, dropping
#[inline]
modifier should not be done before New Pass Manager stabilizes because this affects how early function would be inlined.Also,
fn drop
can have this modifier too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thing I see that
fn drop
always do amemmove
call despite the fact that almost always there is 0 items to copy (this is not true only if panic occurs).LLVM doesn't do any checks before memmove call: godbolt
Maybe condition must look like
if self.deleted_cnt > 0 && self.original_len != self.processed_len {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it relevant though? I think most of the performance comes from the loops themselves being optimized properly, not from inlining the loops into the larger function. They only do unchecked pointer arithmetic anyway, so it's not like they benefit from bounds check elimination.