Skip to content

Commit

Permalink
Speed up the fast path for assert_eq! and assert_ne!
Browse files Browse the repository at this point in the history
Currently, the panic!() calls directly borrow the value bindings. This
causes those bindings to always be initialized, i.e. they're initialized
even before the values are even compared. This causes noticeable
overhead in what should be a really cheap operation.

By performing a reborrow of the value in the call to panic!(), we allow
LLVM to optimize that code, so that the extra borrow only happens in the
error case.

We could achieve the same result by dereferencing the values passed to
panic!(), as the format machinery borrows them anyway, but this causes
assertions to fail to compile if one of the values is unsized, i.e. it
would be a breaking change.
  • Loading branch information
dotdash committed Jan 21, 2019
1 parent 4db2394 commit 5a7cd84
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/libcore/macros.rs
Expand Up @@ -45,9 +45,12 @@ macro_rules! assert_eq {
match (&$left, &$right) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`"#, left_val, right_val)
right: `{:?}`"#, &*left_val, &*right_val)
}
}
}
Expand All @@ -59,9 +62,12 @@ macro_rules! assert_eq {
match (&($left), &($right)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`: {}"#, left_val, right_val,
right: `{:?}`: {}"#, &*left_val, &*right_val,
format_args!($($arg)+))
}
}
Expand Down Expand Up @@ -96,9 +102,12 @@ macro_rules! assert_ne {
match (&$left, &$right) {
(left_val, right_val) => {
if *left_val == *right_val {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left != right)`
left: `{:?}`,
right: `{:?}`"#, left_val, right_val)
right: `{:?}`"#, &*left_val, &*right_val)
}
}
}
Expand All @@ -110,9 +119,12 @@ macro_rules! assert_ne {
match (&($left), &($right)) {
(left_val, right_val) => {
if *left_val == *right_val {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left != right)`
left: `{:?}`,
right: `{:?}`: {}"#, left_val, right_val,
right: `{:?}`: {}"#, &*left_val, &*right_val,
format_args!($($arg)+))
}
}
Expand Down

0 comments on commit 5a7cd84

Please sign in to comment.