aboutsummaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorGravatar Yash Tibrewal <yashkt@google.com>2018-04-10 17:21:03 -0700
committerGravatar Yash Tibrewal <yashkt@google.com>2018-04-10 17:21:03 -0700
commit20019ceef3f97830d83996464bbab6b3c239a721 (patch)
treef14ca59a0037235dd9396c993b672c0c57adcfc6 /include
parentcc3ae1f0c423db229343886b5c67687089869f24 (diff)
Add support for GPR_LIKELY and GPR_UNLIKELY
Diffstat (limited to 'include')
-rw-r--r--include/grpc/impl/codegen/port_platform.h10
-rw-r--r--include/grpc/support/log.h2
2 files changed, 11 insertions, 1 deletions
diff --git a/include/grpc/impl/codegen/port_platform.h b/include/grpc/impl/codegen/port_platform.h
index 819d17ce2a..66e7a13bb9 100644
--- a/include/grpc/impl/codegen/port_platform.h
+++ b/include/grpc/impl/codegen/port_platform.h
@@ -500,6 +500,16 @@ typedef unsigned __int64 uint64_t;
#endif /* __GPR_WINDOWS */
#endif /* GRPC_ALLOW_EXCEPTIONS */
+/* Use GPR_LIKELY only in cases where you are sure that a certain outcome is the
+ * most likely. Ideally, also collect performance numbers to justify the claim.
+ */
+#ifdef __GNUC__
+#define GPR_LIKELY(x) __builtin_expect((x), 1)
+#define GPR_UNLIKELY(x) __builtin_expect((x), 0)
+#else /* __GNUC__ */
+#define GPR_LIKELY(x) (x)
+#endif /* __GNUC__ */
+
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
diff --git a/include/grpc/support/log.h b/include/grpc/support/log.h
index 2236703db3..fdd963a481 100644
--- a/include/grpc/support/log.h
+++ b/include/grpc/support/log.h
@@ -91,7 +91,7 @@ GPRAPI void gpr_set_log_function(gpr_log_func func);
an exception in a higher-level language, consider returning error code. */
#define GPR_ASSERT(x) \
do { \
- if (!(x)) { \
+ if (GPR_UNLIKELY(!(x))) { \
gpr_log(GPR_ERROR, "assertion failed: %s", #x); \
abort(); \
} \