TL;DR: Once the compiler team fixes one optimizer regression, this seems to become a clear winner.
#[cfg(not(old))]
macro_rules! assert_eq {
($left:expr, $right:expr $(,)?) => {{
if let (left_val, right_val) = (&$left, &$right) && !(*left_val == *right_val) {
let kind = $crate::panicking::AssertKind::Eq;
// 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.
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None);
}
}};
($left:expr, $right:expr, $($arg:tt)+) => {{
if let (left_val, right_val) = (&$left, &$right) && !(*left_val == *right_val) {
let kind = $crate::panicking::AssertKind::Eq;
// 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.
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
}
}};
}
I replicated the context in my crate, so it’s now a drop in replacement. I also drop in the old macro, so it doesn’t benefit from any std-magic. I generate 1000 calls, round robin over 10 different constants, where $c is just their name.
cfg_select! {
msg => assert_eq!(black_box($c), black_box($c), "a {} b {}", black_box($c), black_box($c)),
_ => assert_eq!(black_box($c), black_box($c)),
}
For all 8 combinations with and without the last 3 options, I run rustc 1.101.0-nightly (76456a688 2026-10-25) 100 times from and to RAM disk. The PC is otherwise as inactive as I can get it.
export TIMEFORMAT='%9U' # user nanosecs
time /opt/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --emit asm --edition 2024 -O --cfg msg --cfg old
The good news is in all four scenarios old and new produce identical size asm. The mixed news is that the compiler doesn’t optimize new with message faster:
| ϕ seconds |
w/o worst 3 |
w/o best 3 & worst 3 |
all 100 |
best |
4th best |
| _msg_old |
0.838827 |
0.845705 |
0.843241 |
0.602919 |
0.675757 |
| _msg |
0.803638 |
0.811803 |
0.809563 |
0.541333 |
0.596285 |
| Δ |
-4.20% |
-4.01% |
-3.99% |
-10.21% |
-11.76% |
| _old |
0.358534 |
0.363073 |
0.362178 |
0.213046 |
0.240937 |
| _ |
0.336784 |
0.339290 |
0.339542 |
0.255494 |
0.272354 |
| Δ |
-6.07% |
-6.55% |
-6.25% |
+19.92% |
+13.04% |
| -O_msg_old |
1.763747 |
1.772820 |
1.771908 |
1.392145 |
1.562478 |
| -O_msg |
1.819400 |
1.829742 |
1.828947 |
1.463535 |
1.552192 |
| Δ |
+3.16% |
+3.21% |
+3.22% |
+5.13% |
-0.66% |
| -O_old |
0.826725 |
0.832608 |
0.832530 |
0.632574 |
0.665209 |
| -O |
0.766940 |
0.771043 |
0.770704 |
0.627128 |
0.654795 |
| Δ |
-7.23% |
-7.39% |
-7.43% |
-0.86% |
-1.57% |
It raises some questions:
-
The reborrow comment is weird. Is this a special case, or should this rather be optimized in the compiler for all such cases? It should not be necessary to do such an optimisation oneself!
-
Should the body not be cold_path()?
-
Why the extra block? Do the statements not scope any temporaries?
Btw. we could also make the caller benefit from our reference capture. A 3rd branch could infer the message args as being the comparees: assert_eq!(black_box($c), black_box($c), "a {} b {}", _, _)
Edit: flipped the signs, as a negative Δ is good.