mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibJS: Elide function wrapper for class field literal initializers
If a class field initializer is just a simple literal, we can skip creating (and calling) a wrapper function for it entirely. 1.44x speedup on JetStream3/raytrace-private-class-fields.js 1.53x speedup on JetStream3/raytrace-public-class-fields.js
This commit is contained in:
parent
23f0fddeab
commit
6d117095db
5 changed files with 61 additions and 22 deletions
|
@ -225,24 +225,35 @@ ThrowCompletionOr<ClassElement::ClassValue> ClassField::class_element_evaluation
|
|||
auto& realm = *vm.current_realm();
|
||||
|
||||
auto property_key_or_private_name = TRY(class_key_to_property_name(vm, *m_key, property_key));
|
||||
GC::Ptr<ECMAScriptFunctionObject> initializer;
|
||||
Variant<GC::Ref<ECMAScriptFunctionObject>, Value, Empty> initializer;
|
||||
if (m_initializer) {
|
||||
auto copy_initializer = m_initializer;
|
||||
auto name = property_key_or_private_name.visit(
|
||||
[&](PropertyKey const& property_key) -> String {
|
||||
return property_key.is_number() ? property_key.to_string() : property_key.to_string_or_symbol().to_display_string();
|
||||
},
|
||||
[&](PrivateName const& private_name) -> String {
|
||||
return private_name.description.to_string();
|
||||
});
|
||||
if (auto const* literal = as_if<NumericLiteral>(*m_initializer)) {
|
||||
initializer = literal->value();
|
||||
} else if (auto const* literal = as_if<BooleanLiteral>(*m_initializer)) {
|
||||
initializer = literal->value();
|
||||
} else if (auto const* literal = as_if<NullLiteral>(*m_initializer)) {
|
||||
initializer = literal->value();
|
||||
} else if (auto const* literal = as_if<StringLiteral>(*m_initializer)) {
|
||||
initializer = Value(PrimitiveString::create(vm, literal->value()));
|
||||
} else {
|
||||
auto copy_initializer = m_initializer;
|
||||
auto name = property_key_or_private_name.visit(
|
||||
[&](PropertyKey const& property_key) -> String {
|
||||
return property_key.is_number() ? property_key.to_string() : property_key.to_string_or_symbol().to_display_string();
|
||||
},
|
||||
[&](PrivateName const& private_name) -> String {
|
||||
return private_name.description.to_string();
|
||||
});
|
||||
|
||||
// FIXME: A potential optimization is not creating the functions here since these are never directly accessible.
|
||||
auto function_code = create_ast_node<ClassFieldInitializerStatement>(m_initializer->source_range(), copy_initializer.release_nonnull(), name);
|
||||
FunctionParsingInsights parsing_insights;
|
||||
parsing_insights.uses_this_from_environment = true;
|
||||
parsing_insights.uses_this = true;
|
||||
initializer = ECMAScriptFunctionObject::create(realm, "field"_string, ByteString::empty(), *function_code, FunctionParameters::empty(), 0, {}, vm.lexical_environment(), vm.running_execution_context().private_environment, FunctionKind::Normal, true, parsing_insights, false, property_key_or_private_name);
|
||||
initializer->make_method(target);
|
||||
// FIXME: A potential optimization is not creating the functions here since these are never directly accessible.
|
||||
auto function_code = create_ast_node<ClassFieldInitializerStatement>(m_initializer->source_range(), copy_initializer.release_nonnull(), name);
|
||||
FunctionParsingInsights parsing_insights;
|
||||
parsing_insights.uses_this_from_environment = true;
|
||||
parsing_insights.uses_this = true;
|
||||
auto function = ECMAScriptFunctionObject::create(realm, "field"_string, ByteString::empty(), *function_code, FunctionParameters::empty(), 0, {}, vm.lexical_environment(), vm.running_execution_context().private_environment, FunctionKind::Normal, true, parsing_insights, false, property_key_or_private_name);
|
||||
function->make_method(target);
|
||||
initializer = function;
|
||||
}
|
||||
}
|
||||
|
||||
return ClassValue {
|
||||
|
|
|
@ -94,6 +94,8 @@ public:
|
|||
virtual bool is_object_expression() const { return false; }
|
||||
virtual bool is_numeric_literal() const { return false; }
|
||||
virtual bool is_string_literal() const { return false; }
|
||||
virtual bool is_boolean_literal() const { return false; }
|
||||
virtual bool is_null_literal() const { return false; }
|
||||
virtual bool is_update_expression() const { return false; }
|
||||
virtual bool is_call_expression() const { return false; }
|
||||
virtual bool is_labelled_statement() const { return false; }
|
||||
|
@ -1214,6 +1216,8 @@ public:
|
|||
virtual Value value() const override { return Value(m_value); }
|
||||
|
||||
private:
|
||||
virtual bool is_boolean_literal() const override { return true; }
|
||||
|
||||
bool m_value { false };
|
||||
};
|
||||
|
||||
|
@ -1281,6 +1285,9 @@ public:
|
|||
virtual Bytecode::CodeGenerationErrorOr<Optional<Bytecode::ScopedOperand>> generate_bytecode(Bytecode::Generator&, Optional<Bytecode::ScopedOperand> preferred_dst = {}) const override;
|
||||
|
||||
virtual Value value() const override { return js_null(); }
|
||||
|
||||
private:
|
||||
virtual bool is_null_literal() const override { return true; }
|
||||
};
|
||||
|
||||
class RegExpLiteral final : public Expression {
|
||||
|
@ -2267,6 +2274,15 @@ inline bool ASTNode::fast_is<ObjectExpression>() const { return is_object_expres
|
|||
template<>
|
||||
inline bool ASTNode::fast_is<ImportCall>() const { return is_import_call(); }
|
||||
|
||||
template<>
|
||||
inline bool ASTNode::fast_is<NumericLiteral>() const { return is_numeric_literal(); }
|
||||
|
||||
template<>
|
||||
inline bool ASTNode::fast_is<BooleanLiteral>() const { return is_boolean_literal(); }
|
||||
|
||||
template<>
|
||||
inline bool ASTNode::fast_is<NullLiteral>() const { return is_null_literal(); }
|
||||
|
||||
template<>
|
||||
inline bool ASTNode::fast_is<StringLiteral>() const { return is_string_literal(); }
|
||||
|
||||
|
|
|
@ -17,8 +17,8 @@ using ClassElementName = Variant<PropertyKey, PrivateName>;
|
|||
|
||||
// 6.2.10 The ClassFieldDefinition Record Specification Type, https://tc39.es/ecma262/#sec-classfielddefinition-record-specification-type
|
||||
struct ClassFieldDefinition {
|
||||
ClassElementName name; // [[Name]]
|
||||
GC::Ptr<ECMAScriptFunctionObject> initializer; // [[Initializer]]
|
||||
ClassElementName name; // [[Name]]
|
||||
Variant<GC::Ref<ECMAScriptFunctionObject>, Value, Empty> initializer; // [[Initializer]]
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -553,7 +553,14 @@ void ECMAScriptFunctionObject::visit_edges(Visitor& visitor)
|
|||
visitor.visit(m_bytecode_executable);
|
||||
|
||||
for (auto& field : m_fields) {
|
||||
visitor.visit(field.initializer);
|
||||
field.initializer.visit(
|
||||
[&visitor](GC::Ref<ECMAScriptFunctionObject>& initializer) {
|
||||
visitor.visit(initializer);
|
||||
},
|
||||
[&visitor](Value initializer) {
|
||||
visitor.visit(initializer);
|
||||
},
|
||||
[](Empty) {});
|
||||
if (auto* property_key_ptr = field.name.get_pointer<PropertyKey>(); property_key_ptr && property_key_ptr->is_symbol())
|
||||
visitor.visit(property_key_ptr->as_symbol());
|
||||
}
|
||||
|
|
|
@ -669,9 +669,14 @@ ThrowCompletionOr<void> Object::define_field(ClassFieldDefinition const& field)
|
|||
auto init_value = js_undefined();
|
||||
|
||||
// 3. If initializer is not empty, then
|
||||
if (initializer) {
|
||||
// a. Let initValue be ? Call(initializer, receiver).
|
||||
init_value = TRY(call(vm, initializer, this));
|
||||
if (!initializer.has<Empty>()) {
|
||||
// OPTIMIZATION: If the initializer is a value (from a literal), we can skip the call.
|
||||
if (auto const* initializer_value = initializer.get_pointer<Value>()) {
|
||||
init_value = *initializer_value;
|
||||
} else {
|
||||
// a. Let initValue be ? Call(initializer, receiver).
|
||||
init_value = TRY(call(vm, *initializer.get<GC::Ref<ECMAScriptFunctionObject>>(), this));
|
||||
}
|
||||
}
|
||||
// 4. Else, let initValue be undefined.
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue