[compiler-rt] e4efa88 - [scudo] Slightly improve the handling of last block in a region

Chia-hung Duan via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 9 08:18:41 PST 2023


Author: Chia-hung Duan
Date: 2023-03-09T16:18:00Z
New Revision: e4efa885387e2df3a5e9abb3d64f232d2b4e9025

URL: https://github.com/llvm/llvm-project/commit/e4efa885387e2df3a5e9abb3d64f232d2b4e9025
DIFF: https://github.com/llvm/llvm-project/commit/e4efa885387e2df3a5e9abb3d64f232d2b4e9025.diff

LOG: [scudo] Slightly improve the handling of last block in a region

Instead of going through all those trailing blocks, just count the
number and increase the counter at once.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D145419

Added: 
    

Modified: 
    compiler-rt/lib/scudo/standalone/primary32.h
    compiler-rt/lib/scudo/standalone/release.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/primary32.h b/compiler-rt/lib/scudo/standalone/primary32.h
index 1f06214753982..5d157f0277ac7 100644
--- a/compiler-rt/lib/scudo/standalone/primary32.h
+++ b/compiler-rt/lib/scudo/standalone/primary32.h
@@ -788,8 +788,12 @@ template <typename Config> class SizeClassAllocator32 {
         continue;
 
       const uptr GroupBase = decompactGroupBase(BG.CompactPtrGroupBase);
-      uptr AllocatedGroupSize =
-          GroupBase == CurGroupBase ? Sci->CurrentRegionAllocated : GroupSize;
+      // The `GroupSize` may not be divided by `BlockSize`, which means there is
+      // an unused space at the end of Region. Exclude that space to avoid
+      // unused page map entry.
+      uptr AllocatedGroupSize = GroupBase == CurGroupBase
+                                    ? Sci->CurrentRegionAllocated
+                                    : roundDownSlow(GroupSize, BlockSize);
       if (AllocatedGroupSize == 0)
         continue;
 

diff  --git a/compiler-rt/lib/scudo/standalone/release.h b/compiler-rt/lib/scudo/standalone/release.h
index c3b0a77d45ab6..bb66a5d2c409c 100644
--- a/compiler-rt/lib/scudo/standalone/release.h
+++ b/compiler-rt/lib/scudo/standalone/release.h
@@ -498,12 +498,28 @@ struct PageReleaseContext {
           ((RegionSize / BlockSize) - 1U) * BlockSize;
       // The last block in a region may not use the entire page, we mark the
       // following "pretend" memory block(s) as free in advance.
+      //
+      //     Region Boundary
+      //         v
+      //  -----+-----------------------+
+      //       |      Last Page        | <- Rounded Region Boundary
+      //  -----+-----------------------+
+      //   |-----||- trailing blocks  -|
+      //      ^
+      //   last block
       const uptr RoundedRegionSize = roundUp(RegionSize, PageSize);
-      uptr PInRegion = LastBlockInRegion + BlockSize;
-      while (PInRegion < RoundedRegionSize) {
-        PageMap.incRange(RegionIndex, getPageIndex(PInRegion),
-                         getPageIndex(PInRegion + BlockSize - 1));
-        PInRegion += BlockSize;
+      const uptr TrailingBlockBase = LastBlockInRegion + BlockSize;
+      // Only the last page touched by the last block needs to mark the trailing
+      // blocks. If the 
diff erence between `RoundedRegionSize` and
+      // `TrailingBlockBase` is larger than a page, that implies the reported
+      // `RegionSize` may not be accurate.
+      DCHECK_LT(RoundedRegionSize - TrailingBlockBase, PageSize);
+      uptr NumTrailingBlocks =
+          roundUpSlow(RoundedRegionSize - TrailingBlockBase, BlockSize) /
+          BlockSize;
+      if (NumTrailingBlocks > 0) {
+        PageMap.incN(RegionIndex, getPageIndex(TrailingBlockBase),
+                     NumTrailingBlocks);
       }
     }
 


        


More information about the llvm-commits mailing list