Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aa3ca5e

Browse files
committed
Add likely/unlikely() branch hint macros.
These are useful for very hot code paths. Because it's easy to guess wrongly about likelihood, and because such likelihoods change over time, they should be used sparingly. Past tests have shown it'd be a good idea to use them in some places, e.g. in error checks around ereports that ERROR out, but that's work for later. Discussion: <20160727004333.r3e2k2y6fvk2ntup@alap3.anarazel.de>
1 parent 32fdf42 commit aa3ca5e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/include/c.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,22 @@ typedef NameData *Name;
939939
#endif
940940

941941

942+
/*
943+
* Hints to the compiler about the likelihood of a branch. Both likely() and
944+
* unlikely() return the boolean value of the contained expression.
945+
*
946+
* These should only be used sparingly, in very hot code paths. It's very easy
947+
* to mis-estimate likelihoods.
948+
*/
949+
#if __GNUC__ >= 3
950+
#define likely(x) __builtin_expect((x) != 0, 1)
951+
#define unlikely(x) __builtin_expect((x) != 0, 0)
952+
#else
953+
#define likely(x) ((x) != 0)
954+
#define unlikely(x) ((x) != 0)
955+
#endif
956+
957+
942958
/* ----------------------------------------------------------------
943959
* Section 8: random stuff
944960
* ----------------------------------------------------------------

0 commit comments

Comments
 (0)