[llvm-commits] [llvm] r40100 - /llvm/trunk/include/llvm/ADT/DenseMap.h

Owen Anderson resistor at mac.com
Fri Jul 20 09:15:25 PDT 2007


Author: resistor
Date: Fri Jul 20 11:15:24 2007
New Revision: 40100

URL: http://llvm.org/viewvc/llvm-project?rev=40100&view=rev
Log:
Have DenseMap auto-shrink itself on clear().  This improves the time to optimize
403.gcc from 15.2s to 14.3s.

Modified:
    llvm/trunk/include/llvm/ADT/DenseMap.h

Modified: llvm/trunk/include/llvm/ADT/DenseMap.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/DenseMap.h?rev=40100&r1=40099&r2=40100&view=diff

==============================================================================
--- llvm/trunk/include/llvm/ADT/DenseMap.h (original)
+++ llvm/trunk/include/llvm/ADT/DenseMap.h Fri Jul 20 11:15:24 2007
@@ -90,6 +90,11 @@
   unsigned size() const { return NumEntries; }
   
   void clear() {
+    if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
+      shrink_and_clear();
+      return;
+    }
+    
     const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
     for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
       if (P->first != EmptyKey && P->first != TombstoneKey) {
@@ -101,7 +106,7 @@
     assert(NumEntries == 0 && "Node count imbalance!");
     NumTombstones = 0;
   }
-  
+
   /// count - Return true if the specified key is in the map.
   bool count(const KeyT &Val) const {
     BucketT *TheBucket;
@@ -290,6 +295,36 @@
     // Free the old table.
     delete[] (char*)OldBuckets;
   }
+  
+  void shrink_and_clear() {
+    unsigned OldNumBuckets = NumBuckets;
+    BucketT *OldBuckets = Buckets;
+    
+    // Halve the number of buckets.
+    NumBuckets >>= 1;
+    NumTombstones = 0;
+    Buckets = (BucketT*)new char[sizeof(BucketT)*NumBuckets];
+
+    // Initialize all the keys to EmptyKey.
+    const KeyT EmptyKey = getEmptyKey();
+    for (unsigned i = 0, e = NumBuckets; i != e; ++i)
+      new (&Buckets[i].first) KeyT(EmptyKey);
+
+    // Free the old buckets.
+    const KeyT TombstoneKey = getTombstoneKey();
+    for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
+      if (B->first != EmptyKey && B->first != TombstoneKey) {
+        // Free the value.
+        B->second.~ValueT();
+      }
+      B->first.~KeyT();
+    }
+    
+    // Free the old table.
+    delete[] (char*)OldBuckets;
+    
+    NumEntries = 0;
+  }
 };
 
 template<typename KeyT, typename ValueT, typename KeyInfoT>





More information about the llvm-commits mailing list