-
Notifications
You must be signed in to change notification settings - Fork 14k
Added [T; N]::zip() #79451
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
Added [T; N]::zip() #79451
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 |
|---|---|---|
|
|
@@ -463,6 +463,37 @@ impl<T, const N: usize> [T; N] { | |
| unsafe { crate::mem::transmute_copy::<_, [U; N]>(&dst) } | ||
| } | ||
|
|
||
| /// 'Zips up' two arrays into a single array of pairs. | ||
| /// | ||
| /// `zip()` returns a new array where every element is a tuple where the | ||
| /// first element comes from the first array, and the second element comes | ||
| /// from the second array. In other words, it zips two arrays together, | ||
| /// into a single one. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// #![feature(array_zip)] | ||
| /// let x = [1, 2, 3]; | ||
| /// let y = [4, 5, 6]; | ||
| /// let z = x.zip(y); | ||
| /// assert_eq!(z, [(1, 4), (2, 5), (3, 6)]); | ||
| /// ``` | ||
| #[unstable(feature = "array_zip", issue = "80094")] | ||
| pub fn zip<U>(self, rhs: [U; N]) -> [(T, U); N] { | ||
| use crate::mem::MaybeUninit; | ||
|
|
||
| let mut dst = MaybeUninit::uninit_array::<N>(); | ||
| for (i, (lhs, rhs)) in IntoIter::new(self).zip(IntoIter::new(rhs)).enumerate() { | ||
| dst[i].write((lhs, rhs)); | ||
| } | ||
| // FIXME: Convert to crate::mem::transmute once it works with generics. | ||
| // unsafe { crate::mem::transmute::<[MaybeUninit<U>; N], [U; N]>(dst) } | ||
| // SAFETY: At this point we've properly initialized the whole array | ||
| // and we just need to cast it to the correct type. | ||
| unsafe { crate::mem::transmute_copy::<_, [(T, U); N]>(&dst) } | ||
|
Contributor
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. This implementation (x86-64 codegen with -O) uses more stack space and includes branches/a loop compared to this one (which is branch-free and utilizes less stack, at least for smaller sizes): let mut dst = MaybeUninit::<[(T, U); N]>::uninit();
let ptr = dst.as_mut_ptr() as *mut (T, U);
for (idx, (lhs, rhs)) in IntoIter::new(lhs).zip(IntoIter::new(rhs)).enumerate() {
unsafe { ptr.add(idx).write((lhs, rhs)) }
}
unsafe { dst.assume_init() }
Contributor
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. However, that codegen is still not optimal (the memcpy is basically redundant and
Contributor
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. Are
Contributor
Author
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 am quite new to working with unsafe but the doc states
and there are examples with
Contributor
Author
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. Looking at
Contributor
Author
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.
Contributor
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. ah ty for the update! The asm just helps me see what differences there are between the two versions, mainly looking for bounds checks which shouldn't be necessary here. The version you have has a loop in the LBB1 block, whereas cynecx's has no loop and is unrolled as he mentions in his top comment. If you increase the size of the arrays, you'll start to see more differences between the two. The one thing is, if you change into both versions output the same asm. So the main difference between the two is here if you choose to zip or enumerate, rather than whether you use a pointer or a
Contributor
Author
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. Then would that be the preferred implementation (zip3 on godbolt)? Seems to me like best of both worlds, no extra
Contributor
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. To me that seems good, but I think that's ultimately up to you & the reviewer! I was just curious how each impl compared to the others.
Contributor
Author
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. Do you all think |
||
| } | ||
|
|
||
| /// Returns a slice containing the entire array. Equivalent to `&s[..]`. | ||
| #[unstable(feature = "array_methods", issue = "76118")] | ||
| pub fn as_slice(&self) -> &[T] { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.