1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 21:27:34 +00:00

LibJS: Add a bunch of fast_is<T> to avoid slow RTTI in hot code

This commit is contained in:
Andreas Kling 2021-06-13 11:23:23 +02:00
parent 39ad705c13
commit 095accd2b2
5 changed files with 61 additions and 0 deletions

View file

@ -50,6 +50,17 @@ public:
String class_name() const; String class_name() const;
template<typename T>
bool fast_is() const = delete;
virtual bool is_new_expression() const { return false; }
virtual bool is_member_expression() const { return false; }
virtual bool is_super_expression() const { return false; }
virtual bool is_expression_statement() const { return false; }
virtual bool is_identifier() const { return false; }
virtual bool is_scope_node() const { return false; }
virtual bool is_program() const { return false; }
protected: protected:
ASTNode(SourceRange source_range) ASTNode(SourceRange source_range)
: m_source_range(move(source_range)) : m_source_range(move(source_range))
@ -108,6 +119,8 @@ public:
Expression const& expression() const { return m_expression; }; Expression const& expression() const { return m_expression; };
private: private:
virtual bool is_expression_statement() const override { return true; }
NonnullRefPtr<Expression> m_expression; NonnullRefPtr<Expression> m_expression;
}; };
@ -142,6 +155,8 @@ protected:
} }
private: private:
virtual bool is_scope_node() const final { return true; }
NonnullRefPtrVector<Statement> m_children; NonnullRefPtrVector<Statement> m_children;
NonnullRefPtrVector<VariableDeclaration> m_variables; NonnullRefPtrVector<VariableDeclaration> m_variables;
NonnullRefPtrVector<FunctionDeclaration> m_functions; NonnullRefPtrVector<FunctionDeclaration> m_functions;
@ -160,6 +175,8 @@ public:
void set_strict_mode() { m_is_strict_mode = true; } void set_strict_mode() { m_is_strict_mode = true; }
private: private:
virtual bool is_program() const override { return true; }
bool m_is_strict_mode { false }; bool m_is_strict_mode { false };
}; };
@ -765,6 +782,8 @@ public:
virtual void generate_bytecode(Bytecode::Generator&) const override; virtual void generate_bytecode(Bytecode::Generator&) const override;
private: private:
virtual bool is_identifier() const override { return true; }
FlyString m_string; FlyString m_string;
}; };
@ -808,6 +827,8 @@ public:
virtual Value execute(Interpreter&, GlobalObject&) const override; virtual Value execute(Interpreter&, GlobalObject&) const override;
virtual void dump(int indent) const override; virtual void dump(int indent) const override;
virtual bool is_super_expression() const override { return true; }
}; };
class ClassExpression final : public Expression { class ClassExpression final : public Expression {
@ -908,6 +929,8 @@ public:
: CallExpression(move(source_range), move(callee), move(arguments)) : CallExpression(move(source_range), move(callee), move(arguments))
{ {
} }
virtual bool is_new_expression() const override { return true; }
}; };
enum class AssignmentOp { enum class AssignmentOp {
@ -1176,6 +1199,8 @@ public:
String to_string_approximation() const; String to_string_approximation() const;
private: private:
virtual bool is_member_expression() const override { return true; }
NonnullRefPtr<Expression> m_object; NonnullRefPtr<Expression> m_object;
NonnullRefPtr<Expression> m_property; NonnullRefPtr<Expression> m_property;
bool m_computed { false }; bool m_computed { false };
@ -1378,4 +1403,25 @@ void BindingPattern::for_each_assigned_name(C&& callback) const
} }
} }
template<>
inline bool ASTNode::fast_is<NewExpression>() const { return is_new_expression(); }
template<>
inline bool ASTNode::fast_is<MemberExpression>() const { return is_member_expression(); }
template<>
inline bool ASTNode::fast_is<SuperExpression>() const { return is_super_expression(); }
template<>
inline bool ASTNode::fast_is<Identifier>() const { return is_identifier(); }
template<>
inline bool ASTNode::fast_is<ExpressionStatement>() const { return is_expression_statement(); }
template<>
inline bool ASTNode::fast_is<ScopeNode>() const { return is_scope_node(); }
template<>
inline bool ASTNode::fast_is<Program>() const { return is_program(); }
} }

View file

@ -66,6 +66,7 @@ public:
EnvironmentRecordType type() const { return m_environment_record_type; } EnvironmentRecordType type() const { return m_environment_record_type; }
private: private:
virtual bool is_lexical_environment() const override { return true; }
virtual void visit_edges(Visitor&) override; virtual void visit_edges(Visitor&) override;
EnvironmentRecordType m_environment_record_type : 8 { EnvironmentRecordType::Declarative }; EnvironmentRecordType m_environment_record_type : 8 { EnvironmentRecordType::Declarative };
@ -78,4 +79,7 @@ private:
Function* m_current_function { nullptr }; Function* m_current_function { nullptr };
}; };
template<>
inline bool Object::fast_is<LexicalEnvironment>() const { return is_lexical_environment(); }
} }

View file

@ -35,9 +35,13 @@ protected:
private: private:
virtual LexicalEnvironment* create_environment() override final; virtual LexicalEnvironment* create_environment() override final;
virtual bool is_native_function() const final { return true; }
FlyString m_name; FlyString m_name;
AK::Function<Value(VM&, GlobalObject&)> m_native_function; AK::Function<Value(VM&, GlobalObject&)> m_native_function;
}; };
template<>
inline bool Object::fast_is<NativeFunction>() const { return is_native_function(); }
} }

View file

@ -106,6 +106,9 @@ public:
virtual bool is_typed_array() const { return false; } virtual bool is_typed_array() const { return false; }
virtual bool is_string_object() const { return false; } virtual bool is_string_object() const { return false; }
virtual bool is_global_object() const { return false; } virtual bool is_global_object() const { return false; }
virtual bool is_proxy_object() const { return false; }
virtual bool is_native_function() const { return false; }
virtual bool is_lexical_environment() const { return false; }
virtual const char* class_name() const override { return "Object"; } virtual const char* class_name() const override { return "Object"; }
virtual void visit_edges(Cell::Visitor&) override; virtual void visit_edges(Cell::Visitor&) override;

View file

@ -46,10 +46,14 @@ private:
virtual void visit_edges(Visitor&) override; virtual void visit_edges(Visitor&) override;
virtual bool is_function() const override { return m_target.is_function(); } virtual bool is_function() const override { return m_target.is_function(); }
virtual bool is_proxy_object() const final { return true; }
Object& m_target; Object& m_target;
Object& m_handler; Object& m_handler;
bool m_is_revoked { false }; bool m_is_revoked { false };
}; };
template<>
inline bool Object::fast_is<ProxyObject>() const { return is_proxy_object(); }
} }