optimize StringMap::clear
authorChris Lattner <sabre@nondot.org>
Wed, 2 Jul 2008 05:30:45 +0000 (05:30 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 2 Jul 2008 05:30:45 +0000 (05:30 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53009 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/StringMap.h

index 7189931abb9fb6c8a2a44bcaa91599e714817b7b..6675d04f8d0790d34be96af5429430081709a828 100644 (file)
@@ -335,8 +335,16 @@ public:
 
   // clear - Empties out the StringMap
   void clear() {
-    while (!empty())
-      erase(begin());
+    if (empty()) return;
+    
+    // Zap all values, resetting the keys back to non-present (not tombstone),
+    // which is safe because we're removing all elements.
+    for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
+      if (I->Item && I->Item != getTombstoneVal()) {
+        static_cast<MapEntryTy*>(I->Item)->Destroy(Allocator);
+        I->Item = 0;
+      }
+    }
   }
 
   /// GetOrCreateValue - Look up the specified key in the table.  If a value
@@ -398,10 +406,7 @@ public:
   }
 
   ~StringMap() {
-    for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
-      if (I->Item && I->Item != getTombstoneVal())
-        static_cast<MapEntryTy*>(I->Item)->Destroy(Allocator);
-    }
+    clear();
     free(TheTable);
   }
 private: