[cfe-commits] r38689 - /cfe/cfe/trunk/Lex/IdentifierTable.cpp

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:23:50 PDT 2007


Author: sabre
Date: Wed Jul 11 11:23:50 2007
New Revision: 38689

URL: http://llvm.org/viewvc/llvm-project?rev=38689&view=rev
Log:
Add a simple but useful optimization for identifier lookup.  Each time we
query the hash table, when we look up an identifier that isn't at the head
of it's bucket's list, move it there.  This reduces the number of list
traversals in the common case where identifiers are used in bursts.

Modified:
    cfe/cfe/trunk/Lex/IdentifierTable.cpp

Modified: cfe/cfe/trunk/Lex/IdentifierTable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/IdentifierTable.cpp?rev=38689&r1=38688&r2=38689&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/IdentifierTable.cpp (original)
+++ cfe/cfe/trunk/Lex/IdentifierTable.cpp Wed Jul 11 11:23:50 2007
@@ -178,11 +178,21 @@
   unsigned Length = NameEnd-NameStart;
   
   IdentifierBucket *IdentHead = TableArray[Hash];
-  for (IdentifierBucket *Identifier = IdentHead; Identifier; 
-       Identifier = Identifier->Next) {
+  for (IdentifierBucket *Identifier = IdentHead, *LastID = 0; Identifier; 
+       LastID = Identifier, Identifier = Identifier->Next) {
     if (Identifier->TokInfo.getNameLength() == Length &&
-        memcmp(Identifier->TokInfo.getName(), NameStart, Length) == 0)
+        memcmp(Identifier->TokInfo.getName(), NameStart, Length) == 0) {
+      // If found identifier wasn't at start of bucket, move it there so
+      // that frequently searched for identifiers are found earlier, even if
+      // they first occur late in the source file.
+      if (LastID) {
+        LastID->Next = Identifier->Next;
+        Identifier->Next = IdentHead;
+        TableArray[Hash] = Identifier;
+      }
+      
       return Identifier->TokInfo;
+    }
   }
 
   // Allocate a new identifier, with space for the null-terminated string at the
@@ -207,7 +217,9 @@
   memcpy(StrBuffer, NameStart, Length);
   StrBuffer[Length] = 0;  // Null terminate string.
   
-  // Link it into the hash table.
+  // Link it into the hash table.  Adding it to the start of the hash table is
+  // useful for buckets with lots of entries.  This means that more recently
+  // referenced identifiers will be near the head of the bucket.
   Identifier->Next = IdentHead;
   TableArray[Hash] = Identifier;
   return Identifier->TokInfo;





More information about the cfe-commits mailing list