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.
This commit is contained in:
Craig Barnes 2021-02-06 00:53:29 +00:00
parent 1d9cd8cc04
commit cb9c13800e

24
debug.h
View file

@ -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