1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:07:34 +00:00

LibCpp: Don't store entire ASTNode vector in each parser state

We previously stored the entire ASTNode vector in each parser state,
and this vector was copied whenever a state was loaded or saved.

We don't actually need to store the whole nodes list in each state
because a new state can only add new nodes to this list, and won't
mutate existing nodes.

It would suffice to only hold a vector of the nodes that were created
while parsing in the current state to keep a reference to them.

This reduces the time it takes on my machine for the c++ language
server to handle a file that #includes <LibGUI/Widget.h> from ~4sec to
~0.7sec.
This commit is contained in:
Itamar 2021-07-13 22:07:05 +03:00 committed by Andreas Kling
parent eb6a15d52b
commit 42eb06f045
2 changed files with 17 additions and 16 deletions

View file

@ -24,7 +24,6 @@ public:
NonnullRefPtr<TranslationUnit> parse();
bool eof() const;
RefPtr<ASTNode> eof_node() const;
RefPtr<ASTNode> node_at(Position) const;
Optional<size_t> index_of_node_at(Position) const;
Optional<Token> token_at(Position) const;
@ -147,7 +146,7 @@ private:
struct State {
size_t token_index { 0 };
NonnullRefPtrVector<ASTNode> nodes;
NonnullRefPtrVector<ASTNode> state_nodes;
};
void error(StringView message = {});
@ -157,9 +156,13 @@ private:
create_ast_node(ASTNode& parent, const Position& start, Optional<Position> end, Args&&... args)
{
auto node = adopt_ref(*new T(&parent, start, end, m_filename, forward<Args>(args)...));
if (!parent.is_dummy_node()) {
m_state.nodes.append(node);
if (m_saved_states.is_empty()) {
m_nodes.append(node);
} else {
m_state.state_nodes.append(node);
}
return node;
}
@ -167,7 +170,7 @@ private:
create_root_ast_node(const Position& start, Position end)
{
auto node = adopt_ref(*new TranslationUnit(nullptr, start, end, m_filename));
m_state.nodes.append(node);
m_nodes.append(node);
m_root_node = node;
return node;
}
@ -200,6 +203,7 @@ private:
Vector<State> m_saved_states;
RefPtr<TranslationUnit> m_root_node;
Vector<String> m_errors;
NonnullRefPtrVector<ASTNode> m_nodes;
Vector<TokenAndPreprocessorDefinition> m_replaced_preprocessor_tokens;
};