[compiler-rt] Reapply "[scudo] Update secondary cache time-based release logic" (PR #110391)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 21 10:41:56 PDT 2024


================
@@ -523,22 +522,37 @@ class MapAllocatorCache {
     // Cache should be populated with valid entries when not empty
     DCHECK_NE(AvailableHead, CachedBlock::InvalidEntry);
 
-    u32 FreeIndex = AvailableHead;
+    u16 FreeIndex = AvailableHead;
     AvailableHead = Entries[AvailableHead].Next;
+    Entries[FreeIndex] = Entry;
 
-    if (EntriesCount == 0) {
-      LRUTail = static_cast<u16>(FreeIndex);
+    // Check list order
+    if (EntriesCount > 1)
+      DCHECK_GE(Entries[LRUHead].Time, Entries[Entries[LRUHead].Next].Time);
+
+    // Released entry goes after LastUnreleasedEntry rather than at LRUHead
+    if (Entry.Time == 0 && LastUnreleasedEntry != CachedBlock::InvalidEntry) {
+      Entries[FreeIndex].Next = Entries[LastUnreleasedEntry].Next;
+      Entries[FreeIndex].Prev = LastUnreleasedEntry;
+      Entries[LastUnreleasedEntry].Next = FreeIndex;
+      if (LRUTail == LastUnreleasedEntry) {
+        LRUTail = FreeIndex;
+      } else {
+        Entries[Entries[FreeIndex].Next].Prev = FreeIndex;
+      }
     } else {
-      // Check list order
-      if (EntriesCount > 1)
-        DCHECK_GE(Entries[LRUHead].Time, Entries[Entries[LRUHead].Next].Time);
-      Entries[LRUHead].Prev = static_cast<u16>(FreeIndex);
+      Entries[FreeIndex].Next = LRUHead;
+      Entries[FreeIndex].Prev = CachedBlock::InvalidEntry;
+      if (EntriesCount == 0) {
+        LRUTail = FreeIndex;
+      } else {
+        Entries[LRUHead].Prev = FreeIndex;
+      }
+      LRUHead = FreeIndex;
+      if (LastUnreleasedEntry == CachedBlock::InvalidEntry)
+        LastUnreleasedEntry = FreeIndex;
     }
----------------
ChiaHungDuan wrote:

Yes, I'm thinking of the similar thing. Instead of inserting before, I may do `insert after` (to align what Scudo list supports)
```
// Insert `Cur` next to `Prev`
insert(CacheBlock &Prev, CacheBlock& Cur) { ... }
```
In the caller (i.e., in store())
```
if (Entry.Time == 0)
  insert(LastUnreleasedEntry, CurBlock);
else
  insert(LRUHead, CurBlock);
```
So every insertion needs to provide the position to insert, this will help the future replacement with Scudo list.

If you also agree with this, it's better to have another CL to do the refactor and have this later

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


More information about the llvm-commits mailing list