From: Julian Seward Date: Thu, 23 Aug 2007 10:22:44 +0000 (+0000) Subject: The drastic increase in the number of per-arena freelists in r6771 X-Git-Tag: svn/VALGRIND_3_3_0~243 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3773a4d6c21aa8c081f58e557eeeeede013bfa3f;p=thirdparty%2Fvalgrind.git The drastic increase in the number of per-arena freelists in r6771 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 --- diff --git a/coregrind/m_mallocfree.c b/coregrind/m_mallocfree.c index ceea36a06a..6f64d7ea96 100644 --- a/coregrind/m_mallocfree.c +++ b/coregrind/m_mallocfree.c @@ -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?