LibJS: Call shrink_to_fit() on various Vectors created during parse

Vectors that stick around in the AST were wasting a fair bit of memory
due to the growth padding we keep by default. This patch goes after some
of these vectors with the shrink_to_fit() stick to reduce waste.

Since the AST can stay around for a long time, it is worth making an
effort to shrink it down when we have a chance.
This commit is contained in:
Andreas Kling 2022-11-26 20:45:06 +01:00 committed by Linus Groh
parent 2e98c17347
commit 9721da2e6a
Notes: sideshowbarker 2024-07-17 08:37:36 +09:00
2 changed files with 18 additions and 0 deletions

View file

@ -280,6 +280,14 @@ public:
m_children.append(move(child));
}
void shrink_to_fit()
{
m_children.shrink_to_fit();
m_lexical_declarations.shrink_to_fit();
m_var_declarations.shrink_to_fit();
m_functions_hoistable_with_annexB_extension.shrink_to_fit();
}
NonnullRefPtrVector<Statement> const& children() const { return m_children; }
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;

View file

@ -1807,6 +1807,7 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression()
m_state.invalid_property_range_in_object_expression.set(object_expression_offset, invalid_object_literal_property_range->start);
}
properties.shrink_to_fit();
return create_ast_node<ObjectExpression>(
{ m_source_code, rule_start.position(), position() },
move(properties));
@ -1835,6 +1836,8 @@ NonnullRefPtr<ArrayExpression> Parser::parse_array_expression()
}
consume(TokenType::BracketClose);
elements.shrink_to_fit();
return create_ast_node<ArrayExpression>({ m_source_code, rule_start.position(), position() }, move(elements));
}
@ -2022,6 +2025,7 @@ NonnullRefPtr<Expression> Parser::parse_expression(int min_precedence, Associati
consume();
expressions.append(parse_expression(2));
}
expressions.shrink_to_fit();
expression = create_ast_node<SequenceExpression>({ m_source_code, rule_start.position(), position() }, move(expressions));
}
return expression;
@ -2461,6 +2465,8 @@ void Parser::parse_statement_list(ScopeNode& output_node, AllowLabelledFunction
break;
}
}
output_node.shrink_to_fit();
}
// FunctionBody, https://tc39.es/ecma262/#prod-FunctionBody
@ -2729,6 +2735,8 @@ Vector<FunctionParameter> Parser::parse_formal_parameters(int& function_length,
// Otherwise, we need a closing parenthesis (which is consumed elsewhere). If we get neither, it's an error.
if (!match(TokenType::Eof) && !match(TokenType::ParenClose))
expected(Token::name(TokenType::ParenClose));
parameters.shrink_to_fit();
return parameters;
}
@ -3033,6 +3041,8 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(bool for_l
if (!for_loop_variable_declaration)
consume_or_insert_semicolon();
declarations.shrink_to_fit();
auto declaration = create_ast_node<VariableDeclaration>({ m_source_code, rule_start.position(), position() }, declaration_kind, move(declarations));
return declaration;
}