This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Annotate malloc conditions with glibc_likely.


Hi,

Another little optimization in malloc is annotate that fastbins are
likely case. These should be fast path and given that other path 
slower it should pay off.

I also moved a short mmmaped case before long normal one to make code
more readable.

There is a discrepancy in freeing mmaped chunks, this is first done in
__libc_free and __libc_realloc. I wonder from where in __int_free mmaped
chunk comes from.


	* malloc/malloc.c (_int_malloc, _int_free): Annotate branches
	by __glibc_likely/__glibc_unlikely.


diff --git a/malloc/malloc.c b/malloc/malloc.c
index 393316e..c9d67f3 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3221,7 +3221,8 @@ _int_malloc(mstate av, size_t bytes)
     can try it without checking, which saves some time on this fast path.
   */
 
-  if ((unsigned long)(nb) <= (unsigned long)(get_max_fast ())) {
+  if (__glibc_likely((unsigned long)(nb) <= (unsigned long)(get_max_fast ()))
+     ) {
     idx = fastbin_index(nb);
     mfastbinptr* fb = &fastbin (av, idx);
     mchunkptr pp = *fb;
@@ -3711,8 +3712,8 @@ _int_free(mstate av, mchunkptr p, int have_lock)
     and used quickly in malloc.
   */
 
-  if ((unsigned long)(size) <= (unsigned long)(get_max_fast ())
-
+  if (__glibc_likely ((unsigned long)(size) <=
+		      (unsigned long)(get_max_fast ()))
 #if TRIM_FASTBINS
       /*
 	If TRIM_FASTBINS set, don't place chunks
@@ -3777,12 +3778,15 @@ _int_free(mstate av, mchunkptr p, int have_lock)
 	goto errout;
       }
   }
+  else if (__glibc_unlikely(chunk_is_mmapped(p))) {
+    munmap_chunk (p);
+  }
+  else {
 
   /*
     Consolidate other non-mmapped chunks as they arrive.
   */
 
-  else if (!chunk_is_mmapped(p)) {
     if (! have_lock) {
 #if THREAD_STATS
       if(!mutex_trylock(&av->mutex))
@@ -3929,13 +3933,6 @@ _int_free(mstate av, mchunkptr p, int have_lock)
       (void)mutex_unlock(&av->mutex);
     }
   }
-  /*
-    If the chunk was allocated via mmap, release via munmap().
-  */
-
-  else {
-    munmap_chunk (p);
-  }
 }
 
 /*


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]