Author: jkorous
Date: Wed Apr 10 13:23:33 2019
New Revision: 358133

URL: http://llvm.org/viewvc/llvm-project?rev=358133&view=rev
Log:
[clang][ASTContext] Try to exit early before loading serialized comments from 
AST files

Loading external comments is expensive. This change probably doesn't apply to 
common cases but is almost for free and would save some work in case none of 
the declaration needs external comments to be loaded.

Differential Revision: https://reviews.llvm.org/D60493

Modified:
    cfe/trunk/lib/AST/ASTContext.cpp

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=358133&r1=358132&r2=358133&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Wed Apr 10 13:23:33 2019
@@ -99,20 +99,13 @@ enum FloatingRank {
 };
 
 RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
-  if (!CommentsLoaded && ExternalSource) {
-    ExternalSource->ReadComments();
-
-#ifndef NDEBUG
-    ArrayRef<RawComment *> RawComments = Comments.getComments();
-    assert(std::is_sorted(RawComments.begin(), RawComments.end(),
-                          BeforeThanCompare<RawComment>(SourceMgr)));
-#endif
-
-    CommentsLoaded = true;
-  }
-
   assert(D);
 
+  // If we already tried to load comments but there are none,
+  // we won't find anything.
+  if (CommentsLoaded && Comments.getComments().empty())
+    return nullptr;
+
   // User can not attach documentation to implicit declarations.
   if (D->isImplicit())
     return nullptr;
@@ -162,12 +155,6 @@ RawComment *ASTContext::getRawCommentFor
       isa<TemplateTemplateParmDecl>(D))
     return nullptr;
 
-  ArrayRef<RawComment *> RawComments = Comments.getComments();
-
-  // If there are no comments anywhere, we won't find anything.
-  if (RawComments.empty())
-    return nullptr;
-
   // Find declaration location.
   // For Objective-C declarations we generally don't expect to have multiple
   // declarators, thus use declaration starting location as the "declaration
@@ -206,6 +193,23 @@ RawComment *ASTContext::getRawCommentFor
   if (DeclLoc.isInvalid() || !DeclLoc.isFileID())
     return nullptr;
 
+  if (!CommentsLoaded && ExternalSource) {
+    ExternalSource->ReadComments();
+
+#ifndef NDEBUG
+    ArrayRef<RawComment *> RawComments = Comments.getComments();
+    assert(std::is_sorted(RawComments.begin(), RawComments.end(),
+                          BeforeThanCompare<RawComment>(SourceMgr)));
+#endif
+
+    CommentsLoaded = true;
+  }
+
+  ArrayRef<RawComment *> RawComments = Comments.getComments();
+  // If there are no comments anywhere, we won't find anything.
+  if (RawComments.empty())
+    return nullptr;
+
   // Find the comment that occurs just after this declaration.
   ArrayRef<RawComment *>::iterator Comment;
   {


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to