Put panic code path from copy_from_slice into cold function

The previous `assert_eq` generated quite some code, which is especially
problematic when this call is inlined. This commit also slightly
improves the panic message from:

  assertion failed: `(left == right)`
    left: `3`,
   right: `2`: destination and source slices have different lengths

...to:

  source slice length (2) does not match destination slice length (3)
This commit is contained in:
Lukas Kalbertodt
2020-07-19 14:04:30 +02:00
parent 3df25ae186
commit db99f98c3e
2 changed files with 18 additions and 3 deletions

View File

@@ -2501,7 +2501,22 @@ impl<T> [T] {
where
T: Copy,
{
assert_eq!(self.len(), src.len(), "destination and source slices have different lengths");
// The panic code path was put into a cold function to not bloat the
// call site.
#[inline(never)]
#[cold]
#[track_caller]
fn len_mismatch_fail(dst_len: usize, src_len: usize) -> ! {
panic!(
"source slice length ({}) does not match destination slice length ({})",
src_len, dst_len,
);
}
if self.len() != src.len() {
len_mismatch_fail(self.len(), src.len());
}
// SAFETY: `self` is valid for `self.len()` elements by definition, and `src` was
// checked to have the same length. The slices cannot overlap because
// mutable references are exclusive.