Bug 404824: use a look-up table to avoid expensive PR_CeilingLog2
authorwtc%google.com
Sat, 19 Jan 2008 06:04:29 +0000
changeset 3907 f20a788da2cddf2987a80f0364763980c9e30fa4
parent 3905 9897fc11de28cea71add06c60ee2a5aea6bf0849
child 3908 799bd862d51b985a11f32f383a9c4b7878d68155
push idunknown
push userunknown
push dateunknown
bugs404824
Bug 404824: use a look-up table to avoid expensive PR_CeilingLog2 calculations for common cases. The patch is contributed by Steve Snyder <swsnyder@insightbb.com>. r=nelson,wtc
lib/ds/plarena.c
--- a/lib/ds/plarena.c
+++ b/lib/ds/plarena.c
@@ -106,19 +106,33 @@ static void UnlockArena( void )
 
 PR_IMPLEMENT(void) PL_InitArenaPool(
     PLArenaPool *pool, const char *name, PRUint32 size, PRUint32 align)
 {
 #if defined(XP_MAC)
 #pragma unused (name)
 #endif
 
+    /*
+     * Look-up table of PR_BITMASK(PR_CeilingLog2(align)) values for
+     * align = 1 to 32.
+     */
+    static const PRUint8 pmasks[33] = {
+         0,                                               /*  not used */
+         0, 1, 3, 3, 7, 7, 7, 7,15,15,15,15,15,15,15,15,  /*  1 ... 16 */
+        31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31}; /* 17 ... 32 */
+
     if (align == 0)
         align = PL_ARENA_DEFAULT_ALIGN;
-    pool->mask = PR_BITMASK(PR_CeilingLog2(align));
+
+    if (align < sizeof(pmasks)/sizeof(pmasks[0]))
+        pool->mask = pmasks[align];
+    else
+        pool->mask = PR_BITMASK(PR_CeilingLog2(align));
+
     pool->first.next = NULL;
     pool->first.base = pool->first.avail = pool->first.limit =
         (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1);
     pool->current = &pool->first;
     pool->arenasize = size;                                  
 #ifdef PL_ARENAMETER
     memset(&pool->stats, 0, sizeof pool->stats);
     pool->stats.name = strdup(name);