From: Date: Fri, 7 Mar 2008 16:20:40 +0000 (-0500) Subject: Branch prediction optimizations. X-Git-Tag: 0.20~1^2~66 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=fb6f24da772d54e4ce975e9c7745382ede1d5cb9;p=awesomized%2Flibmemcached Branch prediction optimizations. --- diff --git a/ChangeLog b/ChangeLog index 9f77a0ef..c68ade0f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,7 @@ * MEMCACHED_HASH_MURMUR added for murmur algorithm provided. * MEMCACHED_BEHAVIOR_RETRY_TIMEOUT added to keep connecting from looping on timeout. + * gcc branch prediction optimizations 0.17 Wed Feb 27 03:33:29 PST 2008 * MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT added for connect timeout in diff --git a/lib/common.h b/lib/common.h index c126b5d1..a70e65f3 100644 --- a/lib/common.h +++ b/lib/common.h @@ -40,6 +40,19 @@ #include +#if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96) +#define __builtin_expect(x, expected_value) (x) + +#define likely(x) __builtin_expect((x),1) +#define unlikely(x) __builtin_expect((x),0) + +#else + +#define likely(x) if((x)) +#define unlikely(x) if((x)) + +#endif + #ifdef HAVE_DTRACE #define _DTRACE_VERSION 1 #else diff --git a/lib/memcached_auto.c b/lib/memcached_auto.c index 0d0fc917..a607ed66 100644 --- a/lib/memcached_auto.c +++ b/lib/memcached_auto.c @@ -11,10 +11,10 @@ static memcached_return memcached_auto(memcached_st *ptr, char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; unsigned int server_key; - if (key_length == 0) + unlikely (key_length == 0) return MEMCACHED_NO_KEY_PROVIDED; - if (ptr->hosts == NULL || ptr->number_of_hosts == 0) + unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test(&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) @@ -26,7 +26,7 @@ static memcached_return memcached_auto(memcached_st *ptr, "%s %.*s %u\r\n", verb, (int)key_length, key, offset); - if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) + unlikely (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1); diff --git a/lib/memcached_connect.c b/lib/memcached_connect.c index dc86d1e1..ab4b84f0 100644 --- a/lib/memcached_connect.c +++ b/lib/memcached_connect.c @@ -102,7 +102,7 @@ static memcached_return set_socket_options(memcached_server_st *ptr) int flags; flags= fcntl(ptr->fd, F_GETFL, 0); - if (flags != -1) + unlikely (flags != -1) { (void)fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK); } diff --git a/lib/memcached_delete.c b/lib/memcached_delete.c index b3ae560d..60e3bed4 100644 --- a/lib/memcached_delete.c +++ b/lib/memcached_delete.c @@ -20,10 +20,10 @@ memcached_return memcached_delete_by_key(memcached_st *ptr, LIBMEMCACHED_MEMCACHED_DELETE_START(); - if (key_length == 0) + unlikely (key_length == 0) return MEMCACHED_NO_KEY_PROVIDED; - if (ptr->hosts == NULL || ptr->number_of_hosts == 0) + unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; server_key= memcached_generate_hash(ptr, master_key, master_key_length); diff --git a/lib/memcached_flush.c b/lib/memcached_flush.c index fa3dd8d8..1511adbe 100644 --- a/lib/memcached_flush.c +++ b/lib/memcached_flush.c @@ -8,7 +8,7 @@ memcached_return memcached_flush(memcached_st *ptr, time_t expiration) char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; LIBMEMCACHED_MEMCACHED_FLUSH_START(); - if (ptr->number_of_hosts == 0) + unlikely (ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; for (x= 0; x < ptr->number_of_hosts; x++) diff --git a/lib/memcached_storage.c b/lib/memcached_storage.c index 6d115913..fb3e6b2b 100644 --- a/lib/memcached_storage.c +++ b/lib/memcached_storage.c @@ -60,10 +60,10 @@ static inline memcached_return memcached_send(memcached_st *ptr, WATCHPOINT_ASSERT(!(value == NULL && value_length > 0)); WATCHPOINT_ASSERT(!(value && value_length == 0)); - if (key_length == 0) + unlikely (key_length == 0) return MEMCACHED_NO_KEY_PROVIDED; - if (ptr->number_of_hosts == 0) + unlikely (ptr->number_of_hosts == 0) return MEMCACHED_NO_SERVERS; if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test(&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) diff --git a/lib/memcached_verbosity.c b/lib/memcached_verbosity.c index f599787b..7cfb073f 100644 --- a/lib/memcached_verbosity.c +++ b/lib/memcached_verbosity.c @@ -9,7 +9,7 @@ memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity) send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "verbosity %u\r\n", verbosity); - if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) + unlikely (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE) return MEMCACHED_WRITE_FAILURE; rc= MEMCACHED_SUCCESS; diff --git a/tests/test.c b/tests/test.c index 6c6bea64..998b25f1 100644 --- a/tests/test.c +++ b/tests/test.c @@ -45,6 +45,8 @@ int main(int argc, char *argv[]) if (world.create) world_ptr= world.create(); + else + world_ptr= NULL; startup_ptr= (server_startup_st *)world_ptr; servers= (memcached_server_st *)startup_ptr->servers;