[cfe-commits] r70109 - in /cfe/trunk: include/clang/Frontend/PCHWriter.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp

Douglas Gregor dgregor at apple.com
Sat Apr 25 20:49:15 PDT 2009


Author: dgregor
Date: Sat Apr 25 22:49:13 2009
New Revision: 70109

URL: http://llvm.org/viewvc/llvm-project?rev=70109&view=rev
Log:
When writing a PCH file, write multiple type and declaration blocks as
necessary and iterate until all types and declarations have been
written. This reduces the Cocoa.h PCH file size by about 4% (since we
don't write types we don't need), and fixes problems where writing a
declaration generates a new type.

This doesn't seem to have any impact on performance either way.

Modified:
    cfe/trunk/include/clang/Frontend/PCHWriter.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp

Modified: cfe/trunk/include/clang/Frontend/PCHWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHWriter.h?rev=70109&r1=70108&r2=70109&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHWriter.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHWriter.h Sat Apr 25 22:49:13 2009
@@ -86,6 +86,10 @@
   /// \brief The type ID that will be assigned to the next new type.
   pch::TypeID NextTypeID;
 
+  /// \brief Queue containing the types that we still need to
+  /// emit.
+  std::queue<const Type *> TypesToEmit;
+
   /// \brief Map that provides the ID numbers of each identifier in
   /// the output stream.
   ///

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=70109&r1=70108&r2=70109&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Sat Apr 25 22:49:13 2009
@@ -2563,6 +2563,7 @@
   }
 
   Index -= pch::NUM_PREDEF_TYPE_IDS;
+  assert(Index < TypesLoaded.size() && "Type index out-of-range");
   if (!TypesLoaded[Index])
     TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr();
     

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=70109&r1=70108&r2=70109&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Sat Apr 25 22:49:13 2009
@@ -1674,15 +1674,12 @@
   // Enter the types block.
   Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
 
-  // Emit all of the types in the ASTContext
-  for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
-                                       TEnd = Context.getTypes().end();
-       T != TEnd; ++T) {
-    // Builtin types are never serialized.
-    if (isa<BuiltinType>(*T))
-      continue;
-
-    WriteType(*T);
+  // Emit all of the types that need to be emitted (so far).
+  while (!TypesToEmit.empty()) {
+    const Type *T = TypesToEmit.front();
+    TypesToEmit.pop();
+    assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
+    WriteType(T);
   }
 
   // Exit the types block
@@ -2409,8 +2406,16 @@
   WriteLanguageOptions(Context.getLangOptions());
   WriteSourceManagerBlock(Context.getSourceManager(), PP);
   WritePreprocessor(PP);
-  WriteTypesBlock(Context);
-  WriteDeclsBlock(Context);
+
+  // Keep writing types and declarations until all types and
+  // declarations have been written.
+  do {
+    if (!DeclsToEmit.empty())
+      WriteDeclsBlock(Context);
+    if (!TypesToEmit.empty())
+      WriteTypesBlock(Context);
+  } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
+
   WriteMethodPool(SemaRef);
   WriteIdentifierTable(PP);
 
@@ -2559,8 +2564,12 @@
   }
 
   pch::TypeID &ID = TypeIDs[T.getTypePtr()];
-  if (ID == 0) // we haven't seen this type before
+  if (ID == 0) {
+    // We haven't seen this type before. Assign it a new ID and put it
+    // into the queu of types to emit.
     ID = NextTypeID++;
+    TypesToEmit.push(T.getTypePtr());
+  }
 
   // Encode the type qualifiers in the type reference.
   Record.push_back((ID << 3) | T.getCVRQualifiers());





More information about the cfe-commits mailing list