From 5a7cd848f740291f94f2400b50f41136fc8657bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Mon, 21 Jan 2019 19:36:27 +0100 Subject: [PATCH] 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. --- src/libcore/macros.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 2f350df2f5c18..5eaefb7d1cc51 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -45,9 +45,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) } } } @@ -59,9 +62,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)+)) } } @@ -96,9 +102,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) } } } @@ -110,9 +119,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)+)) } }