Skip to content

Commit bb8c96c

Browse files
committed
Optimize for assertion success and assertions disabled
1 parent 37e5f30 commit bb8c96c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

include/swift/Basic/Assertions.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
#include <stdint.h>
2121

22+
// Only for use in this header
23+
#if __has_builtin(__builtin_expect)
24+
#define ASSERT_UNLIKELY(expression) (__builtin_expect(!!(expression), 0))
25+
#else
26+
#define ASSERT_UNLIKELY(expression) ((expression))
27+
#endif
28+
2229
// ================================ Mandatory Asserts ================================
2330

2431
// `ASSERT(expr)`:
@@ -38,7 +45,7 @@
3845

3946
#define ASSERT(expr) \
4047
do { \
41-
if (!(expr)) { \
48+
if (ASSERT_UNLIKELY(!expr)) { \
4249
ASSERT_failure(#expr, __FILE__, __LINE__, __func__); \
4350
} \
4451
} while (0)
@@ -69,7 +76,7 @@ void ASSERT_failure(const char *expr, const char *file, int line, const char *fu
6976

7077
#define CONDITIONAL_ASSERT(expr) \
7178
do { \
72-
if (CONDITIONAL_ASSERT_enabled()) { \
79+
if (ASSERT_UNLIKELY(CONDITIONAL_ASSERT_enabled())) { \
7380
ASSERT(expr); \
7481
} \
7582
} while (0)

0 commit comments

Comments
 (0)