Skip to content

Commit

Permalink
Replace assert! with intrinsics::abort()
Browse files Browse the repository at this point in the history
This shrinks the binary-size for things that should never happen and are
considred logic bugs, but allows for safer function interfaces.
  • Loading branch information
Voultapher committed Jul 29, 2023
1 parent a2bc908 commit 1620ec5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/merge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::cmp;
use core::intrinsics;
use core::mem::MaybeUninit;
use core::ptr;

Expand Down Expand Up @@ -154,7 +155,10 @@ where
F: FnMut(&T, &T) -> bool,
{
let len = v.len();
assert!(mid > 0 && mid < len && scratch.len() >= (cmp::min(mid, len - mid)));

if mid == 0 || mid >= len || scratch.len() < cmp::min(mid, len - mid) {
intrinsics::abort();
}

// SAFETY: We checked that the two slices must be non-empty and `mid` must be in bounds. The
// caller has to guarantee that Buffer `buf` must be long enough to hold a copy of the shorter
Expand Down
5 changes: 3 additions & 2 deletions src/smallsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ where
{
let len = v.len();

// Using assert here improves performance.
assert!(offset != 0 && offset <= len);
if offset == 0 || offset > len {
intrinsics::abort();
}

// Shift each element of the unsorted region v[i..] as far left as is needed to make v sorted.
for i in offset..len {
Expand Down

0 comments on commit 1620ec5

Please sign in to comment.