[llvm] r357192 - Delay initialization of three static global maps, NFC

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 28 10:33:41 PDT 2019


Author: rnk
Date: Thu Mar 28 10:33:41 2019
New Revision: 357192

URL: http://llvm.org/viewvc/llvm-project?rev=357192&view=rev
Log:
Delay initialization of three static global maps, NFC

This avoids allocating a few KB of heap memory on startup, and instead
allocates these maps lazily. I noticed this while profiling LLD.

Modified:
    llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTableBuilder.cpp
    llvm/trunk/lib/Target/AMDGPU/AMDGPULibFunc.cpp

Modified: llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTableBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTableBuilder.cpp?rev=357192&r1=357191&r2=357192&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTableBuilder.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Native/PDBStringTableBuilder.cpp Thu Mar 28 10:33:41 2019
@@ -49,63 +49,62 @@ StringRef PDBStringTableBuilder::getStri
   return Strings.getStringForId(Id);
 }
 
-// This is a precomputed list of Buckets given the specified number of
-// strings.  Matching the reference algorithm exactly is not strictly
-// necessary for correctness, but it helps when comparing LLD's PDBs with
-// Microsoft's PDBs so as to eliminate superfluous differences.
-static std::map<uint32_t, uint32_t> StringsToBuckets = {
-    {1, 2},
-    {2, 4},
-    {4, 7},
-    {6, 11},
-    {9, 17},
-    {13, 26},
-    {20, 40},
-    {31, 61},
-    {46, 92},
-    {70, 139},
-    {105, 209},
-    {157, 314},
-    {236, 472},
-    {355, 709},
-    {532, 1064},
-    {799, 1597},
-    {1198, 2396},
-    {1798, 3595},
-    {2697, 5393},
-    {4045, 8090},
-    {6068, 12136},
-    {9103, 18205},
-    {13654, 27308},
-    {20482, 40963},
-    {30723, 61445},
-    {46084, 92168},
-    {69127, 138253},
-    {103690, 207380},
-    {155536, 311071},
-    {233304, 466607},
-    {349956, 699911},
-    {524934, 1049867},
-    {787401, 1574801},
-    {1181101, 2362202},
-    {1771652, 3543304},
-    {2657479, 5314957},
-    {3986218, 7972436},
-    {5979328, 11958655},
-    {8968992, 17937983},
-    {13453488, 26906975},
-    {20180232, 40360463},
-    {30270348, 60540695},
-    {45405522, 90811043},
-    {68108283, 136216565},
-    {102162424, 204324848},
-    {153243637, 306487273},
-    {229865455, 459730910},
-    {344798183, 689596366},
-    {517197275, 1034394550},
-    {775795913, 1551591826}};
-
 static uint32_t computeBucketCount(uint32_t NumStrings) {
+  // This is a precomputed list of Buckets given the specified number of
+  // strings.  Matching the reference algorithm exactly is not strictly
+  // necessary for correctness, but it helps when comparing LLD's PDBs with
+  // Microsoft's PDBs so as to eliminate superfluous differences.
+  static std::map<uint32_t, uint32_t> StringsToBuckets = {
+      {1, 2},
+      {2, 4},
+      {4, 7},
+      {6, 11},
+      {9, 17},
+      {13, 26},
+      {20, 40},
+      {31, 61},
+      {46, 92},
+      {70, 139},
+      {105, 209},
+      {157, 314},
+      {236, 472},
+      {355, 709},
+      {532, 1064},
+      {799, 1597},
+      {1198, 2396},
+      {1798, 3595},
+      {2697, 5393},
+      {4045, 8090},
+      {6068, 12136},
+      {9103, 18205},
+      {13654, 27308},
+      {20482, 40963},
+      {30723, 61445},
+      {46084, 92168},
+      {69127, 138253},
+      {103690, 207380},
+      {155536, 311071},
+      {233304, 466607},
+      {349956, 699911},
+      {524934, 1049867},
+      {787401, 1574801},
+      {1181101, 2362202},
+      {1771652, 3543304},
+      {2657479, 5314957},
+      {3986218, 7972436},
+      {5979328, 11958655},
+      {8968992, 17937983},
+      {13453488, 26906975},
+      {20180232, 40360463},
+      {30270348, 60540695},
+      {45405522, 90811043},
+      {68108283, 136216565},
+      {102162424, 204324848},
+      {153243637, 306487273},
+      {229865455, 459730910},
+      {344798183, 689596366},
+      {517197275, 1034394550},
+      {775795913, 1551591826}};
   auto Entry = StringsToBuckets.lower_bound(NumStrings);
   assert(Entry != StringsToBuckets.end());
   return Entry->second;

Modified: llvm/trunk/lib/Target/AMDGPU/AMDGPULibFunc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AMDGPU/AMDGPULibFunc.cpp?rev=357192&r1=357191&r2=357192&view=diff
==============================================================================
--- llvm/trunk/lib/Target/AMDGPU/AMDGPULibFunc.cpp (original)
+++ llvm/trunk/lib/Target/AMDGPU/AMDGPULibFunc.cpp Thu Mar 28 10:33:41 2019
@@ -63,6 +63,8 @@ struct ManglingRule {
    int getNumLeads() const { return (Lead[0] ? 1 : 0) + (Lead[1] ? 1 : 0); }
 
    unsigned getNumArgs() const;
+
+   static StringMap<int> buildManglingRulesMap();
 };
 
 // Information about library functions with unmangled names.
@@ -76,16 +78,7 @@ class UnmangledFuncInfo {
   // Number of entries in Table.
   static const unsigned TableSize;
 
-  // Map function name to index.
-  class NameMap : public StringMap<unsigned> {
-  public:
-    NameMap() {
-      for (unsigned I = 0; I != TableSize; ++I)
-        (*this)[Table[I].Name] = I;
-    }
-  };
-  friend class NameMap;
-  static NameMap Map;
+  static StringMap<unsigned> buildNameMap();
 
 public:
   using ID = AMDGPULibFunc::EFuncId;
@@ -101,7 +94,8 @@ public:
            static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED);
   }
   static ID toFuncId(unsigned Index) {
-    assert(Index < TableSize && "Invalid unmangled library function");
+    assert(Index < TableSize &&
+           "Invalid unmangled library function");
     return static_cast<ID>(
         Index + 1 + static_cast<unsigned>(AMDGPULibFunc::EI_LAST_MANGLED));
   }
@@ -349,18 +343,7 @@ const UnmangledFuncInfo UnmangledFuncInf
 };
 
 const unsigned UnmangledFuncInfo::TableSize =
-    sizeof(UnmangledFuncInfo::Table) / sizeof(UnmangledFuncInfo::Table[0]);
-
-UnmangledFuncInfo::NameMap UnmangledFuncInfo::Map;
-
-static const struct ManglingRulesMap : public StringMap<int> {
-  ManglingRulesMap()
-    : StringMap<int>(sizeof(manglingRules)/sizeof(manglingRules[0])) {
-    int Id = 0;
-    for (auto Rule : manglingRules)
-      insert({ Rule.Name, Id++ });
-  }
-} manglingRulesMap;
+    array_lengthof(UnmangledFuncInfo::Table);
 
 static AMDGPULibFunc::Param getRetType(AMDGPULibFunc::EFuncId id,
                                        const AMDGPULibFunc::Param (&Leads)[2]) {
@@ -568,7 +551,17 @@ static AMDGPULibFunc::ENamePrefix parseN
   return Pfx;
 }
 
+StringMap<int> ManglingRule::buildManglingRulesMap() {
+  StringMap<int> Map(array_lengthof(manglingRules));
+  int Id = 0;
+  for (auto Rule : manglingRules)
+    Map.insert({Rule.Name, Id++});
+  return Map;
+}
+
 bool AMDGPUMangledLibFunc::parseUnmangledName(StringRef FullName) {
+  static const StringMap<int> manglingRulesMap =
+      ManglingRule::buildManglingRulesMap();
   FuncId = static_cast<EFuncId>(manglingRulesMap.lookup(FullName));
   return FuncId != EI_NONE;
 }
@@ -1004,7 +997,15 @@ FunctionCallee AMDGPULibFunc::getOrInser
   return C;
 }
 
+StringMap<unsigned> UnmangledFuncInfo::buildNameMap() {
+  StringMap<unsigned> Map;
+  for (unsigned I = 0; I != TableSize; ++I)
+    Map[Table[I].Name] = I;
+  return Map;
+}
+
 bool UnmangledFuncInfo::lookup(StringRef Name, ID &Id) {
+  static const StringMap<unsigned> Map = buildNameMap();
   auto Loc = Map.find(Name);
   if (Loc != Map.end()) {
     Id = toFuncId(Loc->second);




More information about the llvm-commits mailing list