Add likely/unlikely() branch hint macros.
authorAndres Freund <andres@anarazel.de>
Sun, 3 Jul 2016 22:05:18 +0000 (15:05 -0700)
committerAndres Freund <andres@anarazel.de>
Fri, 30 Sep 2016 23:27:53 +0000 (16:27 -0700)
These are useful for hot code paths. Because it's easy to guess wrongly
about likelihood, and because such likelihoods change over time, they
should be used sparingly.

src/include/c.h

index 4ab3f8027a56362f5c2ce3c4408f6e271edb40c1..3a77107e609a4d8c94fd48b6fafcf6baa3a9524c 100644 (file)
@@ -939,6 +939,22 @@ typedef NameData *Name;
 #endif
 
 
+/*
+ * Hints to the compiler about the likelihood of a branch. Both likely() and
+ * unlikely() return the boolean value of the contained expression.
+ *
+ * These should only be used sparingly, in very hot code paths. It's very easy
+ * to mis-estimate likelihoods.
+ */
+#if __GNUC__ >= 3
+#define likely(x)  __builtin_expect((x) != 0, 1)
+#define unlikely(x)    __builtin_expect((x) != 0, 0)
+#else
+#define likely(x)  ((x) != 0)
+#define unlikely(x)    ((x) != 0)
+#endif
+
+
 /* ----------------------------------------------------------------
  *             Section 8:  random stuff
  * ----------------------------------------------------------------