-
Notifications
You must be signed in to change notification settings - Fork 998
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
Simplify WeakVec
#6587
Simplify WeakVec
#6587
Conversation
459fe85
to
48cc33a
Compare
Just tested in Bevy with our previously degenerate case and things look a lot better! Don't have concrete numbers but the remaining small but observable hang we saw post-fix in 0.23 is basically eliminated. |
GitHub won't let me make suggestions on deleted lines, but: I think pub(crate) fn push(&mut self, value: Weak<T>) {
let original_capacity = self.inner.capacity();
self.inner.push(value);
if self.inner.capacity() > original_capacity {
for i in (0..self.inner.len()).rev() {
if self.inner[i].strong_count() == 0 {
self.inner.swap_remove(i);
}
}
// Make sure our spare capacity is not (much) more than
// twice the number of live elements. Leaving some spare
// capacity ensures that we won't re-scan immediately.
self.inner.shrink_to(self.inner.len() * 2);
}
} By iterating backwards, you avoid needing to do any index adjustment. This ensures that successive scans are always separated by at least as many pushes as there are elements in the set after the earlier scan. |
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'm not certain my suggestions are good, but please take a look. If not, I'll approve this.
The most expensive operation within a single scan will be dropping the |
Co-authored-by: Jim Blandy <[email protected]>
48cc33a
to
e15e69d
Compare
That's a nice solution!
And it satisfies this nicely.
It also avoids moving items from the back that haven't been checked yet (possibly having to be dropped). So it gets rid of this cliff as well:
|
One thing I changed from your proposal is instead of reallocating the whole Vec twice, we only reallocate it once. |
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.
Looks good to me!
Resolves #6580.
Follow-up to #6419.
Related: bevyengine/bevy#15893.
@DGriffin91 / @tychedelia could you give this PR a try?