-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Use try_fold in Vec::extend_desugared
#156440
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -4006,19 +4006,23 @@ impl<T, A: Allocator> Vec<T, A> { | |
| // for item in iterator { | ||
| // self.push(item); | ||
| // } | ||
| while let Some(element) = iterator.next() { | ||
| let len = self.len(); | ||
| if len == self.capacity() { | ||
| let (lower, _) = iterator.size_hint(); | ||
| self.reserve(lower.saturating_add(1)); | ||
| } | ||
| unsafe { | ||
| ptr::write(self.as_mut_ptr().add(len), element); | ||
| // Since next() executes user code which can panic we have to bump the length | ||
| // after each step. | ||
| // NB can't overflow since we would have had to alloc the address space | ||
| self.set_len(len + 1); | ||
| let (lower, _) = iterator.size_hint(); | ||
| self.reserve(lower); | ||
| while let Err(element) = iterator.try_fold((), |(), element| { | ||
|
Member
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 think we could have an inner function for this closure so that it is not instantiated for every passed iterator The function may still want to be |
||
| if self.len < self.capacity() { | ||
|
Member
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 check the codegen for this? I'd expect it to help to make This is probably a bit incompatible with the function suggestion above without a custom struct + fnmut impl (probably not worth it).
Member
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. Is there a reason we're changing the condition from equality to |
||
| unsafe { | ||
| let end = self.as_mut_ptr().add(self.len); | ||
| ptr::write(end, element); | ||
| self.len += 1; | ||
| } | ||
| Ok(()) | ||
| } else { | ||
| Err(element) | ||
| } | ||
| }) { | ||
| let (lower, _) = iterator.size_hint(); | ||
| self.reserve(lower.saturating_add(1)); | ||
| self.push(element); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
Vec::push_within_capacitydoes the same and avoids some unsafe.