[PATCH] D101105: scudo: Optimize getSizeLSBByClassId() by compressing the table into an integer if possible. NFCI.

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 22 13:20:43 PDT 2021


pcc created this revision.
pcc added reviewers: eugenis, hctim, cryptoad.
pcc requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

With AndroidSizeClassMap all of the LSBs are in the range 4-6 so we
only need 2 bits of information per size class. Furthermore we have
32 size classes, which conveniently lets us fit all of the information
into a 64-bit integer. Do so if possible so that we can avoid a table
lookup entirely.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101105

Files:
  compiler-rt/lib/scudo/standalone/size_class_map.h


Index: compiler-rt/lib/scudo/standalone/size_class_map.h
===================================================================
--- compiler-rt/lib/scudo/standalone/size_class_map.h
+++ compiler-rt/lib/scudo/standalone/size_class_map.h
@@ -145,17 +145,34 @@
 
   struct LSBTable {
     constexpr LSBTable() {
+      u8 Min = 255, Max = 0;
       for (uptr I = 0; I != ClassesSize; ++I) {
         for (u8 Bit = 0; Bit != 64; ++Bit) {
           if (Config::Classes[I] & (1 << Bit)) {
             Tab[I] = Bit;
+            if (Bit < Min)
+              Min = Bit;
+            if (Bit > Max)
+              Max = Bit;
             break;
           }
         }
       }
+
+      if (Max - Min > 3 || ClassesSize > 32)
+        return;
+
+      UseCompressedFormat = true;
+      CompressedMin = Min;
+      for (uptr I = 0; I != ClassesSize; ++I)
+        CompressedValue |= u64(Tab[I] - Min) << (I * 2);
     }
 
     u8 Tab[ClassesSize] = {};
+
+    bool UseCompressedFormat = false;
+    u8 CompressedMin = 0;
+    u64 CompressedValue = 0;
   };
 
   static constexpr LSBTable LTable = {};
@@ -174,7 +191,11 @@
   }
 
   static u8 getSizeLSBByClassId(uptr ClassId) {
-    return LTable.Tab[ClassId - 1];
+    if (LTable.UseCompressedFormat)
+      return ((LTable.CompressedValue >> ((ClassId - 1) * 2)) & 3) +
+             LTable.CompressedMin;
+    else
+      return LTable.Tab[ClassId - 1];
   }
 
   static uptr getClassIdBySize(uptr Size) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101105.339767.patch
Type: text/x-patch
Size: 1450 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210422/c1168a33/attachment.bin>


More information about the llvm-commits mailing list