Skip to content

Commit 9f4d5b4

Browse files
committedJul 8, 2015
Improve PartialEq for slices
Exploiting the fact that getting the length of the slices is known, we can use a counted loop instead of iterators, which means that we only need a single counter, instead of having to increment and check one pointer for each iterator. Benchmarks comparing vectors with 100,000 elements: Before: ``` running 8 tests test eq1_u8 ... bench: 66,757 ns/iter (+/- 113) test eq2_u16 ... bench: 111,267 ns/iter (+/- 149) test eq3_u32 ... bench: 126,282 ns/iter (+/- 111) test eq4_u64 ... bench: 126,418 ns/iter (+/- 155) test ne1_u8 ... bench: 88,990 ns/iter (+/- 161) test ne2_u16 ... bench: 89,126 ns/iter (+/- 265) test ne3_u32 ... bench: 96,901 ns/iter (+/- 92) test ne4_u64 ... bench: 96,750 ns/iter (+/- 137) ``` After: ``` running 8 tests test eq1_u8 ... bench: 46,413 ns/iter (+/- 521) test eq2_u16 ... bench: 46,500 ns/iter (+/- 74) test eq3_u32 ... bench: 50,059 ns/iter (+/- 92) test eq4_u64 ... bench: 54,001 ns/iter (+/- 92) test ne1_u8 ... bench: 47,595 ns/iter (+/- 53) test ne2_u16 ... bench: 47,521 ns/iter (+/- 59) test ne3_u32 ... bench: 44,889 ns/iter (+/- 74) test ne4_u64 ... bench: 47,775 ns/iter (+/- 68) ```
1 parent 7b78538 commit 9f4d5b4

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed
 

‎src/libcore/slice.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -1459,12 +1459,30 @@ pub mod bytes {
14591459
#[stable(feature = "rust1", since = "1.0.0")]
14601460
impl<A, B> PartialEq<[B]> for [A] where A: PartialEq<B> {
14611461
fn eq(&self, other: &[B]) -> bool {
1462-
self.len() == other.len() &&
1463-
order::eq(self.iter(), other.iter())
1462+
if self.len() != other.len() {
1463+
return false;
1464+
}
1465+
1466+
for i in 0..self.len() {
1467+
if !self[i].eq(&other[i]) {
1468+
return false;
1469+
}
1470+
}
1471+
1472+
true
14641473
}
14651474
fn ne(&self, other: &[B]) -> bool {
1466-
self.len() != other.len() ||
1467-
order::ne(self.iter(), other.iter())
1475+
if self.len() != other.len() {
1476+
return true;
1477+
}
1478+
1479+
for i in 0..self.len() {
1480+
if self[i].ne(&other[i]) {
1481+
return true;
1482+
}
1483+
}
1484+
1485+
false
14681486
}
14691487
}
14701488

0 commit comments

Comments
 (0)
Please sign in to comment.