mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:47:45 +00:00
LibJS: Add Token::flystring_value() to produce FlyString directly
When parsing identifiers, we ultimately want to sink the token string into a FlyString anyway, and since Token may have a FlyString already inside it, this allows us to bypass the costly FlyString(StringView). This gives a ~3% speedup when parsing the largest Discord JS file.
This commit is contained in:
parent
515594c667
commit
0cb0979990
2 changed files with 20 additions and 11 deletions
|
@ -1692,7 +1692,7 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression()
|
||||||
property_key = parse_property_key();
|
property_key = parse_property_key();
|
||||||
} else {
|
} else {
|
||||||
property_key = create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, identifier.value());
|
property_key = create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, identifier.value());
|
||||||
property_value = create_ast_node<Identifier>({ m_state.current_token.filename(), rule_start.position(), position() }, identifier.value());
|
property_value = create_ast_node<Identifier>({ m_state.current_token.filename(), rule_start.position(), position() }, identifier.flystring_value());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
property_key = parse_property_key();
|
property_key = parse_property_key();
|
||||||
|
@ -2076,7 +2076,7 @@ NonnullRefPtr<Expression> Parser::parse_secondary_expression(NonnullRefPtr<Expre
|
||||||
expected("IdentifierName");
|
expected("IdentifierName");
|
||||||
}
|
}
|
||||||
|
|
||||||
return create_ast_node<MemberExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), create_ast_node<Identifier>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value()));
|
return create_ast_node<MemberExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), create_ast_node<Identifier>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().flystring_value()));
|
||||||
case TokenType::BracketOpen: {
|
case TokenType::BracketOpen: {
|
||||||
consume(TokenType::BracketOpen);
|
consume(TokenType::BracketOpen);
|
||||||
auto expression = create_ast_node<MemberExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), parse_expression(0), true);
|
auto expression = create_ast_node<MemberExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(lhs), parse_expression(0), true);
|
||||||
|
@ -2246,7 +2246,7 @@ NonnullRefPtr<Identifier> Parser::parse_identifier()
|
||||||
syntax_error("'arguments' is not allowed in class field initializer");
|
syntax_error("'arguments' is not allowed in class field initializer");
|
||||||
return create_ast_node<Identifier>(
|
return create_ast_node<Identifier>(
|
||||||
{ m_state.current_token.filename(), identifier_start, position() },
|
{ m_state.current_token.filename(), identifier_start, position() },
|
||||||
token.value());
|
token.flystring_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<CallExpression::Argument> Parser::parse_arguments()
|
Vector<CallExpression::Argument> Parser::parse_arguments()
|
||||||
|
@ -2726,7 +2726,7 @@ RefPtr<BindingPattern> Parser::parse_binding_pattern(Parser::AllowDuplicates all
|
||||||
} else {
|
} else {
|
||||||
name = create_ast_node<Identifier>(
|
name = create_ast_node<Identifier>(
|
||||||
{ m_state.current_token.filename(), rule_start.position(), position() },
|
{ m_state.current_token.filename(), rule_start.position(), position() },
|
||||||
consume().value());
|
consume().flystring_value());
|
||||||
}
|
}
|
||||||
} else if (match(TokenType::BracketOpen)) {
|
} else if (match(TokenType::BracketOpen)) {
|
||||||
consume();
|
consume();
|
||||||
|
@ -2764,7 +2764,7 @@ RefPtr<BindingPattern> Parser::parse_binding_pattern(Parser::AllowDuplicates all
|
||||||
} else if (match_identifier_name()) {
|
} else if (match_identifier_name()) {
|
||||||
alias = create_ast_node<Identifier>(
|
alias = create_ast_node<Identifier>(
|
||||||
{ m_state.current_token.filename(), rule_start.position(), position() },
|
{ m_state.current_token.filename(), rule_start.position(), position() },
|
||||||
consume().value());
|
consume().flystring_value());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
expected("identifier or binding pattern");
|
expected("identifier or binding pattern");
|
||||||
|
@ -2800,7 +2800,7 @@ RefPtr<BindingPattern> Parser::parse_binding_pattern(Parser::AllowDuplicates all
|
||||||
alias = pattern.release_nonnull();
|
alias = pattern.release_nonnull();
|
||||||
} else if (match_identifier_name()) {
|
} else if (match_identifier_name()) {
|
||||||
// BindingElement must always have an Empty name field
|
// BindingElement must always have an Empty name field
|
||||||
auto identifier_name = consume_identifier().value();
|
auto identifier_name = consume_identifier().flystring_value();
|
||||||
alias = create_ast_node<Identifier>(
|
alias = create_ast_node<Identifier>(
|
||||||
{ m_state.current_token.filename(), rule_start.position(), position() },
|
{ m_state.current_token.filename(), rule_start.position(), position() },
|
||||||
identifier_name);
|
identifier_name);
|
||||||
|
@ -2886,7 +2886,7 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(bool for_l
|
||||||
Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>, Empty> target {};
|
Variant<NonnullRefPtr<Identifier>, NonnullRefPtr<BindingPattern>, Empty> target {};
|
||||||
if (match_identifier()) {
|
if (match_identifier()) {
|
||||||
auto identifier_start = push_start();
|
auto identifier_start = push_start();
|
||||||
auto name = consume_identifier().value();
|
auto name = consume_identifier().flystring_value();
|
||||||
target = create_ast_node<Identifier>(
|
target = create_ast_node<Identifier>(
|
||||||
{ m_state.current_token.filename(), rule_start.position(), position() },
|
{ m_state.current_token.filename(), rule_start.position(), position() },
|
||||||
name);
|
name);
|
||||||
|
@ -2908,14 +2908,14 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(bool for_l
|
||||||
|
|
||||||
target = create_ast_node<Identifier>(
|
target = create_ast_node<Identifier>(
|
||||||
{ m_state.current_token.filename(), rule_start.position(), position() },
|
{ m_state.current_token.filename(), rule_start.position(), position() },
|
||||||
consume().value());
|
consume().flystring_value());
|
||||||
} else if (!m_state.await_expression_is_valid && match(TokenType::Async)) {
|
} else if (!m_state.await_expression_is_valid && match(TokenType::Async)) {
|
||||||
if (m_program_type == Program::Type::Module)
|
if (m_program_type == Program::Type::Module)
|
||||||
syntax_error("Identifier must not be a reserved word in modules ('async')");
|
syntax_error("Identifier must not be a reserved word in modules ('async')");
|
||||||
|
|
||||||
target = create_ast_node<Identifier>(
|
target = create_ast_node<Identifier>(
|
||||||
{ m_state.current_token.filename(), rule_start.position(), position() },
|
{ m_state.current_token.filename(), rule_start.position(), position() },
|
||||||
consume().value());
|
consume().flystring_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (target.has<Empty>()) {
|
if (target.has<Empty>()) {
|
||||||
|
@ -3076,7 +3076,7 @@ NonnullRefPtr<OptionalChain> Parser::parse_optional_chain(NonnullRefPtr<Expressi
|
||||||
auto start = position();
|
auto start = position();
|
||||||
auto identifier = consume();
|
auto identifier = consume();
|
||||||
chain.append(OptionalChain::MemberReference {
|
chain.append(OptionalChain::MemberReference {
|
||||||
create_ast_node<Identifier>({ m_state.current_token.filename(), start, position() }, identifier.value()),
|
create_ast_node<Identifier>({ m_state.current_token.filename(), start, position() }, identifier.flystring_value()),
|
||||||
OptionalChain::Mode::Optional,
|
OptionalChain::Mode::Optional,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
@ -3099,7 +3099,7 @@ NonnullRefPtr<OptionalChain> Parser::parse_optional_chain(NonnullRefPtr<Expressi
|
||||||
auto start = position();
|
auto start = position();
|
||||||
auto identifier = consume();
|
auto identifier = consume();
|
||||||
chain.append(OptionalChain::MemberReference {
|
chain.append(OptionalChain::MemberReference {
|
||||||
create_ast_node<Identifier>({ m_state.current_token.filename(), start, position() }, identifier.value()),
|
create_ast_node<Identifier>({ m_state.current_token.filename(), start, position() }, identifier.flystring_value()),
|
||||||
OptionalChain::Mode::NotOptional,
|
OptionalChain::Mode::NotOptional,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -210,6 +210,15 @@ public:
|
||||||
[](FlyString const& identifier) { return identifier.view(); },
|
[](FlyString const& identifier) { return identifier.view(); },
|
||||||
[](Empty) -> StringView { VERIFY_NOT_REACHED(); });
|
[](Empty) -> StringView { VERIFY_NOT_REACHED(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FlyString flystring_value() const
|
||||||
|
{
|
||||||
|
return m_value.visit(
|
||||||
|
[](StringView view) -> FlyString { return view; },
|
||||||
|
[](FlyString const& identifier) -> FlyString { return identifier; },
|
||||||
|
[](Empty) -> FlyString { VERIFY_NOT_REACHED(); });
|
||||||
|
}
|
||||||
|
|
||||||
StringView filename() const { return m_filename; }
|
StringView filename() const { return m_filename; }
|
||||||
size_t line_number() const { return m_line_number; }
|
size_t line_number() const { return m_line_number; }
|
||||||
size_t line_column() const { return m_line_column; }
|
size_t line_column() const { return m_line_column; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue