Skip to content

Commit dcbbfb6

Browse files
committed
Auto merge of #45007 - undecidabot:optimize-iter, r=bluss
Optimize comparison functions of Iterator Replaced matching on tuples which led to less performant code generation. Testing on microbenchmarks consistently showed ~1.35x improvement in performance on my machine. Fixes #44729.
2 parents 1807f27 + 3264c83 commit dcbbfb6

File tree

1 file changed

+114
-72
lines changed

1 file changed

+114
-72
lines changed

src/libcore/iter/iterator.rs

+114-72
Original file line numberDiff line numberDiff line change
@@ -2059,14 +2059,23 @@ pub trait Iterator {
20592059
let mut other = other.into_iter();
20602060

20612061
loop {
2062-
match (self.next(), other.next()) {
2063-
(None, None) => return Ordering::Equal,
2064-
(None, _ ) => return Ordering::Less,
2065-
(_ , None) => return Ordering::Greater,
2066-
(Some(x), Some(y)) => match x.cmp(&y) {
2067-
Ordering::Equal => (),
2068-
non_eq => return non_eq,
2062+
let x = match self.next() {
2063+
None => if other.next().is_none() {
2064+
return Ordering::Equal
2065+
} else {
2066+
return Ordering::Less
20692067
},
2068+
Some(val) => val,
2069+
};
2070+
2071+
let y = match other.next() {
2072+
None => return Ordering::Greater,
2073+
Some(val) => val,
2074+
};
2075+
2076+
match x.cmp(&y) {
2077+
Ordering::Equal => (),
2078+
non_eq => return non_eq,
20702079
}
20712080
}
20722081
}
@@ -2082,14 +2091,23 @@ pub trait Iterator {
20822091
let mut other = other.into_iter();
20832092

20842093
loop {
2085-
match (self.next(), other.next()) {
2086-
(None, None) => return Some(Ordering::Equal),
2087-
(None, _ ) => return Some(Ordering::Less),
2088-
(_ , None) => return Some(Ordering::Greater),
2089-
(Some(x), Some(y)) => match x.partial_cmp(&y) {
2090-
Some(Ordering::Equal) => (),
2091-
non_eq => return non_eq,
2094+
let x = match self.next() {
2095+
None => if other.next().is_none() {
2096+
return Some(Ordering::Equal)
2097+
} else {
2098+
return Some(Ordering::Less)
20922099
},
2100+
Some(val) => val,
2101+
};
2102+
2103+
let y = match other.next() {
2104+
None => return Some(Ordering::Greater),
2105+
Some(val) => val,
2106+
};
2107+
2108+
match x.partial_cmp(&y) {
2109+
Some(Ordering::Equal) => (),
2110+
non_eq => return non_eq,
20932111
}
20942112
}
20952113
}
@@ -2105,11 +2123,17 @@ pub trait Iterator {
21052123
let mut other = other.into_iter();
21062124

21072125
loop {
2108-
match (self.next(), other.next()) {
2109-
(None, None) => return true,
2110-
(None, _) | (_, None) => return false,
2111-
(Some(x), Some(y)) => if x != y { return false },
2112-
}
2126+
let x = match self.next() {
2127+
None => return other.next().is_none(),
2128+
Some(val) => val,
2129+
};
2130+
2131+
let y = match other.next() {
2132+
None => return false,
2133+
Some(val) => val,
2134+
};
2135+
2136+
if x != y { return false }
21132137
}
21142138
}
21152139

@@ -2124,11 +2148,17 @@ pub trait Iterator {
21242148
let mut other = other.into_iter();
21252149

21262150
loop {
2127-
match (self.next(), other.next()) {
2128-
(None, None) => return false,
2129-
(None, _) | (_, None) => return true,
2130-
(Some(x), Some(y)) => if x.ne(&y) { return true },
2131-
}
2151+
let x = match self.next() {
2152+
None => return other.next().is_some(),
2153+
Some(val) => val,
2154+
};
2155+
2156+
let y = match other.next() {
2157+
None => return true,
2158+
Some(val) => val,
2159+
};
2160+
2161+
if x != y { return true }
21322162
}
21332163
}
21342164

@@ -2143,18 +2173,21 @@ pub trait Iterator {
21432173
let mut other = other.into_iter();
21442174

21452175
loop {
2146-
match (self.next(), other.next()) {
2147-
(None, None) => return false,
2148-
(None, _ ) => return true,
2149-
(_ , None) => return false,
2150-
(Some(x), Some(y)) => {
2151-
match x.partial_cmp(&y) {
2152-
Some(Ordering::Less) => return true,
2153-
Some(Ordering::Equal) => {}
2154-
Some(Ordering::Greater) => return false,
2155-
None => return false,
2156-
}
2157-
},
2176+
let x = match self.next() {
2177+
None => return other.next().is_some(),
2178+
Some(val) => val,
2179+
};
2180+
2181+
let y = match other.next() {
2182+
None => return false,
2183+
Some(val) => val,
2184+
};
2185+
2186+
match x.partial_cmp(&y) {
2187+
Some(Ordering::Less) => return true,
2188+
Some(Ordering::Equal) => (),
2189+
Some(Ordering::Greater) => return false,
2190+
None => return false,
21582191
}
21592192
}
21602193
}
@@ -2170,18 +2203,21 @@ pub trait Iterator {
21702203
let mut other = other.into_iter();
21712204

21722205
loop {
2173-
match (self.next(), other.next()) {
2174-
(None, None) => return true,
2175-
(None, _ ) => return true,
2176-
(_ , None) => return false,
2177-
(Some(x), Some(y)) => {
2178-
match x.partial_cmp(&y) {
2179-
Some(Ordering::Less) => return true,
2180-
Some(Ordering::Equal) => {}
2181-
Some(Ordering::Greater) => return false,
2182-
None => return false,
2183-
}
2184-
},
2206+
let x = match self.next() {
2207+
None => { other.next(); return true; },
2208+
Some(val) => val,
2209+
};
2210+
2211+
let y = match other.next() {
2212+
None => return false,
2213+
Some(val) => val,
2214+
};
2215+
2216+
match x.partial_cmp(&y) {
2217+
Some(Ordering::Less) => return true,
2218+
Some(Ordering::Equal) => (),
2219+
Some(Ordering::Greater) => return false,
2220+
None => return false,
21852221
}
21862222
}
21872223
}
@@ -2197,18 +2233,21 @@ pub trait Iterator {
21972233
let mut other = other.into_iter();
21982234

21992235
loop {
2200-
match (self.next(), other.next()) {
2201-
(None, None) => return false,
2202-
(None, _ ) => return false,
2203-
(_ , None) => return true,
2204-
(Some(x), Some(y)) => {
2205-
match x.partial_cmp(&y) {
2206-
Some(Ordering::Less) => return false,
2207-
Some(Ordering::Equal) => {}
2208-
Some(Ordering::Greater) => return true,
2209-
None => return false,
2210-
}
2211-
}
2236+
let x = match self.next() {
2237+
None => { other.next(); return false; },
2238+
Some(val) => val,
2239+
};
2240+
2241+
let y = match other.next() {
2242+
None => return true,
2243+
Some(val) => val,
2244+
};
2245+
2246+
match x.partial_cmp(&y) {
2247+
Some(Ordering::Less) => return false,
2248+
Some(Ordering::Equal) => (),
2249+
Some(Ordering::Greater) => return true,
2250+
None => return false,
22122251
}
22132252
}
22142253
}
@@ -2224,18 +2263,21 @@ pub trait Iterator {
22242263
let mut other = other.into_iter();
22252264

22262265
loop {
2227-
match (self.next(), other.next()) {
2228-
(None, None) => return true,
2229-
(None, _ ) => return false,
2230-
(_ , None) => return true,
2231-
(Some(x), Some(y)) => {
2232-
match x.partial_cmp(&y) {
2233-
Some(Ordering::Less) => return false,
2234-
Some(Ordering::Equal) => {}
2235-
Some(Ordering::Greater) => return true,
2236-
None => return false,
2237-
}
2238-
},
2266+
let x = match self.next() {
2267+
None => return other.next().is_none(),
2268+
Some(val) => val,
2269+
};
2270+
2271+
let y = match other.next() {
2272+
None => return true,
2273+
Some(val) => val,
2274+
};
2275+
2276+
match x.partial_cmp(&y) {
2277+
Some(Ordering::Less) => return false,
2278+
Some(Ordering::Equal) => (),
2279+
Some(Ordering::Greater) => return true,
2280+
None => return false,
22392281
}
22402282
}
22412283
}

0 commit comments

Comments
 (0)