Implement an optimization for finding the comment that occurs just after a
given declaration.
It is based on the observation that during parsing the comment that should be
attached to the decl is usually among the last two documentation comments
parsed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160400 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index c83e70a..2e13d0c 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -97,12 +97,30 @@
return NULL;
// Find the comment that occurs just after this declaration.
- RawComment CommentAtDeclLoc(SourceMgr, SourceRange(DeclLoc));
- ArrayRef<RawComment *>::iterator Comment
- = std::lower_bound(RawComments.begin(),
- RawComments.end(),
- &CommentAtDeclLoc,
- BeforeThanCompare<RawComment>(SourceMgr));
+ ArrayRef<RawComment *>::iterator Comment;
+ {
+ // When searching for comments during parsing, the comment we are looking
+ // for is usually among the last two comments we parsed -- check them
+ // first.
+ RawComment CommentAtDeclLoc(SourceMgr, SourceRange(DeclLoc));
+ BeforeThanCompare<RawComment> Compare(SourceMgr);
+ ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1;
+ bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
+ if (!Found && RawComments.size() >= 2) {
+ MaybeBeforeDecl--;
+ Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
+ }
+
+ if (Found) {
+ Comment = MaybeBeforeDecl + 1;
+ assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(),
+ &CommentAtDeclLoc, Compare));
+ } else {
+ // Slow path.
+ Comment = std::lower_bound(RawComments.begin(), RawComments.end(),
+ &CommentAtDeclLoc, Compare);
+ }
+ }
// Decompose the location for the declaration and find the beginning of the
// file buffer.