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

Joshua Baehring via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 19 15:41:32 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;
     }
----------------
JoshuaMBa wrote:

I think the issue is that in the case that `Entry.Time == 0` and `LastUnreleasedEntry != CachedBlock::InvalidEntry`, then the code will be exactly the same as if we were just inserting the entry at the head of the LRU list. I was thinking that maybe we can just have a function called `insertBefore()` that inserts a cache entry before a certain index. Then our code could just be
```
if (Entry.Time == 0 && LastUnreleasedEntry != CachedBlock::InvalidEntry) {
   // Inserts `Entry` right after `LastUnreleasedEntry`
   insertBefore(Entry, Entries[LastUnreleasedEntry].Next)
} else {
    // Inserts `Entry` before `LRUHead`
    insertBefore(LRUHead)
}
```
(where `insertBefore(CachedBlock::InvalidEntry)` would just insert the entry at the tail of the LRU list). Let me know what you think.

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


More information about the llvm-commits mailing list