Skip to content

Commit fa29099

Browse files
authored
Rollup merge of rust-lang#57815 - dotdash:asserts, r=sfackler
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.
2 parents 3f30ed9 + 5a7cd84 commit fa29099

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/libcore/macros.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ macro_rules! assert_eq {
4646
match (&$left, &$right) {
4747
(left_val, right_val) => {
4848
if !(*left_val == *right_val) {
49+
// The reborrows below are intentional. Without them, the stack slot for the
50+
// borrow is initialized even before the values are compared, leading to a
51+
// noticeable slow down.
4952
panic!(r#"assertion failed: `(left == right)`
5053
left: `{:?}`,
51-
right: `{:?}`"#, left_val, right_val)
54+
right: `{:?}`"#, &*left_val, &*right_val)
5255
}
5356
}
5457
}
@@ -60,9 +63,12 @@ macro_rules! assert_eq {
6063
match (&($left), &($right)) {
6164
(left_val, right_val) => {
6265
if !(*left_val == *right_val) {
66+
// The reborrows below are intentional. Without them, the stack slot for the
67+
// borrow is initialized even before the values are compared, leading to a
68+
// noticeable slow down.
6369
panic!(r#"assertion failed: `(left == right)`
6470
left: `{:?}`,
65-
right: `{:?}`: {}"#, left_val, right_val,
71+
right: `{:?}`: {}"#, &*left_val, &*right_val,
6672
format_args!($($arg)+))
6773
}
6874
}
@@ -97,9 +103,12 @@ macro_rules! assert_ne {
97103
match (&$left, &$right) {
98104
(left_val, right_val) => {
99105
if *left_val == *right_val {
106+
// The reborrows below are intentional. Without them, the stack slot for the
107+
// borrow is initialized even before the values are compared, leading to a
108+
// noticeable slow down.
100109
panic!(r#"assertion failed: `(left != right)`
101110
left: `{:?}`,
102-
right: `{:?}`"#, left_val, right_val)
111+
right: `{:?}`"#, &*left_val, &*right_val)
103112
}
104113
}
105114
}
@@ -111,9 +120,12 @@ macro_rules! assert_ne {
111120
match (&($left), &($right)) {
112121
(left_val, right_val) => {
113122
if *left_val == *right_val {
123+
// The reborrows below are intentional. Without them, the stack slot for the
124+
// borrow is initialized even before the values are compared, leading to a
125+
// noticeable slow down.
114126
panic!(r#"assertion failed: `(left != right)`
115127
left: `{:?}`,
116-
right: `{:?}`: {}"#, left_val, right_val,
128+
right: `{:?}`: {}"#, &*left_val, &*right_val,
117129
format_args!($($arg)+))
118130
}
119131
}

0 commit comments

Comments
 (0)