Skip to content

Commit

Permalink
Auto merge of rust-lang#116846 - krtab:slice_compare_no_memcmp_opt, r…
Browse files Browse the repository at this point in the history
…=the8472

A more efficient slice comparison implementation for T: !BytewiseEq

(This is a follow up PR on rust-lang#113654)

This PR changes the implementation for `[T]` slice comparison when `T: !BytewiseEq`. The previous implementation using zip was not optimized properly by the compiler, which didn't leverage the fact that both length were equal. Performance improvements are for example 20% when testing that `[Some(0_u64); 4096].as_slice() == [Some(0_u64); 4096].as_slice()`.
  • Loading branch information
bors committed Jan 9, 2024
2 parents ae9d24d + 5b041ab commit 190f4c9
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion library/core/src/slice/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@ where
return false;
}

self.iter().zip(other.iter()).all(|(x, y)| x == y)
// Implemented as explicit indexing rather
// than zipped iterators for performance reasons.
// See PR https://github.com/rust-lang/rust/pull/116846
for idx in 0..self.len() {
// bound checks are optimized away
if self[idx] != other[idx] {
return false;
}
}

true
}
}

Expand Down

0 comments on commit 190f4c9

Please sign in to comment.