Skip to content

Commit 7b0eb10

Browse files
authored
Rollup merge of rust-lang#37230 - bluss:zip-specialization-for-map, r=alexcrichton
Expand .zip() specialization to .map() and .cloned() Implement .zip() specialization for Map and Cloned. The crucial thing for transparent specialization is that we want to preserve the potential side effects. The simplest example is that in this code snippet: `(0..6).map(f).zip((0..4).map(g)).count()` `f` will be called five times, and `g` four times. The last time for `f` is when the other iterator is at its end, so this element is unused. This side effect can be preserved without disturbing code generation for simple uses of `.map()`. The `Zip::next_back()` case is even more complicated, unfortunately.
2 parents beaa4c5 + ed50159 commit 7b0eb10

File tree

5 files changed

+186
-1
lines changed

5 files changed

+186
-1
lines changed

src/libcore/iter/mod.rs

+59
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,18 @@ impl<'a, I, T: 'a> FusedIterator for Cloned<I>
420420
where I: FusedIterator<Item=&'a T>, T: Clone
421421
{}
422422

423+
#[doc(hidden)]
424+
unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I>
425+
where I: TrustedRandomAccess<Item=&'a T>, T: Clone
426+
{
427+
unsafe fn get_unchecked(&mut self, i: usize) -> Self::Item {
428+
self.it.get_unchecked(i).clone()
429+
}
430+
431+
#[inline]
432+
fn may_have_side_effect() -> bool { true }
433+
}
434+
423435
/// An iterator that repeats endlessly.
424436
///
425437
/// This `struct` is created by the [`cycle()`] method on [`Iterator`]. See its
@@ -773,6 +785,13 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
773785
unsafe {
774786
Some((self.a.get_unchecked(i), self.b.get_unchecked(i)))
775787
}
788+
} else if A::may_have_side_effect() && self.index < self.a.len() {
789+
// match the base implementation's potential side effects
790+
unsafe {
791+
self.a.get_unchecked(self.index);
792+
}
793+
self.index += 1;
794+
None
776795
} else {
777796
None
778797
}
@@ -789,6 +808,23 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
789808
where A: DoubleEndedIterator + ExactSizeIterator,
790809
B: DoubleEndedIterator + ExactSizeIterator
791810
{
811+
// Adjust a, b to equal length
812+
if A::may_have_side_effect() {
813+
let sz = self.a.len();
814+
if sz > self.len {
815+
for _ in 0..sz - cmp::max(self.len, self.index) {
816+
self.a.next_back();
817+
}
818+
}
819+
}
820+
if B::may_have_side_effect() {
821+
let sz = self.b.len();
822+
if sz > self.len {
823+
for _ in 0..sz - self.len {
824+
self.b.next_back();
825+
}
826+
}
827+
}
792828
if self.index < self.len {
793829
self.len -= 1;
794830
let i = self.len;
@@ -814,6 +850,9 @@ unsafe impl<A, B> TrustedRandomAccess for Zip<A, B>
814850
(self.a.get_unchecked(i), self.b.get_unchecked(i))
815851
}
816852

853+
fn may_have_side_effect() -> bool {
854+
A::may_have_side_effect() || B::may_have_side_effect()
855+
}
817856
}
818857

819858
#[unstable(feature = "fused", issue = "35602")]
@@ -920,6 +959,18 @@ impl<B, I: ExactSizeIterator, F> ExactSizeIterator for Map<I, F>
920959
impl<B, I: FusedIterator, F> FusedIterator for Map<I, F>
921960
where F: FnMut(I::Item) -> B {}
922961

962+
#[doc(hidden)]
963+
unsafe impl<B, I, F> TrustedRandomAccess for Map<I, F>
964+
where I: TrustedRandomAccess,
965+
F: FnMut(I::Item) -> B,
966+
{
967+
unsafe fn get_unchecked(&mut self, i: usize) -> Self::Item {
968+
(self.f)(self.iter.get_unchecked(i))
969+
}
970+
#[inline]
971+
fn may_have_side_effect() -> bool { true }
972+
}
973+
923974
/// An iterator that filters the elements of `iter` with `predicate`.
924975
///
925976
/// This `struct` is created by the [`filter()`] method on [`Iterator`]. See its
@@ -1135,6 +1186,10 @@ unsafe impl<I> TrustedRandomAccess for Enumerate<I>
11351186
unsafe fn get_unchecked(&mut self, i: usize) -> (usize, I::Item) {
11361187
(self.count + i, self.iter.get_unchecked(i))
11371188
}
1189+
1190+
fn may_have_side_effect() -> bool {
1191+
I::may_have_side_effect()
1192+
}
11381193
}
11391194

11401195
#[unstable(feature = "fused", issue = "35602")]
@@ -1764,6 +1819,10 @@ unsafe impl<I> TrustedRandomAccess for Fuse<I>
17641819
unsafe fn get_unchecked(&mut self, i: usize) -> I::Item {
17651820
self.iter.get_unchecked(i)
17661821
}
1822+
1823+
fn may_have_side_effect() -> bool {
1824+
I::may_have_side_effect()
1825+
}
17671826
}
17681827

17691828
#[unstable(feature = "fused", issue = "35602")]

src/libcore/iter_private.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
/// # Safety
1515
///
1616
/// The iterator's .len() and size_hint() must be exact.
17+
/// `.len()` must be cheap to call.
1718
///
1819
/// .get_unchecked() must return distinct mutable references for distinct
1920
/// indices (if applicable), and must return a valid reference if index is in
2021
/// 0..self.len().
2122
#[doc(hidden)]
2223
pub unsafe trait TrustedRandomAccess : ExactSizeIterator {
2324
unsafe fn get_unchecked(&mut self, i: usize) -> Self::Item;
25+
/// Return `true` if getting an iterator element may have
26+
/// side effects. Remember to take inner iterators into account.
27+
fn may_have_side_effect() -> bool;
2428
}
25-

src/libcore/slice.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1968,11 +1968,13 @@ unsafe impl<'a, T> TrustedRandomAccess for Iter<'a, T> {
19681968
unsafe fn get_unchecked(&mut self, i: usize) -> &'a T {
19691969
&*self.ptr.offset(i as isize)
19701970
}
1971+
fn may_have_side_effect() -> bool { false }
19711972
}
19721973

19731974
#[doc(hidden)]
19741975
unsafe impl<'a, T> TrustedRandomAccess for IterMut<'a, T> {
19751976
unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut T {
19761977
&mut *self.ptr.offset(i as isize)
19771978
}
1979+
fn may_have_side_effect() -> bool { false }
19781980
}

src/test/codegen/zip.rs

+9
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@ pub fn zip_copy(xs: &[u8], ys: &mut [u8]) {
2020
*y = *x;
2121
}
2222
}
23+
24+
// CHECK-LABEL: @zip_copy_mapped
25+
#[no_mangle]
26+
pub fn zip_copy_mapped(xs: &[u8], ys: &mut [u8]) {
27+
// CHECK: memcpy
28+
for (x, y) in xs.iter().map(|&x| x).zip(ys) {
29+
*y = x;
30+
}
31+
}

src/test/run-pass/iter-zip.rs

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that .zip() specialization preserves side effects
12+
// in sideeffectful iterator adaptors.
13+
14+
use std::cell::Cell;
15+
16+
#[derive(Debug)]
17+
struct CountClone(Cell<i32>);
18+
19+
fn count_clone() -> CountClone { CountClone(Cell::new(0)) }
20+
21+
impl PartialEq<i32> for CountClone {
22+
fn eq(&self, rhs: &i32) -> bool {
23+
self.0.get() == *rhs
24+
}
25+
}
26+
27+
impl Clone for CountClone {
28+
fn clone(&self) -> Self {
29+
let ret = CountClone(self.0.clone());
30+
let n = self.0.get();
31+
self.0.set(n + 1);
32+
ret
33+
}
34+
}
35+
36+
fn test_zip_cloned_sideffectful() {
37+
let xs = [count_clone(), count_clone(), count_clone(), count_clone()];
38+
let ys = [count_clone(), count_clone()];
39+
40+
for _ in xs.iter().cloned().zip(ys.iter().cloned()) { }
41+
42+
assert_eq!(&xs, &[1, 1, 1, 0][..]);
43+
assert_eq!(&ys, &[1, 1][..]);
44+
45+
let xs = [count_clone(), count_clone()];
46+
let ys = [count_clone(), count_clone(), count_clone(), count_clone()];
47+
48+
for _ in xs.iter().cloned().zip(ys.iter().cloned()) { }
49+
50+
assert_eq!(&xs, &[1, 1][..]);
51+
assert_eq!(&ys, &[1, 1, 0, 0][..]);
52+
}
53+
54+
fn test_zip_map_sideffectful() {
55+
let mut xs = [0; 6];
56+
let mut ys = [0; 4];
57+
58+
for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) { }
59+
60+
assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]);
61+
assert_eq!(&ys, &[1, 1, 1, 1]);
62+
63+
let mut xs = [0; 4];
64+
let mut ys = [0; 6];
65+
66+
for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) { }
67+
68+
assert_eq!(&xs, &[1, 1, 1, 1]);
69+
assert_eq!(&ys, &[1, 1, 1, 1, 0, 0]);
70+
}
71+
72+
fn test_zip_map_rev_sideffectful() {
73+
let mut xs = [0; 6];
74+
let mut ys = [0; 4];
75+
76+
{
77+
let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1));
78+
it.next_back();
79+
}
80+
assert_eq!(&xs, &[0, 0, 0, 1, 1, 1]);
81+
assert_eq!(&ys, &[0, 0, 0, 1]);
82+
83+
let mut xs = [0; 6];
84+
let mut ys = [0; 4];
85+
86+
{
87+
let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1));
88+
(&mut it).take(5).count();
89+
it.next_back();
90+
}
91+
assert_eq!(&xs, &[1, 1, 1, 1, 1, 1]);
92+
assert_eq!(&ys, &[1, 1, 1, 1]);
93+
}
94+
95+
fn test_zip_nested_sideffectful() {
96+
let mut xs = [0; 6];
97+
let ys = [0; 4];
98+
99+
{
100+
// test that it has the side effect nested inside enumerate
101+
let it = xs.iter_mut().map(|x| *x = 1).enumerate().zip(&ys);
102+
it.count();
103+
}
104+
assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]);
105+
}
106+
107+
fn main() {
108+
test_zip_cloned_sideffectful();
109+
test_zip_map_sideffectful();
110+
test_zip_map_rev_sideffectful();
111+
test_zip_nested_sideffectful();
112+
}

0 commit comments

Comments
 (0)