Skip to content

Commit

Permalink
Rollup merge of rust-lang#57815 - dotdash:asserts, r=sfackler
Browse files Browse the repository at this point in the history
Speed up the fast path for assert_eq! and assert_ne!

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
Centril committed Feb 13, 2019
2 parents afe7741 + 5a7cd84 commit 3d4bfae
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/libcore/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,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 @@ -60,9 +63,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 @@ -97,9 +103,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 @@ -111,9 +120,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 3d4bfae

Please sign in to comment.