diff options
author | Andreas Kling <kling@serenityos.org> | 2020-03-22 11:07:55 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-22 13:03:43 +0100 |
commit | cccbe43056748a9443a49abf5bdc938ba5e537d6 (patch) | |
tree | 09767cad7e20b1494f8ab23b7c8e31677cbd2970 /Libraries | |
parent | 4f72f6b8866cfa353a29248a4f1ca7c242949231 (diff) | |
download | serenity-cccbe43056748a9443a49abf5bdc938ba5e537d6.zip |
LibJS: Use FlyString for identifiers
This makes variable and property lookups a lot faster since comparing
two FlyStrings is O(1).
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibJS/AST.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/AST.h | 39 | ||||
-rw-r--r-- | Libraries/LibJS/Interpreter.cpp | 8 | ||||
-rw-r--r-- | Libraries/LibJS/Interpreter.h | 17 | ||||
-rw-r--r-- | Libraries/LibJS/Parser.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Array.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Array.h | 4 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ConsoleObject.cpp | 1 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/MathObject.cpp | 1 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Object.cpp | 14 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Object.h | 16 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ScriptFunction.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/ScriptFunction.h | 6 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/DocumentWrapper.cpp | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/EventTargetWrapper.cpp | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/MouseEventWrapper.cpp | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/Bindings/NodeWrapper.cpp | 1 |
19 files changed, 67 insertions, 57 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 591301b257..d3b34dc027 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -657,7 +657,7 @@ void MemberExpression::dump(int indent) const m_property->dump(indent + 1); } -String MemberExpression::computed_property_name(Interpreter& interpreter) const +FlyString MemberExpression::computed_property_name(Interpreter& interpreter) const { if (!is_computed()) { ASSERT(m_property->is_identifier()); diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h index 2ff6a06722..3d58f0b395 100644 --- a/Libraries/LibJS/AST.h +++ b/Libraries/LibJS/AST.h @@ -30,6 +30,7 @@ #include <AK/NonnullRefPtrVector.h> #include <AK/RefPtr.h> #include <AK/String.h> +#include <AK/FlyString.h> #include <AK/Vector.h> #include <LibJS/Forward.h> #include <LibJS/Runtime/Value.h> @@ -130,13 +131,13 @@ class Expression : public ASTNode { class FunctionNode { public: - String name() const { return m_name; } + const FlyString& name() const { return m_name; } const ScopeNode& body() const { return *m_body; } - const Vector<String>& parameters() const { return m_parameters; }; + const Vector<FlyString>& parameters() const { return m_parameters; }; protected: - FunctionNode(String name, NonnullRefPtr<ScopeNode> body, Vector<String> parameters = {}) - : m_name(move(name)) + FunctionNode(const FlyString& name, NonnullRefPtr<ScopeNode> body, Vector<FlyString> parameters = {}) + : m_name(name) , m_body(move(body)) , m_parameters(move(parameters)) { @@ -145,9 +146,9 @@ protected: void dump(int indent, const char* class_name) const; private: - String m_name; + FlyString m_name; NonnullRefPtr<ScopeNode> m_body; - const Vector<String> m_parameters; + const Vector<FlyString> m_parameters; }; class FunctionDeclaration final @@ -156,7 +157,7 @@ class FunctionDeclaration final public: static bool must_have_name() { return true; } - FunctionDeclaration(String name, NonnullRefPtr<ScopeNode> body, Vector<String> parameters = {}) + FunctionDeclaration(String name, NonnullRefPtr<ScopeNode> body, Vector<FlyString> parameters = {}) : FunctionNode(move(name), move(body), move(parameters)) { } @@ -173,8 +174,8 @@ class FunctionExpression final : public Expression public: static bool must_have_name() { return false; } - FunctionExpression(String name, NonnullRefPtr<ScopeNode> body, Vector<String> parameters = {}) - : FunctionNode(move(name), move(body), move(parameters)) + FunctionExpression(const FlyString& name, NonnullRefPtr<ScopeNode> body, Vector<FlyString> parameters = {}) + : FunctionNode(name, move(body), move(parameters)) { } @@ -425,17 +426,13 @@ private: class NullLiteral final : public Literal { public: - explicit NullLiteral() - { - } + explicit NullLiteral() {} virtual Value execute(Interpreter&) const override; virtual void dump(int indent) const override; private: virtual const char* class_name() const override { return "NullLiteral"; } - - String m_value; }; class UndefinedLiteral final : public Literal { @@ -453,12 +450,12 @@ private: class Identifier final : public Expression { public: - explicit Identifier(String string) - : m_string(move(string)) + explicit Identifier(const FlyString& string) + : m_string(string) { } - const String& string() const { return m_string; } + const FlyString& string() const { return m_string; } virtual Value execute(Interpreter&) const override; virtual void dump(int indent) const override; @@ -467,7 +464,7 @@ public: private: virtual const char* class_name() const override { return "Identifier"; } - String m_string; + FlyString m_string; }; class CallExpression : public Expression { @@ -573,7 +570,7 @@ private: class ObjectExpression : public Expression { public: - ObjectExpression(HashMap<String, NonnullRefPtr<Expression>> properties = {}) + ObjectExpression(HashMap<FlyString, NonnullRefPtr<Expression>> properties = {}) : m_properties(move(properties)) { } @@ -584,7 +581,7 @@ public: private: virtual const char* class_name() const override { return "ObjectExpression"; } - HashMap<String, NonnullRefPtr<Expression>> m_properties; + HashMap<FlyString, NonnullRefPtr<Expression>> m_properties; }; class ArrayExpression : public Expression { @@ -621,7 +618,7 @@ public: const Expression& object() const { return *m_object; } const Expression& property() const { return *m_property; } - String computed_property_name(Interpreter&) const; + FlyString computed_property_name(Interpreter&) const; private: virtual bool is_member_expression() const override { return true; } diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 4485b5c86a..47a63646d1 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -65,7 +65,7 @@ Value Interpreter::run(const ScopeNode& scope_node, Vector<Argument> arguments, void Interpreter::enter_scope(const ScopeNode& scope_node, Vector<Argument> arguments, ScopeType scope_type) { - HashMap<String, Variable> scope_variables_with_declaration_type; + HashMap<FlyString, Variable> scope_variables_with_declaration_type; for (auto& argument : arguments) { scope_variables_with_declaration_type.set(argument.name, { argument.value, DeclarationType::Var }); } @@ -83,7 +83,7 @@ void Interpreter::do_return() dbg() << "FIXME: Implement Interpreter::do_return()"; } -void Interpreter::declare_variable(String name, DeclarationType declaration_type) +void Interpreter::declare_variable(const FlyString& name, DeclarationType declaration_type) { switch (declaration_type) { case DeclarationType::Var: @@ -110,7 +110,7 @@ void Interpreter::declare_variable(String name, DeclarationType declaration_type } } -void Interpreter::set_variable(String name, Value value, bool first_assignment) +void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment) { for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) { auto& scope = m_scope_stack.at(i); @@ -128,7 +128,7 @@ void Interpreter::set_variable(String name, Value value, bool first_assignment) global_object().put(move(name), move(value)); } -Value Interpreter::get_variable(const String& name) +Value Interpreter::get_variable(const FlyString& name) { for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) { auto& scope = m_scope_stack.at(i); diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index 52962c6d78..4c749fcd0e 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -28,6 +28,7 @@ #include <AK/HashMap.h> #include <AK/String.h> +#include <AK/FlyString.h> #include <AK/Vector.h> #include <LibJS/Forward.h> #include <LibJS/Heap/Heap.h> @@ -48,7 +49,7 @@ struct Variable { struct ScopeFrame { ScopeType type; NonnullRefPtr<ScopeNode> scope_node; - HashMap<String, Variable> variables; + HashMap<FlyString, Variable> variables; }; struct CallFrame { @@ -57,7 +58,7 @@ struct CallFrame { }; struct Argument { - String name; + FlyString name; Value value; }; @@ -75,9 +76,9 @@ public: void do_return(); - Value get_variable(const String& name); - void set_variable(String name, Value, bool first_assignment = false); - void declare_variable(String name, DeclarationType); + Value get_variable(const FlyString& name); + void set_variable(const FlyString& name, Value, bool first_assignment = false); + void declare_variable(const FlyString& name, DeclarationType); void gather_roots(Badge<Heap>, HashTable<Cell*>&); @@ -86,7 +87,11 @@ public: Value call(Function*, Value this_value, const Vector<Value>& arguments); - CallFrame& push_call_frame() { m_call_stack.append({ js_undefined(), {} }); return m_call_stack.last(); } + CallFrame& push_call_frame() + { + m_call_stack.append({ js_undefined(), {} }); + return m_call_stack.last(); + } void pop_call_frame() { m_call_stack.take_last(); } Value this_value() const { diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 7ef64a3d2b..6b5a09cb18 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -271,7 +271,7 @@ NonnullRefPtr<Expression> Parser::parse_unary_prefixed_expression() NonnullRefPtr<ObjectExpression> Parser::parse_object_expression() { - HashMap<String, NonnullRefPtr<Expression>> properties; + HashMap<FlyString, NonnullRefPtr<Expression>> properties; consume(TokenType::CurlyOpen); while (!match(TokenType::CurlyClose)) { @@ -471,7 +471,7 @@ NonnullRefPtr<FunctionNodeType> Parser::parse_function_node() name = consume(TokenType::Identifier).value(); } consume(TokenType::ParenOpen); - Vector<String> parameters; + Vector<FlyString> parameters; while (match(TokenType::Identifier)) { auto parameter = consume(TokenType::Identifier).value(); parameters.append(parameter); diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp index e8cfb28f1b..cab66eb3a5 100644 --- a/Libraries/LibJS/Runtime/Array.cpp +++ b/Libraries/LibJS/Runtime/Array.cpp @@ -59,7 +59,7 @@ void Array::visit_children(Cell::Visitor& visitor) visitor.visit(element); } -Optional<Value> Array::get_own_property(const String& property_name) const +Optional<Value> Array::get_own_property(const FlyString& property_name) const { bool ok; i32 index = property_name.to_int(ok); @@ -70,7 +70,7 @@ Optional<Value> Array::get_own_property(const String& property_name) const return Object::get_own_property(property_name); } -bool Array::put_own_property(const String& property_name, Value value) +bool Array::put_own_property(const FlyString& property_name, Value value) { bool ok; i32 index = property_name.to_int(ok); diff --git a/Libraries/LibJS/Runtime/Array.h b/Libraries/LibJS/Runtime/Array.h index 828b70c33f..930b7a586c 100644 --- a/Libraries/LibJS/Runtime/Array.h +++ b/Libraries/LibJS/Runtime/Array.h @@ -45,8 +45,8 @@ private: virtual const char* class_name() const override { return "Array"; } virtual void visit_children(Cell::Visitor&) override; virtual bool is_array() const override { return true; } - virtual Optional<Value> get_own_property(const String& property_name) const override; - virtual bool put_own_property(const String& property_name, Value) override; + virtual Optional<Value> get_own_property(const FlyString& property_name) const override; + virtual bool put_own_property(const FlyString& property_name, Value) override; Vector<Value> m_elements; }; diff --git a/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Libraries/LibJS/Runtime/ConsoleObject.cpp index b9c3a34ca7..c5e307a982 100644 --- a/Libraries/LibJS/Runtime/ConsoleObject.cpp +++ b/Libraries/LibJS/Runtime/ConsoleObject.cpp @@ -25,6 +25,7 @@ */ #include <AK/Function.h> +#include <AK/FlyString.h> #include <LibJS/Runtime/ConsoleObject.h> #include <stdio.h> diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index 23ef4d6bda..4c2e3764fb 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -25,6 +25,7 @@ */ #include <AK/Function.h> +#include <AK/FlyString.h> #include <LibJS/Runtime/MathObject.h> namespace JS { diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index 2cf1f7cc0a..d72851ad50 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -44,7 +44,7 @@ Object::~Object() { } -Optional<Value> Object::get_own_property(const String& property_name) const +Optional<Value> Object::get_own_property(const FlyString& property_name) const { auto value_here = m_properties.get(property_name); if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property()) @@ -52,7 +52,7 @@ Optional<Value> Object::get_own_property(const String& property_name) const return value_here; } -bool Object::put_own_property(const String& property_name, Value value) +bool Object::put_own_property(const FlyString& property_name, Value value) { auto value_here = m_properties.get(property_name); if (value_here.has_value() && value_here.value().is_object() && value_here.value().as_object()->is_native_property()) { @@ -63,7 +63,7 @@ bool Object::put_own_property(const String& property_name, Value value) return true; } -Value Object::get(const String& property_name) const +Value Object::get(const FlyString& property_name) const { const Object* object = this; while (object) { @@ -75,7 +75,7 @@ Value Object::get(const String& property_name) const return js_undefined(); } -void Object::put(const String& property_name, Value value) +void Object::put(const FlyString& property_name, Value value) { Object* object = this; while (object) { @@ -93,12 +93,12 @@ void Object::put(const String& property_name, Value value) put_own_property(property_name, value); } -void Object::put_native_function(String property_name, AK::Function<Value(Object*, Vector<Value>)> native_function) +void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Object*, Vector<Value>)> native_function) { put(property_name, heap().allocate<NativeFunction>(move(native_function))); } -void Object::put_native_property(String property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter) +void Object::put_native_property(const FlyString& property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter) { put(property_name, heap().allocate<NativeProperty>(move(getter), move(setter))); } @@ -112,7 +112,7 @@ void Object::visit_children(Cell::Visitor& visitor) visitor.visit(it.value); } -bool Object::has_own_property(const String& property_name) const +bool Object::has_own_property(const FlyString& property_name) const { return m_properties.get(property_name).has_value(); } diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index 9e61676c51..fd98b0e44f 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -40,14 +40,14 @@ public: Object(); virtual ~Object(); - Value get(const String& property_name) const; - void put(const String& property_name, Value); + Value get(const FlyString& property_name) const; + void put(const FlyString& property_name, Value); - virtual Optional<Value> get_own_property(const String& property_name) const; - virtual bool put_own_property(const String& property_name, Value); + virtual Optional<Value> get_own_property(const FlyString& property_name) const; + virtual bool put_own_property(const FlyString& property_name, Value); - void put_native_function(String property_name, AK::Function<Value(Object*, Vector<Value>)>); - void put_native_property(String property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter); + void put_native_function(const FlyString& property_name, AK::Function<Value(Object*, Vector<Value>)>); + void put_native_property(const FlyString& property_name, AK::Function<Value(Object*)> getter, AK::Function<void(Object*, Value)> setter); virtual bool is_array() const { return false; } virtual bool is_function() const { return false; } @@ -62,7 +62,7 @@ public: const Object* prototype() const { return m_prototype; } void set_prototype(Object* prototype) { m_prototype = prototype; } - bool has_own_property(const String& property_name) const; + bool has_own_property(const FlyString& property_name) const; enum class PreferredType { Default, String, @@ -74,7 +74,7 @@ public: virtual Value to_string() const; private: - HashMap<String, Value> m_properties; + HashMap<FlyString, Value> m_properties; Object* m_prototype { nullptr }; }; diff --git a/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Libraries/LibJS/Runtime/ScriptFunction.cpp index b7a766bfe3..98883d7587 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -31,7 +31,7 @@ namespace JS { -ScriptFunction::ScriptFunction(const ScopeNode& body, Vector<String> parameters) +ScriptFunction::ScriptFunction(const ScopeNode& body, Vector<FlyString> parameters) : m_body(body) , m_parameters(move(parameters)) { diff --git a/Libraries/LibJS/Runtime/ScriptFunction.h b/Libraries/LibJS/Runtime/ScriptFunction.h index 4d3fc4b2c1..b5799798fd 100644 --- a/Libraries/LibJS/Runtime/ScriptFunction.h +++ b/Libraries/LibJS/Runtime/ScriptFunction.h @@ -32,11 +32,11 @@ namespace JS { class ScriptFunction final : public Function { public: - ScriptFunction(const ScopeNode& body, Vector<String> parameters = {}); + ScriptFunction(const ScopeNode& body, Vector<FlyString> parameters = {}); virtual ~ScriptFunction(); const ScopeNode& body() const { return m_body; } - const Vector<String>& parameters() const { return m_parameters; }; + const Vector<FlyString>& parameters() const { return m_parameters; }; virtual Value call(Interpreter&, const Vector<Value>&) override; @@ -45,7 +45,7 @@ private: virtual const char* class_name() const override { return "ScriptFunction"; } NonnullRefPtr<ScopeNode> m_body; - const Vector<String> m_parameters; + const Vector<FlyString> m_parameters; }; } diff --git a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp index 5944001880..a5c6a576e4 100644 --- a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp +++ b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp @@ -25,6 +25,7 @@ */ #include <AK/Function.h> +#include <AK/FlyString.h> #include <LibJS/Runtime/PrimitiveString.h> #include <LibJS/Runtime/Value.h> #include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h> diff --git a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp index 8d9dcfa295..737a8dca66 100644 --- a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp +++ b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <AK/FlyString.h> #include <LibJS/Runtime/PrimitiveString.h> #include <LibJS/Runtime/Value.h> #include <LibWeb/Bindings/DocumentWrapper.h> diff --git a/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp b/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp index 972f7d524a..452be45599 100644 --- a/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp +++ b/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp @@ -25,6 +25,7 @@ */ #include <AK/Function.h> +#include <AK/FlyString.h> #include <LibJS/Runtime/Function.h> #include <LibWeb/Bindings/EventListenerWrapper.h> #include <LibWeb/Bindings/EventTargetWrapper.h> diff --git a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp index 8ffcc4f77c..8b99dc3768 100644 --- a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp +++ b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp @@ -25,6 +25,7 @@ */ #include <AK/Function.h> +#include <AK/FlyString.h> #include <LibJS/Runtime/PrimitiveString.h> #include <LibJS/Runtime/Value.h> #include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h> diff --git a/Libraries/LibWeb/Bindings/MouseEventWrapper.cpp b/Libraries/LibWeb/Bindings/MouseEventWrapper.cpp index c3d652589a..f002d50732 100644 --- a/Libraries/LibWeb/Bindings/MouseEventWrapper.cpp +++ b/Libraries/LibWeb/Bindings/MouseEventWrapper.cpp @@ -25,6 +25,7 @@ */ #include <AK/Function.h> +#include <AK/FlyString.h> #include <LibJS/Runtime/Function.h> #include <LibWeb/Bindings/MouseEventWrapper.h> #include <LibWeb/DOM/MouseEvent.h> diff --git a/Libraries/LibWeb/Bindings/NodeWrapper.cpp b/Libraries/LibWeb/Bindings/NodeWrapper.cpp index aeb84b4e3d..badf650cc0 100644 --- a/Libraries/LibWeb/Bindings/NodeWrapper.cpp +++ b/Libraries/LibWeb/Bindings/NodeWrapper.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <AK/FlyString.h> #include <LibJS/Runtime/PrimitiveString.h> #include <LibJS/Runtime/Value.h> #include <LibWeb/Bindings/DocumentWrapper.h> |