From c20a54c8e7572ef4278cb084037b5aa821195a71 Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Fri, 27 Nov 2020 11:07:55 -0800 Subject: [PATCH] Remove `__builtin_expect` prediction assistance In microbenchmarks I tried, using `__builtin_expect` to indicate which branch was likely/unlikely was never faster, and often slower, than just omitting. In GSL, this is used only for `Expects` and `Ensures` which are nearly always true, and all processors have long been great at predicting such branches well. --- include/gsl/assert | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/include/gsl/assert b/include/gsl/assert index 0cc54f6..08f7a5e 100644 --- a/include/gsl/assert +++ b/include/gsl/assert @@ -57,16 +57,6 @@ #define GSL_STRINGIFY_DETAIL(x) #x #define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x) -#if defined(__clang__) || defined(__GNUC__) -#define GSL_LIKELY(x) __builtin_expect(!!(x), 1) -#define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0) - -#else - -#define GSL_LIKELY(x) (!!(x)) -#define GSL_UNLIKELY(x) (!!(x)) -#endif // defined(__clang__) || defined(__GNUC__) - // // GSL_ASSUME(cond) // @@ -123,7 +113,7 @@ namespace details } // namespace gsl #define GSL_CONTRACT_CHECK(type, cond) \ - (GSL_LIKELY(cond) ? static_cast(0) : gsl::details::terminate()) + ((cond) ? static_cast(0) : gsl::details::terminate()) #define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond) #define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)