Optimising `assert_eq` & `assert_ne`

TL;DR: Given Kevin’s valuable feedback, and once the compiler team fixes one optimizer regression, this seems to become a clear winner.

For running BorrowSanitizer on an assert_eq heavy test in Stringlet, I had to optimise it a lot. (I joined several into a single call assert_eq!((a1, a2, …), (b1, b2, …))).

While trying to figure this out, VS code RA expand macro showed a pretty horrible, aka stemming from a time long before let-chains, implementation. I suggest the following solution, which in casual benchmarking compiles a multitude of these noticeably faster.

Not sure if short-circuiting idents (to be more general, I tried path, but that had weird effects) improves this much. If so, a mirror of the 2nd branch would be needed. This is just a sketch, so I don’t show the variants with message.

Edit: fixed version of this 3 days below:

macro_rules! assert_eq {
    ($a:ident, $b:ident) => {
        assert!($s == $b);
    };
    ($a:ident, $b:expr) => {
        if let b = &$b && !($a == *b) {
            panic!("ne");
        }
    };
    ($a:expr, $b:expr) => {
        if let (a, b) = &($a, $b) && !(*a == *b) {
            panic!("ne");
        }
    };
}

As I’m short on disk space, I don’t propose to create such a PR myself. I could flesh this out and just send the code half tested.

Have you tested that it is still faster when including the values in the assertion failure messages? If not, the difference might be in the presence or absence of formatting rather than the style of binding.

Also, if either of $a or $b is a constant, not a variable, adding formatting will have to evaluate the constant twice. That might or might not be an acceptable behavior change.

If you want, you can still create a PR and let PR CI do the testing for you. This is, of course, slow and awkward, but it’s possible.

I did have constants, as assert_eq!(black_box(C1), black_box(C1)). But if we worry about double evaling consts, it would mean i only use the 3rd form with message.

Edit: you made me think that this could likewise be optimised for literals. But until we get some flexibility like $a:(ident|literal), which I find myself frequently wishing for, this would cause a small combinatoric explosion.

I can experiment more to see if this has a discernible effect. But my measurements are very coarse. I had the std version (with rustc reading and writing mem-files) processing such calls 150 times clock in at 500-600ms, with an outlier at 477ms. While my version came in 450-550ms, with an outlier at 420ms.

So I don’t know how better to measure. Thus, a few percent improvement would go under, whether to spend more time parsing macro variants or always having slightly bigger generated code…

I think you may have missed my main point. In order to make a benchmark with meaningful results, you need to write a macro that, unlike the one in your original post, prints both values on failure, exactly like assert_eq! does, rather than printing only a constant message. Otherwise you’re measuring the benefits of the lack of message, in addition to any benefits from changing from match to if let.

After doing this, you should also probably use a helper function for the failures, like assert_eq! does (the private assert_failed()); that function is there so that each assert_eq! doesn’t also need to expand a panic! macro, and so that the rarely-executed failure code is compiled out-of-line and shared between all assertions.

3 Likes

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.

The thing about optimizing compilers is that they cannot do a perfect job (it is computationally infeasible to perform superoptimization on all code). Optimizers are full of heuristics, and will always get some things wrong. There might be a way to have the compiler get this right without help, but it might also make other cases worse. So, when there is code that is used so often as to make it worthwhile to put human attention on how it is compiled, it’s not uncommon for there to be room for further tweaking. If you look through the standard library, you will find other places that say some kind of “we wrote this unusually to make it faster/smaller”. It’s worth doing that because the standard library is used by almost every program. Trying to make the compiler handle all those cases in the desired way would require a lot of heuristic tweaks and special cases that would be just as brittle as tweaking the standard library, and hurt maintainability of the compiler.

My understanding is that, because the assert_failed() function returns !, the branch calling it is automatically recognized as a cold path.

I looked up the history of the file and found that it was added to fix this issue:

It improves the consistency of the cases where assert_eq!() is called inside an expression by preventing the match scrutinee from living longer.

2 Likes

I don't think there's such a rule; rather the panic wrapper functions are typically marked #[cold].

2 Likes

Instead of one humungous function of 1000 macros, I split this up into 25 more natural functions of 40 macros each. Each function has a very different order, so that they don’t get unified before compilation. Godbolt

To be on the safe side (avoiding any daemon perturbing one run,) I ran these series of 8x100, 10 times. The results are still mixed, but only ever so lightly.

ϕ seconds w/o worst 3 w/o best 3
& worst 3
all 1000 best 4th best
_msg_old 0.411702 0.412044 0.412640 0.293130 0.304502
_msg 0.418178 0.418548 0.419028 0.293763 0.299631
Δ +0.016% +0.016% +0.015% +0.002% -0.016%
_old 0.208479 0.208700 0.208967 0.134058 0.136964
_ 0.201564 0.201759 0.202058 0.136308 0.137547
Δ -0.033% -0.033% -0.033% +0.017% +0.004%
-O_msg_old 0.817969 0.818573 0.819841 0.616232 0.622958
-O_msg 0.834081 0.834737 0.835399 0.611030 0.629091
Δ +0.020% +0.020% +0.019% -0.008% +0.010%
-O_old 0.452906 0.453276 0.453830 0.326032 0.334296
-O 0.458596 0.459001 0.459529 0.320346 0.333360
Δ +0.013% +0.013% +0.013% -0.017% -0.003%