Import likely/unlikely from PostgreSQL.
authorTatsuo Ishii <ishii@postgresql.org>
Wed, 28 May 2025 12:19:25 +0000 (21:19 +0900)
committerTatsuo Ishii <ishii@postgresql.org>
Wed, 28 May 2025 12:19:25 +0000 (21:19 +0900)
These macros are not only useful to enhance performance (if correctly
used) but make porting codes from PostgreSQL to pgpool easier since
the macros occasionally used in the code.

Discussion: [pgpool-hackers: 4599] Porting likely/unlikely
https://www.pgpool.net/pipermail/pgpool-hackers/2025-May/004600.html

src/include/pool_type.h

index 43f29a595abff8f7883b7c6e0087a518fa7e30ff..d7cdf936a16b92682f5533c91314cb9c3a8b61ff 100644 (file)
@@ -500,4 +500,19 @@ typedef void (*pg_on_exit_callback) (int code, Datum arg);
        } while (0)
 
 
+/*
+ * 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
+
 #endif                                                 /* POOL_TYPE_H */