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]

Re: [PATCH] Replace divide and multiply with mask in sYSTRIm


> When profiling a simple malloc/free testcase, sYSTRIm was found to
> have a divide in it. Looking closer we are using a divide and multiply
> to round something to mp_.pagesize bytes. Since this is always a
> power of two and we have assertions in a number of places to enforce
> this, replace with a mask.
> 
> A simple testcase of malloc/free of 513 bytes on a POWER7 box
> shows a decent improvement:
> 
> baseline:	6869000 ops/sec
> patched:	8174000 ops/sec
> 
> Almost 19% faster
> 
> Anton

I missed the arena version, found when running the same test multithreaded.
Updated patch below.

Anton
--

2010-07-26  Anton Blanchard  <anton@samba.org>

	* malloc/malloc.c (sYSTRIm): Replace divide and multiply with mask.
	* malloc/arena.c (heap_trim): Replace divide and multiply with mask.

Index: glibc/malloc/malloc.c
===================================================================
--- glibc.orig/malloc/malloc.c	2010-07-26 13:42:35.470741337 +1000
+++ glibc/malloc/malloc.c	2010-07-26 13:58:29.870741493 +1000
@@ -3466,7 +3466,7 @@ static int sYSTRIm(pad, av) size_t pad; 
   top_size = chunksize(av->top);
 
   /* Release in pagesize units, keeping at least one page */
-  extra = ((top_size - pad - MINSIZE + (pagesz-1)) / pagesz - 1) * pagesz;
+  extra = (top_size - pad - MINSIZE + (pagesz-1)) & ~(pagesz-1);
 
   if (extra > 0) {

Index: glibc/malloc/arena.c
===================================================================
--- glibc.orig/malloc/arena.c	2010-07-26 17:05:22.311992028 +1000
+++ glibc/malloc/arena.c	2010-07-26 17:06:50.400741083 +1000
@@ -879,7 +879,7 @@ heap_trim(heap, pad) heap_info *heap; si
     /*check_chunk(ar_ptr, top_chunk);*/
   }
   top_size = chunksize(top_chunk);
-  extra = ((top_size - pad - MINSIZE + (pagesz-1))/pagesz - 1) * pagesz;
+  extra = (top_size - pad - MINSIZE + (pagesz-1)) & ~(pagesz-1);
   if(extra < (long)pagesz)
     return 0;
   /* Try to shrink. */


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