[compiler-rt] [scudo] Calling initCache() in init() of SizeClassAllocatorLocalCache (PR #71427)

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 6 10:53:33 PST 2023


https://github.com/ChiaHungDuan created https://github.com/llvm/llvm-project/pull/71427

initCacheMaybe() will init all the size class arrays at once and it doesn't have much work to do even if it supports partial initialization. This avoids the call to initCacheMaybe in each allocate()/deallocate().

>From 6c4914fabd6c9fa84063b9bb45505b3996c0efa5 Mon Sep 17 00:00:00 2001
From: Chia-hung Duan <chiahungduan at google.com>
Date: Mon, 6 Nov 2023 18:49:15 +0000
Subject: [PATCH] [scudo] Calling initCache() in init() of
 SizeClassAllocatorLocalCache

initCacheMaybe() will init all the size class arrays at once and it
doesn't have much work to do even if it supports partial initialization.
This avoids the call to initCacheMaybe in each allocate()/deallocate().
---
 compiler-rt/lib/scudo/standalone/local_cache.h | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)

diff --git a/compiler-rt/lib/scudo/standalone/local_cache.h b/compiler-rt/lib/scudo/standalone/local_cache.h
index 6898840eb2bcba4..46d6affdc033b1f 100644
--- a/compiler-rt/lib/scudo/standalone/local_cache.h
+++ b/compiler-rt/lib/scudo/standalone/local_cache.h
@@ -28,6 +28,7 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {
     if (LIKELY(S))
       S->link(&Stats);
     Allocator = A;
+    initCache();
   }
 
   void destroy(GlobalStats *S) {
@@ -40,8 +41,6 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {
     DCHECK_LT(ClassId, NumClasses);
     PerClass *C = &PerClassArray[ClassId];
     if (C->Count == 0) {
-      initCacheMaybe(C);
-
       // Refill half of the number of max cached.
       DCHECK_GT(C->MaxCount / 2, 0U);
       if (UNLIKELY(!refill(C, ClassId, C->MaxCount / 2)))
@@ -61,9 +60,6 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {
   bool deallocate(uptr ClassId, void *P) {
     CHECK_LT(ClassId, NumClasses);
     PerClass *C = &PerClassArray[ClassId];
-    // We still have to initialize the cache in the event that the first heap
-    // operation in a thread is a deallocation.
-    initCacheMaybe(C);
 
     // If the cache is full, drain half of blocks back to the main allocator.
     const bool NeedToDrainCache = C->Count == C->MaxCount;
@@ -150,13 +146,6 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {
   LocalStats Stats;
   SizeClassAllocator *Allocator = nullptr;
 
-  ALWAYS_INLINE void initCacheMaybe(PerClass *C) {
-    if (LIKELY(C->MaxCount))
-      return;
-    initCache();
-    DCHECK_NE(C->MaxCount, 0U);
-  }
-
   NOINLINE void initCache() {
     for (uptr I = 0; I < NumClasses; I++) {
       PerClass *P = &PerClassArray[I];



More information about the llvm-commits mailing list