[compiler-rt] [scudo] Separated committed and decommitted entries. (PR #100818)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 29 11:27:06 PDT 2024


================
@@ -335,56 +354,70 @@ template <typename Config> class MapAllocatorCache {
     // 10% of the requested size proved to be the optimal choice for
     // retrieving cached blocks after testing several options.
     constexpr u32 FragmentedBytesDivisor = 10;
-    bool Found = false;
     CachedBlock Entry;
     uptr EntryHeaderPos = 0;
+    uptr OptimalFitIndex = CachedBlock::InvalidEntry;
     {
       ScopedLock L(Mutex);
       CallsToRetrieve++;
       if (EntriesCount == 0)
         return false;
-      u32 OptimalFitIndex = 0;
       uptr MinDiff = UINTPTR_MAX;
-      for (u32 I = LRUHead; I != CachedBlock::InvalidEntry;
-           I = Entries[I].Next) {
-        const uptr CommitBase = Entries[I].CommitBase;
-        const uptr CommitSize = Entries[I].CommitSize;
-        const uptr AllocPos =
-            roundDown(CommitBase + CommitSize - Size, Alignment);
-        const uptr HeaderPos = AllocPos - HeadersSize;
-        if (HeaderPos > CommitBase + CommitSize)
-          continue;
-        if (HeaderPos < CommitBase ||
-            AllocPos > CommitBase + PageSize * MaxUnusedCachePages) {
-          continue;
-        }
-        Found = true;
-        const uptr Diff = HeaderPos - CommitBase;
-        // immediately use a cached block if it's size is close enough to the
-        // requested size.
-        const uptr MaxAllowedFragmentedBytes =
-            (CommitBase + CommitSize - HeaderPos) / FragmentedBytesDivisor;
-        if (Diff <= MaxAllowedFragmentedBytes) {
+      EntryListT OptimalFitListType = NONE;
+      auto FindAvailableEntry = [&](EntryListT ListType) REQUIRES(Mutex) {
+        for (uptr I = EntryLists[ListType].Head; I != CachedBlock::InvalidEntry;
+             I = Entries[I].Next) {
+          const uptr CommitBase = Entries[I].CommitBase;
+          const uptr CommitSize = Entries[I].CommitSize;
+          const uptr AllocPos =
+              roundDown(CommitBase + CommitSize - Size, Alignment);
+          const uptr HeaderPos = AllocPos - HeadersSize;
+          if (HeaderPos > CommitBase + CommitSize)
+            continue;
+          if (HeaderPos < CommitBase ||
+              AllocPos > CommitBase + PageSize * MaxUnusedCachePages)
+            continue;
+
+          const uptr Diff = HeaderPos - CommitBase;
+          // immediately use a cached block if it's size is close enough to
+          // the requested size.
+          const uptr MaxAllowedFragmentedBytes =
+              (CommitBase + CommitSize - HeaderPos) / FragmentedBytesDivisor;
+          if (Diff <= MaxAllowedFragmentedBytes) {
+            OptimalFitIndex = I;
+            EntryHeaderPos = HeaderPos;
+            OptimalFitListType = ListType;
+            return Entries[OptimalFitIndex];
+          }
+
+          // keep track of the smallest cached block
+          // that is greater than (AllocSize + HeaderSize)
+          if (Diff > MinDiff)
+            continue;
           OptimalFitIndex = I;
+          MinDiff = Diff;
+          OptimalFitListType = ListType;
           EntryHeaderPos = HeaderPos;
-          break;
         }
-        // keep track of the smallest cached block
-        // that is greater than (AllocSize + HeaderSize)
-        if (Diff > MinDiff)
-          continue;
-        OptimalFitIndex = I;
-        MinDiff = Diff;
-        EntryHeaderPos = HeaderPos;
-      }
-      if (Found) {
-        Entry = Entries[OptimalFitIndex];
-        remove(OptimalFitIndex);
+        CachedBlock FoundEntry;
+        if (OptimalFitIndex != CachedBlock::InvalidEntry)
+          FoundEntry = Entries[OptimalFitIndex];
+        return FoundEntry;
+      };
+
+      // Prioritize valid fit from COMMITTED entries over
+      // optimal fit from DECOMMITTED entries
+      Entry = FindAvailableEntry(COMMITTED);
+      if (!Entry.isValid())
+        Entry = FindAvailableEntry(DECOMMITTED);
+
+      if (!Entry.isValid()) {
+        return false;
+      } else {
+        remove(OptimalFitIndex, OptimalFitListType);
         SuccessfulRetrieves++;
       }
     }
----------------
ChiaHungDuan wrote:

nit: 
```
} // ScopedLock L(Mutex);
```

https://github.com/llvm/llvm-project/pull/100818


More information about the llvm-commits mailing list