WIP: Remove a couple unneccessary jumps using likely/unlikely
authorAndres Freund <andres@anarazel.de>
Tue, 21 Oct 2014 15:31:44 +0000 (17:31 +0200)
committerAndres Freund <andres@anarazel.de>
Tue, 21 Oct 2014 15:31:44 +0000 (17:31 +0200)
src/backend/tcop/postgres.c
src/backend/utils/cache/catcache.c
src/backend/utils/mmgr/mcxt.c

index 61f17bf1e9b2468d14f87398203c1cb1d8dcc52a..f7c902f73fec96df81a74db487390f4b7baafd76 100644 (file)
@@ -411,7 +411,7 @@ SocketBackend(StringInfo inBuf)
        case 'P':               /* parse */
            doing_extended_query_message = true;
            /* these are only legal in protocol 3 */
-           if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
+           if (unlikely(PG_PROTOCOL_MAJOR(FrontendProtocol) < 3))
                ereport(FATAL,
                        (errcode(ERRCODE_PROTOCOL_VIOLATION),
                         errmsg("invalid frontend message type %d", qtype)));
@@ -423,7 +423,7 @@ SocketBackend(StringInfo inBuf)
            /* mark not-extended, so that a new error doesn't begin skip */
            doing_extended_query_message = false;
            /* only legal in protocol 3 */
-           if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
+           if (unlikely(PG_PROTOCOL_MAJOR(FrontendProtocol) < 3))
                ereport(FATAL,
                        (errcode(ERRCODE_PROTOCOL_VIOLATION),
                         errmsg("invalid frontend message type %d", qtype)));
@@ -434,7 +434,7 @@ SocketBackend(StringInfo inBuf)
        case 'f':               /* copy fail */
            doing_extended_query_message = false;
            /* these are only legal in protocol 3 */
-           if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
+           if (unlikely(PG_PROTOCOL_MAJOR(FrontendProtocol) < 3))
                ereport(FATAL,
                        (errcode(ERRCODE_PROTOCOL_VIOLATION),
                         errmsg("invalid frontend message type %d", qtype)));
@@ -4020,7 +4020,8 @@ PostgresMain(int argc, char *argv[],
                    int         numParams;
                    Oid        *paramTypes = NULL;
 
-                   forbidden_in_wal_sender(firstchar);
+                   if (unlikely(am_walsender))
+                       forbidden_in_wal_sender(firstchar);
 
                    /* Set statement_timestamp() */
                    SetCurrentStatementStartTimestamp();
@@ -4044,7 +4045,8 @@ PostgresMain(int argc, char *argv[],
                break;
 
            case 'B':           /* bind */
-               forbidden_in_wal_sender(firstchar);
+               if (unlikely(am_walsender))
+                   forbidden_in_wal_sender(firstchar);
 
                /* Set statement_timestamp() */
                SetCurrentStatementStartTimestamp();
@@ -4061,7 +4063,8 @@ PostgresMain(int argc, char *argv[],
                    const char *portal_name;
                    int         max_rows;
 
-                   forbidden_in_wal_sender(firstchar);
+                   if (unlikely(am_walsender))
+                       forbidden_in_wal_sender(firstchar);
 
                    /* Set statement_timestamp() */
                    SetCurrentStatementStartTimestamp();
@@ -4075,7 +4078,8 @@ PostgresMain(int argc, char *argv[],
                break;
 
            case 'F':           /* fastpath function call */
-               forbidden_in_wal_sender(firstchar);
+               if (unlikely(am_walsender))
+                   forbidden_in_wal_sender(firstchar);
 
                /* Set statement_timestamp() */
                SetCurrentStatementStartTimestamp();
@@ -4124,7 +4128,8 @@ PostgresMain(int argc, char *argv[],
                    int         close_type;
                    const char *close_target;
 
-                   forbidden_in_wal_sender(firstchar);
+                   if (unlikely(am_walsender))
+                       forbidden_in_wal_sender(firstchar);
 
                    close_type = pq_getmsgbyte(&input_message);
                    close_target = pq_getmsgstring(&input_message);
@@ -4168,7 +4173,8 @@ PostgresMain(int argc, char *argv[],
                    int         describe_type;
                    const char *describe_target;
 
-                   forbidden_in_wal_sender(firstchar);
+                   if (unlikely(am_walsender))
+                       forbidden_in_wal_sender(firstchar);
 
                    /* Set statement_timestamp() (needed for xact) */
                    SetCurrentStatementStartTimestamp();
index c70b6c22922c0f860be117bc9c36315954a23e9b..44d901106095718809c94f03a72a306d73555818 100644 (file)
@@ -1157,7 +1157,7 @@ SearchCatCache(CatCache *cache,
 
        ct = dlist_container(CatCTup, cache_elem, iter.cur);
 
-       if (ct->dead)
+       if (unlikely(ct->dead))
            continue;           /* ignore dead entries */
 
        if (ct->hash_value != hashValue)
index 4185a03e9ff1969704c854c4faefc2906414ad00..22dfdc1f92a293b8c4ec998d562ce0ce58406632 100644 (file)
@@ -617,7 +617,7 @@ MemoryContextAlloc(MemoryContext context, Size size)
    AssertArg(MemoryContextIsValid(context));
    AssertNotInCriticalSection(context);
 
-   if (!AllocSizeIsValid(size))
+   if (unlikely(!AllocSizeIsValid(size)))
        elog(ERROR, "invalid memory alloc request size %zu", size);
 
    context->isReset = false;
@@ -643,7 +643,7 @@ MemoryContextAllocZero(MemoryContext context, Size size)
    AssertArg(MemoryContextIsValid(context));
    AssertNotInCriticalSection(context);
 
-   if (!AllocSizeIsValid(size))
+   if (unlikely(!AllocSizeIsValid(size)))
        elog(ERROR, "invalid memory alloc request size %zu", size);
 
    context->isReset = false;
@@ -671,7 +671,7 @@ MemoryContextAllocZeroAligned(MemoryContext context, Size size)
    AssertArg(MemoryContextIsValid(context));
    AssertNotInCriticalSection(context);
 
-   if (!AllocSizeIsValid(size))
+   if (unlikely(!AllocSizeIsValid(size)))
        elog(ERROR, "invalid memory alloc request size %zu", size);
 
    context->isReset = false;
@@ -693,7 +693,7 @@ palloc(Size size)
    AssertArg(MemoryContextIsValid(CurrentMemoryContext));
    AssertNotInCriticalSection(CurrentMemoryContext);
 
-   if (!AllocSizeIsValid(size))
+   if (unlikely(!AllocSizeIsValid(size)))
        elog(ERROR, "invalid memory alloc request size %zu", size);
 
    CurrentMemoryContext->isReset = false;
@@ -713,7 +713,7 @@ palloc0(Size size)
    AssertArg(MemoryContextIsValid(CurrentMemoryContext));
    AssertNotInCriticalSection(CurrentMemoryContext);
 
-   if (!AllocSizeIsValid(size))
+   if (unlikely(!AllocSizeIsValid(size)))
        elog(ERROR, "invalid memory alloc request size %zu", size);
 
    CurrentMemoryContext->isReset = false;
@@ -765,7 +765,7 @@ repalloc(void *pointer, Size size)
    MemoryContext context;
    void       *ret;
 
-   if (!AllocSizeIsValid(size))
+   if (unlikely(!AllocSizeIsValid(size)))
        elog(ERROR, "invalid memory alloc request size %zu", size);
 
    /*
@@ -808,7 +808,7 @@ MemoryContextAllocHuge(MemoryContext context, Size size)
    AssertArg(MemoryContextIsValid(context));
    AssertNotInCriticalSection(context);
 
-   if (!AllocHugeSizeIsValid(size))
+   if (unlikely(!AllocHugeSizeIsValid(size)))
        elog(ERROR, "invalid memory alloc request size %zu", size);
 
    context->isReset = false;
@@ -830,7 +830,7 @@ repalloc_huge(void *pointer, Size size)
    MemoryContext context;
    void       *ret;
 
-   if (!AllocHugeSizeIsValid(size))
+   if (unlikely(!AllocHugeSizeIsValid(size)))
        elog(ERROR, "invalid memory alloc request size %zu", size);
 
    /*