Skip to content

Commit

Permalink
Auto merge of #45007 - undecidabot:optimize-iter, r=bluss
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Oct 12, 2017
2 parents 1807f27 + 3264c83 commit dcbbfb6
Showing 1 changed file with 114 additions and 72 deletions.
186 changes: 114 additions & 72 deletions src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2059,14 +2059,23 @@ pub trait Iterator {
let mut other = other.into_iter();

loop {
match (self.next(), other.next()) {
(None, None) => return Ordering::Equal,
(None, _ ) => return Ordering::Less,
(_ , None) => return Ordering::Greater,
(Some(x), Some(y)) => match x.cmp(&y) {
Ordering::Equal => (),
non_eq => return non_eq,
let x = match self.next() {
None => if other.next().is_none() {
return Ordering::Equal
} else {
return Ordering::Less
},
Some(val) => val,
};

let y = match other.next() {
None => return Ordering::Greater,
Some(val) => val,
};

match x.cmp(&y) {
Ordering::Equal => (),
non_eq => return non_eq,
}
}
}
Expand All @@ -2082,14 +2091,23 @@ pub trait Iterator {
let mut other = other.into_iter();

loop {
match (self.next(), other.next()) {
(None, None) => return Some(Ordering::Equal),
(None, _ ) => return Some(Ordering::Less),
(_ , None) => return Some(Ordering::Greater),
(Some(x), Some(y)) => match x.partial_cmp(&y) {
Some(Ordering::Equal) => (),
non_eq => return non_eq,
let x = match self.next() {
None => if other.next().is_none() {
return Some(Ordering::Equal)
} else {
return Some(Ordering::Less)
},
Some(val) => val,
};

let y = match other.next() {
None => return Some(Ordering::Greater),
Some(val) => val,
};

match x.partial_cmp(&y) {
Some(Ordering::Equal) => (),
non_eq => return non_eq,
}
}
}
Expand All @@ -2105,11 +2123,17 @@ pub trait Iterator {
let mut other = other.into_iter();

loop {
match (self.next(), other.next()) {
(None, None) => return true,
(None, _) | (_, None) => return false,
(Some(x), Some(y)) => if x != y { return false },
}
let x = match self.next() {
None => return other.next().is_none(),
Some(val) => val,
};

let y = match other.next() {
None => return false,
Some(val) => val,
};

if x != y { return false }
}
}

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

loop {
match (self.next(), other.next()) {
(None, None) => return false,
(None, _) | (_, None) => return true,
(Some(x), Some(y)) => if x.ne(&y) { return true },
}
let x = match self.next() {
None => return other.next().is_some(),
Some(val) => val,
};

let y = match other.next() {
None => return true,
Some(val) => val,
};

if x != y { return true }
}
}

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

loop {
match (self.next(), other.next()) {
(None, None) => return false,
(None, _ ) => return true,
(_ , None) => return false,
(Some(x), Some(y)) => {
match x.partial_cmp(&y) {
Some(Ordering::Less) => return true,
Some(Ordering::Equal) => {}
Some(Ordering::Greater) => return false,
None => return false,
}
},
let x = match self.next() {
None => return other.next().is_some(),
Some(val) => val,
};

let y = match other.next() {
None => return false,
Some(val) => val,
};

match x.partial_cmp(&y) {
Some(Ordering::Less) => return true,
Some(Ordering::Equal) => (),
Some(Ordering::Greater) => return false,
None => return false,
}
}
}
Expand All @@ -2170,18 +2203,21 @@ pub trait Iterator {
let mut other = other.into_iter();

loop {
match (self.next(), other.next()) {
(None, None) => return true,
(None, _ ) => return true,
(_ , None) => return false,
(Some(x), Some(y)) => {
match x.partial_cmp(&y) {
Some(Ordering::Less) => return true,
Some(Ordering::Equal) => {}
Some(Ordering::Greater) => return false,
None => return false,
}
},
let x = match self.next() {
None => { other.next(); return true; },
Some(val) => val,
};

let y = match other.next() {
None => return false,
Some(val) => val,
};

match x.partial_cmp(&y) {
Some(Ordering::Less) => return true,
Some(Ordering::Equal) => (),
Some(Ordering::Greater) => return false,
None => return false,
}
}
}
Expand All @@ -2197,18 +2233,21 @@ pub trait Iterator {
let mut other = other.into_iter();

loop {
match (self.next(), other.next()) {
(None, None) => return false,
(None, _ ) => return false,
(_ , None) => return true,
(Some(x), Some(y)) => {
match x.partial_cmp(&y) {
Some(Ordering::Less) => return false,
Some(Ordering::Equal) => {}
Some(Ordering::Greater) => return true,
None => return false,
}
}
let x = match self.next() {
None => { other.next(); return false; },
Some(val) => val,
};

let y = match other.next() {
None => return true,
Some(val) => val,
};

match x.partial_cmp(&y) {
Some(Ordering::Less) => return false,
Some(Ordering::Equal) => (),
Some(Ordering::Greater) => return true,
None => return false,
}
}
}
Expand All @@ -2224,18 +2263,21 @@ pub trait Iterator {
let mut other = other.into_iter();

loop {
match (self.next(), other.next()) {
(None, None) => return true,
(None, _ ) => return false,
(_ , None) => return true,
(Some(x), Some(y)) => {
match x.partial_cmp(&y) {
Some(Ordering::Less) => return false,
Some(Ordering::Equal) => {}
Some(Ordering::Greater) => return true,
None => return false,
}
},
let x = match self.next() {
None => return other.next().is_none(),
Some(val) => val,
};

let y = match other.next() {
None => return true,
Some(val) => val,
};

match x.partial_cmp(&y) {
Some(Ordering::Less) => return false,
Some(Ordering::Equal) => (),
Some(Ordering::Greater) => return true,
None => return false,
}
}
}
Expand Down

0 comments on commit dcbbfb6

Please sign in to comment.