]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
The drastic increase in the number of per-arena freelists in r6771
authorJulian Seward <jseward@acm.org>
Thu, 23 Aug 2007 10:22:44 +0000 (10:22 +0000)
committerJulian Seward <jseward@acm.org>
Thu, 23 Aug 2007 10:22:44 +0000 (10:22 +0000)
exposes a performance problem with doing m_mallocfree.c sanity checks
(at --sanity-level=3, at least), caused by slowness in
listNo_to_pszB_min.  This commit fixes the problem by caching the
results of queries to listNo_to_pszB_min.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@6775

coregrind/m_mallocfree.c

index ceea36a06a6d1e2b8842bd34ada957ab06f1a595..6f64d7ea96daa884551b2ad800710fa5a22811c8 100644 (file)
@@ -737,10 +737,24 @@ UInt pszB_to_listNo ( SizeT pszB )
 static
 SizeT listNo_to_pszB_min ( UInt listNo )
 {
-   SizeT pszB = 0;
+   /* Repeatedly computing this function at every request is
+      expensive.  Hence at the first call just cache the result for
+      every possible argument. */
+   static SizeT cache[N_MALLOC_LISTS];
+   static Bool  cache_valid = False;
+   if (!cache_valid) {
+      UInt i;
+      for (i = 0; i < N_MALLOC_LISTS; i++) {
+         SizeT pszB = 0;
+         while (pszB_to_listNo(pszB) < i)
+            pszB += VG_MIN_MALLOC_SZB;
+         cache[i] = pszB;
+      }
+      cache_valid = True;
+   }
+   /* Returned cached answer. */
    vg_assert(listNo <= N_MALLOC_LISTS);
-   while (pszB_to_listNo(pszB) < listNo) pszB += VG_MIN_MALLOC_SZB;
-   return pszB;
+   return cache[listNo];
 }
 
 // What is the maximum payload size for a given list?