LibJS: Keep parsed function parameters in a shared data structure

Instead of making a copy of the Vector<FunctionParameter> from the AST
every time we instantiate an ECMAScriptFunctionObject, we now keep the
parameters in a ref-counted FunctionParameters object.

This reduces memory usage, and also allows us to cache the bytecode
executables for default parameter expressions without recompiling them
for every instantiation. :^)
This commit is contained in:
Andreas Kling 2025-03-27 12:59:50 +00:00 committed by Jelle Raaijmakers
parent 634f0c2469
commit 7477002e46
Notes: github-actions[bot] 2025-03-27 15:02:02 +00:00
11 changed files with 68 additions and 38 deletions

View file

@ -1136,7 +1136,7 @@ RefPtr<FunctionExpression const> Parser::try_parse_arrow_function_expression(boo
auto source_text = ByteString { m_state.lexer.source().substring_view(function_start_offset, function_end_offset - function_start_offset) };
return create_ast_node<FunctionExpression>(
{ m_source_code, rule_start.position(), position() }, nullptr, move(source_text),
move(body), move(parameters), function_length, function_kind, body->in_strict_mode(),
move(body), FunctionParameters::create(move(parameters)), function_length, function_kind, body->in_strict_mode(),
parsing_insights, move(local_variables_names), /* is_arrow_function */ true);
}
@ -1633,13 +1633,13 @@ NonnullRefPtr<ClassExpression const> Parser::parse_class_expression(bool expect_
FunctionParsingInsights parsing_insights;
constructor = create_ast_node<FunctionExpression>(
{ m_source_code, rule_start.position(), position() }, class_name, "",
move(constructor_body), Vector { FunctionParameter { move(argument_name), nullptr, true } }, 0, FunctionKind::Normal,
move(constructor_body), FunctionParameters::create(Vector { FunctionParameter { move(argument_name), nullptr, true } }), 0, FunctionKind::Normal,
/* is_strict_mode */ true, parsing_insights, /* local_variables_names */ Vector<FlyString> {});
} else {
FunctionParsingInsights parsing_insights;
constructor = create_ast_node<FunctionExpression>(
{ m_source_code, rule_start.position(), position() }, class_name, "",
move(constructor_body), Vector<FunctionParameter> {}, 0, FunctionKind::Normal,
move(constructor_body), FunctionParameters::empty(), 0, FunctionKind::Normal,
/* is_strict_mode */ true, parsing_insights, /* local_variables_names */ Vector<FlyString> {});
}
}
@ -2991,7 +2991,7 @@ NonnullRefPtr<FunctionNodeType> Parser::parse_function_node(u16 parse_options, O
parsing_insights.might_need_arguments_object = m_state.function_might_need_arguments_object;
return create_ast_node<FunctionNodeType>(
{ m_source_code, rule_start.position(), position() },
name, move(source_text), move(body), move(parameters), function_length,
name, move(source_text), move(body), FunctionParameters::create(move(parameters)), function_length,
function_kind, has_strict_directive, parsing_insights,
move(local_variables_names));
}