[cfe-commits] r61827 - /cfe/trunk/lib/Basic/SourceManager.cpp

Ted Kremenek kremenek at apple.com
Tue Jan 6 14:43:04 PST 2009


Author: kremenek
Date: Tue Jan  6 16:43:04 2009
New Revision: 61827

URL: http://llvm.org/viewvc/llvm-project?rev=61827&view=rev
Log:
SourceManager: Implement "lazy" creation of MemBuffers for source files.

- Big Idea:
   Source files are now mmaped when ContentCache::getBuffer() is first called.
   While this doesn't change the functionality when lexing regular source files,
   it can result in source files not being paged in when using PTH.

- Performance change:
  - No observable difference (-fsyntax-only/-Eonly) on Cocoa.h when doing
    regular source lexing.
  - No observable time difference (-fsyntax-only/-Eonly) on Cocoa.h when using
    PTH. We do observe, however, a reduction of 279K in memory mapped source
    code (3% reduction). The majority of pages from Cocoa.h (and friends) are
    still being pulled in, however, because any literal will cause
    Preprocessor::getSpelling() to be called (causing the source for the file to
    get pulled in). The next possible optimization is to cache literal strings
    in the PTH file to avoid the need for the original header sources entirely.

- Right now there is a preprocessor directive to toggle between "lazy" and
  "eager" creation of MemBuffers. This is not permanent, and is there in the
  short term to just test additional optimizations.

Modified:
    cfe/trunk/lib/Basic/SourceManager.cpp

Modified: cfe/trunk/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceManager.cpp?rev=61827&r1=61826&r2=61827&view=diff

==============================================================================
--- cfe/trunk/lib/Basic/SourceManager.cpp (original)
+++ cfe/trunk/lib/Basic/SourceManager.cpp Tue Jan  6 16:43:04 2009
@@ -24,6 +24,12 @@
 using namespace SrcMgr;
 using llvm::MemoryBuffer;
 
+// This (temporary) directive toggles between lazy and eager creation of
+// MemBuffers.  This directive is not permanent, and is here to test a few
+// potential optimizations in PTH.  Once it is clear whether eager or lazy
+// creation of MemBuffers is better this directive will get removed.
+#define LAZY
+
 ContentCache::~ContentCache() {
   delete Buffer;
   delete [] SourceLineCache;
@@ -44,7 +50,17 @@
   return Entry ? Entry->getSize() : Buffer->getBufferSize();
 }
 
-const llvm::MemoryBuffer* ContentCache::getBuffer() const {
+const llvm::MemoryBuffer* ContentCache::getBuffer() const {  
+#ifdef LAZY
+  // Lazily create the Buffer for ContentCaches that wrap files.
+  if (!Buffer && Entry) {
+    // FIXME: Should we support a way to not have to do this check over
+    //   and over if we cannot open the file?
+    // FIXME: This const_cast is ugly.  Should we make getBuffer() non-const?
+    const_cast<ContentCache*>(this)->Buffer = 
+      MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
+  }
+#endif
   return Buffer;
 }
 
@@ -62,16 +78,17 @@
     return &*I;
   
   // Nope, get information.
+#ifndef LAZY
   const MemoryBuffer *File =
     MemoryBuffer::getFile(FileEnt->getName(), 0, FileEnt->getSize());
   if (File == 0)
     return 0;
-
+#endif
+  
   ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt));
-
-  // FIXME: Shortly the above logic that creates a MemBuffer will be moved
-  // to ContentCache::getBuffer().  This way it can be done lazily.
+#ifndef LAZY
   Entry.setBuffer(File);
+#endif
   Entry.SourceLineCache = 0;
   Entry.NumLines = 0;
   return &Entry;





More information about the cfe-commits mailing list