Skip to content

Commit b02aa59

Browse files
authored
perf: reduce binary size by optimizing panic formatting (#536)
1 parent bdc74a9 commit b02aa59

1 file changed

Lines changed: 43 additions & 17 deletions

File tree

‎src/lib.rs‎

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ const fn is_zst<T>() -> bool {
171171
/// and thus cannot be used yet.
172172
fn slice_range<R>(range: R, bounds: core::ops::RangeTo<usize>) -> core::ops::Range<usize>
173173
where R: core::ops::RangeBounds<usize> {
174+
#[cold]
175+
#[inline(never)]
176+
#[track_caller]
177+
fn assert_failed(start: usize, end: usize, len: usize) -> ! {
178+
if start > end {
179+
panic!("slice index starts at {start} but ends at {end}");
180+
} else {
181+
panic!("range end index {end} out of range for slice of length {len}");
182+
}
183+
}
184+
174185
let len = bounds.end;
175186

176187
let start = match range.start_bound() {
@@ -189,11 +200,8 @@ where R: core::ops::RangeBounds<usize> {
189200
core::ops::Bound::Unbounded => len
190201
};
191202

192-
if start > end {
193-
panic!("slice index starts at {start} but ends at {end}");
194-
}
195-
if end > len {
196-
panic!("range end index {end} out of range for slice of length {len}");
203+
if start > end || end > len {
204+
assert_failed(start, end, len);
197205
}
198206

199207
core::ops::Range {
@@ -1370,11 +1378,17 @@ impl<T, const N: usize> SmallVec<T, N> {
13701378

13711379
#[inline]
13721380
pub fn swap_remove(&mut self, index: usize) -> T {
1381+
#[cold]
1382+
#[inline(never)]
1383+
#[track_caller]
1384+
fn assert_failed(index: usize, len: usize) -> ! {
1385+
panic!("swap_remove index (is {index}) should be < len (is {len})");
1386+
}
1387+
13731388
let len = self.len();
1374-
assert!(
1375-
index < len,
1376-
"swap_remove index (is {index}) should be < len (is {len})"
1377-
);
1389+
if index >= len {
1390+
assert_failed(index, len);
1391+
}
13781392
// This can't overflow since `len > index >= 0`
13791393
let new_len = len - 1;
13801394
unsafe {
@@ -1405,11 +1419,17 @@ impl<T, const N: usize> SmallVec<T, N> {
14051419

14061420
#[inline]
14071421
pub fn remove(&mut self, index: usize) -> T {
1422+
#[cold]
1423+
#[inline(never)]
1424+
#[track_caller]
1425+
fn assert_failed(index: usize, len: usize) -> ! {
1426+
panic!("removal index (is {index}) should be < len (is {len})");
1427+
}
1428+
14081429
let len = self.len();
1409-
assert!(
1410-
index < len,
1411-
"removal index (is {index}) should be < len (is {len})"
1412-
);
1430+
if index >= len {
1431+
assert_failed(index, len);
1432+
}
14131433
let new_len = len - 1;
14141434
unsafe {
14151435
// SAFETY: new_len < len
@@ -1431,11 +1451,17 @@ impl<T, const N: usize> SmallVec<T, N> {
14311451
#[inline]
14321452
#[must_use]
14331453
pub fn insert_mut(&mut self, index: usize, value: T) -> &mut T {
1454+
#[cold]
1455+
#[inline(never)]
1456+
#[track_caller]
1457+
fn assert_failed(index: usize, len: usize) -> ! {
1458+
panic!("insertion index (is {index}) should be <= len (is {len})");
1459+
}
1460+
14341461
let len = self.len();
1435-
assert!(
1436-
index <= len,
1437-
"insertion index (is {index}) should be <= len (is {len})"
1438-
);
1462+
if index > len {
1463+
assert_failed(index, len);
1464+
}
14391465
self.reserve(1);
14401466

14411467
// SAFETY: `index <= len <= capacity`,

0 commit comments

Comments
 (0)