1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 04:58:13 +00:00

CppLanguageServer: Cache declarations from headers in every document

Previously, to get the globally available declarations in a document
(including declarations from headers), we would have to recursively
walk the #include tree and get the declarations of each included
document.

To improve upon this, we now store a HashTable of globally available
declaration from included header files in each document, and populate
it when we first process the document.

Before this, invoking simple autocomplete actions in code documents
that had a very large #include tree (e.g when <LibGUI/Widget.h> was
included) hang the CppLanguageServer process and used 100% CPU until
the process ran out of memory.

Now, the autocomplete request in that situation returns immediately :^)
This commit is contained in:
Itamar 2021-05-07 15:20:05 +03:00 committed by Andreas Kling
parent c85775d9f5
commit 5c19a48b95
2 changed files with 18 additions and 15 deletions

View file

@ -44,12 +44,7 @@ OwnPtr<ParserAutoComplete::DocumentData> ParserAutoComplete::create_document_dat
auto document = filedb().get_or_create_from_filesystem(file);
if (!document)
return {};
auto content = document->text();
auto document_data = create_document_data(document->text(), file);
update_declared_symbols(*document_data);
return document_data;
return create_document_data(document->text(), file);
}
void ParserAutoComplete::set_document_data(const String& file, OwnPtr<DocumentData>&& data)
@ -260,13 +255,8 @@ Vector<ParserAutoComplete::PropertyInfo> ParserAutoComplete::properties_of_type(
NonnullRefPtrVector<Declaration> ParserAutoComplete::get_global_declarations_including_headers(const DocumentData& document) const
{
NonnullRefPtrVector<Declaration> declarations;
for (auto& include : document.preprocessor().included_paths()) {
document_path_from_include_path(include);
auto included_document = get_document_data(document_path_from_include_path(include));
if (!included_document)
continue;
declarations.append(get_global_declarations_including_headers(*included_document));
}
for (auto& decl : document.m_declarations_from_headers)
declarations.append(*decl);
declarations.append(get_global_declarations(*document.parser().root_node()));
@ -434,8 +424,16 @@ RefPtr<Declaration> ParserAutoComplete::find_declaration_of(const DocumentData&
return {};
}
void ParserAutoComplete::update_declared_symbols(const DocumentData& document)
void ParserAutoComplete::update_declared_symbols(DocumentData& document)
{
for (auto& include : document.preprocessor().included_paths()) {
auto included_document = get_or_create_document_data(document_path_from_include_path(include));
if (!included_document)
continue;
for (auto&& decl : get_global_declarations_including_headers(*included_document))
document.m_declarations_from_headers.set(move(decl));
}
Vector<GUI::AutocompleteProvider::Declaration> declarations;
for (auto& decl : get_global_declarations(*document.parser().root_node())) {
@ -494,6 +492,8 @@ OwnPtr<ParserAutoComplete::DocumentData> ParserAutoComplete::create_document_dat
if constexpr (CPP_LANGUAGE_SERVER_DEBUG)
root->dump(0);
update_declared_symbols(*document_data);
return document_data;
}