From cb9c13800eeb407fef7ef37e89a28af063f1585a Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Sat, 6 Feb 2021 00:53:29 +0000 Subject: [PATCH] Fix performance regression caused by xassert() macro Before this commit, the xassert() macro still expanded to e.g.: if (unlikely(!(cond))) __builtin_unreachable(); ...even when NDEBUG was set. If `cond` happened to contain a function call that the compiler couldn't prove to be side-effect free, this call would still have to be emitted in the generated code, even when everything else could be optimized out. This commit changes xassert() to be more in-keeping with the standard assert() macro when NDEBUG is set; specifically, it causes the entire statement to expand to nothing, which thus guarantees that any function calls in the condition will be dropped, regardless of whether or not they have side-effects. --- debug.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/debug.h b/debug.h index fcc09830..ec03f87a 100644 --- a/debug.h +++ b/debug.h @@ -2,19 +2,19 @@ #include "macros.h" -#ifdef NDEBUG - #define BUG(...) UNREACHABLE() -#else - #define BUG(...) bug(__FILE__, __LINE__, __func__, __VA_ARGS__) -#endif +#define BUG(...) bug(__FILE__, __LINE__, __func__, __VA_ARGS__) -#define xassert(x) do { \ - IGNORE_WARNING("-Wtautological-compare") \ - if (unlikely(!(x))) { \ - BUG("assertion failed: '%s'", #x); \ - } \ - UNIGNORE_WARNINGS \ -} while (0) +#ifdef NDEBUG + #define xassert(x) +#else + #define xassert(x) do { \ + IGNORE_WARNING("-Wtautological-compare") \ + if (unlikely(!(x))) { \ + BUG("assertion failed: '%s'", #x); \ + } \ + UNIGNORE_WARNINGS \ + } while (0) +#endif #ifndef static_assert #if __STDC_VERSION__ >= 201112L