]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Make our allocator slightly faster with branch hinting.
authorDale Weiler <killfieldengine@gmail.com>
Thu, 15 Aug 2013 06:09:28 +0000 (06:09 +0000)
committerDale Weiler <killfieldengine@gmail.com>
Thu, 15 Aug 2013 06:09:28 +0000 (06:09 +0000)
gmqcc.h
stat.c

diff --git a/gmqcc.h b/gmqcc.h
index 576057ac8c71e61f48d59a5d303768cbb2edc88f..3f960b8462e92c18d5cc39799467cdd43db5c408 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -149,6 +149,14 @@ GMQCC_IND_STRING(GMQCC_VERSION_PATCH) \
 #    define GMQCC_NORETURN
 #endif /*! (defined(__GNUC__) && __GNUC__ >= 2) || defined (__CLANG__) */
 
 #    define GMQCC_NORETURN
 #endif /*! (defined(__GNUC__) && __GNUC__ >= 2) || defined (__CLANG__) */
 
+#if (defined(__GNUC__)) || defined(__CLANG__)
+#   define GMQCC_LIKELY(X)   __builtin_expect((X), 1)
+#   define GMQCC_UNLIKELY(X) __builtin_expect((X), 0)
+#else
+#   define GMQCC_LIKELY(X)   (X)
+#   define GMQCC_UNLIKELY(X) (X)
+#endif
+
 #ifndef _MSC_VER
 #   include <stdint.h>
 #else
 #ifndef _MSC_VER
 #   include <stdint.h>
 #else
diff --git a/stat.c b/stat.c
index b9df032a36966f42604ab8435c8d1ab47a51e12d..170b9dd813ade51a5fce4dea3f2e0053604b70a9 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -107,7 +107,7 @@ void *stat_mem_allocate(size_t size, size_t line, const char *file) {
     stat_mem_block_t *info = (stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size);
     void             *data = (void*)(info + 1);
 
     stat_mem_block_t *info = (stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size);
     void             *data = (void*)(info + 1);
 
-    if(!info)
+    if(GMQCC_UNLIKELY(!info))
         return NULL;
 
     info->line = line;
         return NULL;
 
     info->line = line;
@@ -116,7 +116,8 @@ void *stat_mem_allocate(size_t size, size_t line, const char *file) {
     info->prev = NULL;
     info->next = stat_mem_block_root;
 
     info->prev = NULL;
     info->next = stat_mem_block_root;
 
-    if (stat_mem_block_root)
+    /* unlikely since it only happens once */
+    if (GMQCC_UNLIKELY(stat_mem_block_root != NULL))
         stat_mem_block_root->prev = info;
 
     stat_mem_block_root       = info;
         stat_mem_block_root->prev = info;
 
     stat_mem_block_root       = info;
@@ -133,7 +134,7 @@ void *stat_mem_allocate(size_t size, size_t line, const char *file) {
 void stat_mem_deallocate(void *ptr) {
     stat_mem_block_t *info = NULL;
 
 void stat_mem_deallocate(void *ptr) {
     stat_mem_block_t *info = NULL;
 
-    if (!ptr)
+    if (GMQCC_UNLIKELY(!ptr))
         return;
 
     info = ((stat_mem_block_t*)ptr - 1);
         return;
 
     info = ((stat_mem_block_t*)ptr - 1);
@@ -156,11 +157,11 @@ void *stat_mem_reallocate(void *ptr, size_t size, size_t line, const char *file)
     stat_mem_block_t *oldinfo = NULL;
     stat_mem_block_t *newinfo;
 
     stat_mem_block_t *oldinfo = NULL;
     stat_mem_block_t *newinfo;
 
-    if (!ptr)
+    if (GMQCC_UNLIKELY(!ptr))
         return stat_mem_allocate(size, line, file);
 
         return stat_mem_allocate(size, line, file);
 
-    /* stay consistent with glic */
-    if (!size) {
+    /* stay consistent with glibc */
+    if (GMQCC_UNLIKELY(!size)) {
         stat_mem_deallocate(ptr);
         return NULL;
     }
         stat_mem_deallocate(ptr);
         return NULL;
     }
@@ -168,7 +169,7 @@ void *stat_mem_reallocate(void *ptr, size_t size, size_t line, const char *file)
     oldinfo = ((stat_mem_block_t*)ptr - 1);
     newinfo = ((stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size));
 
     oldinfo = ((stat_mem_block_t*)ptr - 1);
     newinfo = ((stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size));
 
-    if (!newinfo) {
+    if (GMQCC_UNLIKELY(!newinfo)) {
         stat_mem_deallocate(ptr);
         return NULL;
     }
         stat_mem_deallocate(ptr);
         return NULL;
     }
@@ -188,7 +189,11 @@ void *stat_mem_reallocate(void *ptr, size_t size, size_t line, const char *file)
     newinfo->prev = NULL;
     newinfo->next = stat_mem_block_root;
 
     newinfo->prev = NULL;
     newinfo->next = stat_mem_block_root;
 
-    if (stat_mem_block_root)
+    /* 
+     * likely since the only time there is no root is when it's
+     * being initialized first.
+     */
+    if (GMQCC_LIKELY(stat_mem_block_root != NULL))
         stat_mem_block_root->prev = newinfo;
 
     stat_mem_block_root = newinfo;
         stat_mem_block_root->prev = newinfo;
 
     stat_mem_block_root = newinfo;