Skip to content

Commit bbad2b2

Browse files
committed
Improve assert_eq! and assert_ne!
It should improve compile times and reduce instruction cache use by moving the panic formatting to a monomorphised function
1 parent 5fa22fe commit bbad2b2

File tree

3 files changed

+103
-15
lines changed

3 files changed

+103
-15
lines changed

library/core/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ mod macros;
173173

174174
#[macro_use]
175175
mod internal_macros;
176+
#[doc(hidden)]
177+
#[unstable(
178+
feature = "macros_internals",
179+
reason = "macros implementation detail",
180+
issue = "none"
181+
)]
182+
pub use macros::internals as macros_internals;
176183

177184
#[path = "num/shells/int_macros.rs"]
178185
#[macro_use]

library/core/src/macros/internals.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use crate::{fmt, panic};
2+
3+
#[cold]
4+
#[doc(hidden)]
5+
#[unstable(feature = "macros_internals", reason = "macros implementation detail", issue = "none")]
6+
#[track_caller]
7+
pub fn assert_eq_failed<T, U>(left: &T, right: &U) -> !
8+
where
9+
T: fmt::Debug + ?Sized,
10+
U: fmt::Debug + ?Sized,
11+
{
12+
#[track_caller]
13+
fn inner(left: &dyn fmt::Debug, right: &dyn fmt::Debug) -> ! {
14+
panic!(
15+
r#"assertion failed: `(left == right)`
16+
left: `{:?}`,
17+
right: `{:?}`"#,
18+
left, right
19+
)
20+
}
21+
inner(&left, &right)
22+
}
23+
24+
#[cold]
25+
#[doc(hidden)]
26+
#[unstable(feature = "macros_internals", reason = "macros implementation detail", issue = "none")]
27+
#[track_caller]
28+
pub fn assert_eq_failed_args<T, U>(left: &T, right: &U, args: fmt::Arguments<'_>) -> !
29+
where
30+
T: fmt::Debug + ?Sized,
31+
U: fmt::Debug + ?Sized,
32+
{
33+
#[track_caller]
34+
fn inner(left: &dyn fmt::Debug, right: &dyn fmt::Debug, args: fmt::Arguments<'_>) -> ! {
35+
panic!(
36+
r#"assertion failed: `(left == right)`
37+
left: `{:?}`,
38+
right: `{:?}: {}`"#,
39+
left, right, args
40+
)
41+
}
42+
inner(&left, &right, args)
43+
}
44+
45+
#[cold]
46+
#[doc(hidden)]
47+
#[unstable(feature = "macros_internals", reason = "macros implementation detail", issue = "none")]
48+
#[track_caller]
49+
pub fn assert_ne_failed<T, U>(left: &T, right: &U) -> !
50+
where
51+
T: fmt::Debug + ?Sized,
52+
U: fmt::Debug + ?Sized,
53+
{
54+
#[track_caller]
55+
fn inner(left: &dyn fmt::Debug, right: &dyn fmt::Debug) -> ! {
56+
panic!(
57+
r#"assertion failed: `(left != right)`
58+
left: `{:?}`,
59+
right: `{:?}`"#,
60+
left, right
61+
)
62+
}
63+
inner(&left, &right)
64+
}
65+
66+
#[cold]
67+
#[doc(hidden)]
68+
#[unstable(feature = "macros_internals", reason = "macros implementation detail", issue = "none")]
69+
#[track_caller]
70+
pub fn assert_ne_failed_args<T, U>(left: &T, right: &U, args: fmt::Arguments<'_>) -> !
71+
where
72+
T: fmt::Debug + ?Sized,
73+
U: fmt::Debug + ?Sized,
74+
{
75+
#[track_caller]
76+
fn inner(left: &dyn fmt::Debug, right: &dyn fmt::Debug, args: fmt::Arguments<'_>) -> ! {
77+
panic!(
78+
r#"assertion failed: `(left != right)`
79+
left: `{:?}`,
80+
right: `{:?}: {}`"#,
81+
left, right, args
82+
)
83+
}
84+
inner(&left, &right, args)
85+
}

library/core/src/macros/mod.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#[unstable(feature = "macros_internals", reason = "macros implementation detail", issue = "none")]
2+
#[doc(hidden)]
3+
pub mod internals;
4+
15
#[cfg(bootstrap)]
26
#[doc(include = "panic.md")]
37
#[macro_export]
@@ -53,6 +57,7 @@ macro_rules! panic {
5357
/// ```
5458
#[macro_export]
5559
#[stable(feature = "rust1", since = "1.0.0")]
60+
#[allow_internal_unstable(macros_internals)]
5661
macro_rules! assert_eq {
5762
($left:expr, $right:expr $(,)?) => ({
5863
match (&$left, &$right) {
@@ -61,24 +66,19 @@ macro_rules! assert_eq {
6166
// The reborrows below are intentional. Without them, the stack slot for the
6267
// borrow is initialized even before the values are compared, leading to a
6368
// noticeable slow down.
64-
$crate::panic!(r#"assertion failed: `(left == right)`
65-
left: `{:?}`,
66-
right: `{:?}`"#, &*left_val, &*right_val)
69+
$crate::macros_internals::assert_eq_failed(&*left_val, &*right_val);
6770
}
6871
}
6972
}
7073
});
7174
($left:expr, $right:expr, $($arg:tt)+) => ({
72-
match (&($left), &($right)) {
75+
match (&$left, &$right) {
7376
(left_val, right_val) => {
7477
if !(*left_val == *right_val) {
7578
// The reborrows below are intentional. Without them, the stack slot for the
7679
// borrow is initialized even before the values are compared, leading to a
7780
// noticeable slow down.
78-
$crate::panic!(r#"assertion failed: `(left == right)`
79-
left: `{:?}`,
80-
right: `{:?}`: {}"#, &*left_val, &*right_val,
81-
$crate::format_args!($($arg)+))
81+
$crate::macros_internals::assert_eq_failed_args(&*left_val, &*right_val, $crate::format_args!($($arg)+));
8282
}
8383
}
8484
}
@@ -104,6 +104,7 @@ macro_rules! assert_eq {
104104
/// ```
105105
#[macro_export]
106106
#[stable(feature = "assert_ne", since = "1.13.0")]
107+
#[allow_internal_unstable(macros_internals)]
107108
macro_rules! assert_ne {
108109
($left:expr, $right:expr $(,)?) => ({
109110
match (&$left, &$right) {
@@ -112,9 +113,7 @@ macro_rules! assert_ne {
112113
// The reborrows below are intentional. Without them, the stack slot for the
113114
// borrow is initialized even before the values are compared, leading to a
114115
// noticeable slow down.
115-
$crate::panic!(r#"assertion failed: `(left != right)`
116-
left: `{:?}`,
117-
right: `{:?}`"#, &*left_val, &*right_val)
116+
$crate::macros_internals::assert_eq_failed(&*left_val, &*right_val);
118117
}
119118
}
120119
}
@@ -126,10 +125,7 @@ macro_rules! assert_ne {
126125
// The reborrows below are intentional. Without them, the stack slot for the
127126
// borrow is initialized even before the values are compared, leading to a
128127
// noticeable slow down.
129-
$crate::panic!(r#"assertion failed: `(left != right)`
130-
left: `{:?}`,
131-
right: `{:?}`: {}"#, &*left_val, &*right_val,
132-
$crate::format_args!($($arg)+))
128+
$crate::macros_internals::assert_ne_failed_args(&*left_val, &*right_val, $crate::format_args!($($arg)+));
133129
}
134130
}
135131
}

0 commit comments

Comments
 (0)