[RFC] Add and sort decl to maintain order instead of inserting in order

ASTWriter::associateDeclWithFile shows a lot in clangd perf profile due to O(n^2) behaviour in insertion of DeclIDs in SortedFileDeclIDs. Instead of doing that, this patch just appends it to the DeclIDs vector and sorts them at the end.

Reviewed By: akyrtzi

Differential Revision: https://reviews.llvm.org/D124840
This commit is contained in:
Kugan Vivekanandarajah 2022-05-03 17:04:46 +01:00 committed by Ivan Murashko
parent bc3056b382
commit 2deebc0048
1 changed files with 2 additions and 10 deletions

View File

@ -3114,6 +3114,7 @@ void ASTWriter::WriteFileDeclIDsMap() {
for (auto &FileDeclEntry : SortedFileDeclIDs) {
DeclIDInFileInfo &Info = *FileDeclEntry.second;
Info.FirstDeclIndex = FileGroupedDeclIDs.size();
llvm::stable_sort(Info.DeclIDs);
for (auto &LocDeclEntry : Info.DeclIDs)
FileGroupedDeclIDs.push_back(LocDeclEntry.second);
}
@ -5462,16 +5463,7 @@ void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
LocDeclIDsTy &Decls = Info->DeclIDs;
if (Decls.empty() || Decls.back().first <= Offset) {
Decls.push_back(LocDecl);
return;
}
LocDeclIDsTy::iterator I =
llvm::upper_bound(Decls, LocDecl, llvm::less_first());
Decls.insert(I, LocDecl);
}
unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {