From 515594c667abc9c5c9b639b4ba4d3f43b54872f7 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 13 Feb 2022 13:07:38 +0100 Subject: LibJS: Add fast_is() for things that were hot in RTTI This gives a ~4% speedup when parsing the largest Discord JS file. --- Userland/Libraries/LibJS/AST.h | 66 ++++++++++++++++++++++ .../LibJS/Runtime/ECMAScriptFunctionObject.h | 3 + 2 files changed, 69 insertions(+) (limited to 'Userland') diff --git a/Userland/Libraries/LibJS/AST.h b/Userland/Libraries/LibJS/AST.h index f4f5bd9fa2..684ff58106 100644 --- a/Userland/Libraries/LibJS/AST.h +++ b/Userland/Libraries/LibJS/AST.h @@ -67,7 +67,18 @@ public: virtual bool is_identifier() const { return false; } virtual bool is_scope_node() const { return false; } virtual bool is_program() const { return false; } + virtual bool is_class_declaration() const { return false; } virtual bool is_function_declaration() const { return false; } + virtual bool is_variable_declaration() const { return false; } + virtual bool is_import_call() const { return false; } + virtual bool is_array_expression() const { return false; } + virtual bool is_object_expression() const { return false; } + virtual bool is_string_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; } + virtual bool is_iteration_statement() const { return false; } + virtual bool is_class_method() const { return false; } protected: explicit ASTNode(SourceRange source_range) @@ -106,6 +117,8 @@ public: NonnullRefPtr& labelled_item() { return m_labelled_item; } private: + virtual bool is_labelled_statement() const final { return true; } + FlyString m_label; NonnullRefPtr m_labelled_item; }; @@ -126,6 +139,9 @@ public: using Statement::Statement; virtual Completion loop_evaluation(Interpreter&, GlobalObject&, Vector const&) const = 0; + +private: + virtual bool is_iteration_statement() const final { return true; } }; class EmptyStatement final : public Statement { @@ -1124,6 +1140,8 @@ public: bool is_use_strict_directive() const { return m_is_use_strict_directive; }; private: + virtual bool is_string_literal() const override { return true; } + String m_value; bool m_is_use_strict_directive; }; @@ -1272,6 +1290,7 @@ public: virtual Optional private_bound_identifier() const override; private: + virtual bool is_class_method() const override { return true; } NonnullRefPtr m_key; NonnullRefPtr m_function; Kind m_kind; @@ -1387,6 +1406,8 @@ public: StringView name() const { return m_class_expression->name(); } private: + virtual bool is_class_declaration() const override { return true; } + friend ExportStatement; NonnullRefPtr m_class_expression; @@ -1439,6 +1460,8 @@ public: Expression const& callee() const { return m_callee; } protected: + virtual bool is_call_expression() const override { return true; } + Completion throw_type_error_for_callee(Interpreter&, GlobalObject&, Value callee_value, StringView call_type) const; NonnullRefPtr m_callee; @@ -1546,6 +1569,8 @@ public: virtual void generate_bytecode(Bytecode::Generator&) const override; private: + virtual bool is_update_expression() const override { return true; } + UpdateOp m_op; NonnullRefPtr m_argument; bool m_prefixed; @@ -1614,6 +1639,8 @@ public: virtual bool is_lexical_declaration() const override { return m_declaration_kind != DeclarationKind::Var; } private: + virtual bool is_variable_declaration() const override { return true; } + DeclarationKind m_declaration_kind; NonnullRefPtrVector m_declarations; }; @@ -1672,6 +1699,8 @@ public: Optional const& invalid_property_range() const { return m_first_invalid_property_range; } private: + virtual bool is_object_expression() const override { return true; } + NonnullRefPtrVector m_properties; Optional m_first_invalid_property_range; }; @@ -1691,6 +1720,8 @@ public: virtual void generate_bytecode(Bytecode::Generator&) const override; private: + virtual bool is_array_expression() const override { return true; } + Vector> m_elements; }; @@ -1851,6 +1882,8 @@ public: virtual Completion execute(Interpreter&, GlobalObject&) const override; private: + virtual bool is_import_call() const override { return true; } + NonnullRefPtr m_specifier; RefPtr m_options; }; @@ -2070,7 +2103,40 @@ inline bool ASTNode::fast_is() const { return is_scope_node(); } template<> inline bool ASTNode::fast_is() const { return is_program(); } +template<> +inline bool ASTNode::fast_is() const { return is_class_declaration(); } + template<> inline bool ASTNode::fast_is() const { return is_function_declaration(); } +template<> +inline bool ASTNode::fast_is() const { return is_variable_declaration(); } + +template<> +inline bool ASTNode::fast_is() const { return is_array_expression(); } + +template<> +inline bool ASTNode::fast_is() const { return is_object_expression(); } + +template<> +inline bool ASTNode::fast_is() const { return is_import_call(); } + +template<> +inline bool ASTNode::fast_is() const { return is_string_literal(); } + +template<> +inline bool ASTNode::fast_is() const { return is_update_expression(); } + +template<> +inline bool ASTNode::fast_is() const { return is_call_expression(); } + +template<> +inline bool ASTNode::fast_is() const { return is_labelled_statement(); } + +template<> +inline bool ASTNode::fast_is() const { return is_iteration_statement(); } + +template<> +inline bool ASTNode::fast_is() const { return is_class_method(); } + } diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h index e220d5cdca..a72cfdaa48 100644 --- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h +++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.h @@ -134,4 +134,7 @@ private: FunctionKind m_kind : 3 { FunctionKind::Normal }; }; +template<> +inline bool Object::fast_is() const { return is_ecmascript_function_object(); } + } -- cgit v1.2.3