mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-24 18:02:20 +00:00
LibJS: Elide function wrapper for class field literal initializers
Some checks are pending
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Some checks are pending
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
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
fd147e6be0
commit
6bb0d585e3
Notes:
github-actions[bot]
2025-04-01 21:56:14 +00:00
Author: https://github.com/awesomekling
Commit: 6bb0d585e3
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4180
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& realm = *vm.current_realm();
|
||||||
|
|
||||||
auto property_key_or_private_name = TRY(class_key_to_property_name(vm, *m_key, property_key));
|
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) {
|
if (m_initializer) {
|
||||||
auto copy_initializer = m_initializer;
|
if (auto const* literal = as_if<NumericLiteral>(*m_initializer)) {
|
||||||
auto name = property_key_or_private_name.visit(
|
initializer = literal->value();
|
||||||
[&](PropertyKey const& property_key) -> String {
|
} else if (auto const* literal = as_if<BooleanLiteral>(*m_initializer)) {
|
||||||
return property_key.is_number() ? property_key.to_string() : property_key.to_string_or_symbol().to_display_string();
|
initializer = literal->value();
|
||||||
},
|
} else if (auto const* literal = as_if<NullLiteral>(*m_initializer)) {
|
||||||
[&](PrivateName const& private_name) -> String {
|
initializer = literal->value();
|
||||||
return private_name.description.to_string();
|
} 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.
|
// 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);
|
auto function_code = create_ast_node<ClassFieldInitializerStatement>(m_initializer->source_range(), copy_initializer.release_nonnull(), name);
|
||||||
FunctionParsingInsights parsing_insights;
|
FunctionParsingInsights parsing_insights;
|
||||||
parsing_insights.uses_this_from_environment = true;
|
parsing_insights.uses_this_from_environment = true;
|
||||||
parsing_insights.uses_this = 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);
|
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);
|
||||||
initializer->make_method(target);
|
function->make_method(target);
|
||||||
|
initializer = function;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ClassValue {
|
return ClassValue {
|
||||||
|
|
|
@ -94,6 +94,8 @@ public:
|
||||||
virtual bool is_object_expression() const { return false; }
|
virtual bool is_object_expression() const { return false; }
|
||||||
virtual bool is_numeric_literal() const { return false; }
|
virtual bool is_numeric_literal() const { return false; }
|
||||||
virtual bool is_string_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_update_expression() const { return false; }
|
||||||
virtual bool is_call_expression() const { return false; }
|
virtual bool is_call_expression() const { return false; }
|
||||||
virtual bool is_labelled_statement() 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); }
|
virtual Value value() const override { return Value(m_value); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
virtual bool is_boolean_literal() const override { return true; }
|
||||||
|
|
||||||
bool m_value { false };
|
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 Bytecode::CodeGenerationErrorOr<Optional<Bytecode::ScopedOperand>> generate_bytecode(Bytecode::Generator&, Optional<Bytecode::ScopedOperand> preferred_dst = {}) const override;
|
||||||
|
|
||||||
virtual Value value() const override { return js_null(); }
|
virtual Value value() const override { return js_null(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual bool is_null_literal() const override { return true; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class RegExpLiteral final : public Expression {
|
class RegExpLiteral final : public Expression {
|
||||||
|
@ -2267,6 +2274,15 @@ inline bool ASTNode::fast_is<ObjectExpression>() const { return is_object_expres
|
||||||
template<>
|
template<>
|
||||||
inline bool ASTNode::fast_is<ImportCall>() const { return is_import_call(); }
|
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<>
|
template<>
|
||||||
inline bool ASTNode::fast_is<StringLiteral>() const { return is_string_literal(); }
|
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
|
// 6.2.10 The ClassFieldDefinition Record Specification Type, https://tc39.es/ecma262/#sec-classfielddefinition-record-specification-type
|
||||||
struct ClassFieldDefinition {
|
struct ClassFieldDefinition {
|
||||||
ClassElementName name; // [[Name]]
|
ClassElementName name; // [[Name]]
|
||||||
GC::Ptr<ECMAScriptFunctionObject> initializer; // [[Initializer]]
|
Variant<GC::Ref<ECMAScriptFunctionObject>, Value, Empty> initializer; // [[Initializer]]
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -553,7 +553,14 @@ void ECMAScriptFunctionObject::visit_edges(Visitor& visitor)
|
||||||
visitor.visit(m_bytecode_executable);
|
visitor.visit(m_bytecode_executable);
|
||||||
|
|
||||||
for (auto& field : m_fields) {
|
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())
|
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());
|
visitor.visit(property_key_ptr->as_symbol());
|
||||||
}
|
}
|
||||||
|
|
|
@ -669,9 +669,14 @@ ThrowCompletionOr<void> Object::define_field(ClassFieldDefinition const& field)
|
||||||
auto init_value = js_undefined();
|
auto init_value = js_undefined();
|
||||||
|
|
||||||
// 3. If initializer is not empty, then
|
// 3. If initializer is not empty, then
|
||||||
if (initializer) {
|
if (!initializer.has<Empty>()) {
|
||||||
// a. Let initValue be ? Call(initializer, receiver).
|
// OPTIMIZATION: If the initializer is a value (from a literal), we can skip the call.
|
||||||
init_value = TRY(call(vm, initializer, this));
|
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.
|
// 4. Else, let initValue be undefined.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue