瀏覽代碼

LibJS: Remove m_first_invalid_property_range from ObjectExpression

This was state only used by the parser to output an error with
appropriate location. This shrinks the size of ObjectExpression from
120 bytes down to just 56. This saves roughly 2.5 MiB when loading
twitter.
davidot 2 年之前
父節點
當前提交
2c26ee89ac
共有 3 個文件被更改,包括 12 次插入9 次删除
  1. 1 5
      Userland/Libraries/LibJS/AST.h
  2. 10 4
      Userland/Libraries/LibJS/Parser.cpp
  3. 1 0
      Userland/Libraries/LibJS/Parser.h

+ 1 - 5
Userland/Libraries/LibJS/AST.h

@@ -1691,10 +1691,9 @@ private:
 
 
 class ObjectExpression final : public Expression {
 class ObjectExpression final : public Expression {
 public:
 public:
-    explicit ObjectExpression(SourceRange source_range, NonnullRefPtrVector<ObjectProperty> properties = {}, Optional<SourceRange> first_invalid_property_range = {})
+    explicit ObjectExpression(SourceRange source_range, NonnullRefPtrVector<ObjectProperty> properties = {})
         : Expression(source_range)
         : Expression(source_range)
         , m_properties(move(properties))
         , m_properties(move(properties))
-        , m_first_invalid_property_range(move(first_invalid_property_range))
     {
     {
     }
     }
 
 
@@ -1702,13 +1701,10 @@ public:
     virtual void dump(int indent) const override;
     virtual void dump(int indent) const override;
     virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
     virtual Bytecode::CodeGenerationErrorOr<void> generate_bytecode(Bytecode::Generator&) const override;
 
 
-    Optional<SourceRange> const& invalid_property_range() const { return m_first_invalid_property_range; }
-
 private:
 private:
     virtual bool is_object_expression() const override { return true; }
     virtual bool is_object_expression() const override { return true; }
 
 
     NonnullRefPtrVector<ObjectProperty> m_properties;
     NonnullRefPtrVector<ObjectProperty> m_properties;
-    Optional<SourceRange> m_first_invalid_property_range;
 };
 };
 
 
 class ArrayExpression final : public Expression {
 class ArrayExpression final : public Expression {

+ 10 - 4
Userland/Libraries/LibJS/Parser.cpp

@@ -1799,10 +1799,16 @@ NonnullRefPtr<ObjectExpression> Parser::parse_object_expression()
     }
     }
 
 
     consume(TokenType::CurlyClose);
     consume(TokenType::CurlyClose);
+
+    if (invalid_object_literal_property_range.has_value()) {
+        size_t object_expression_offset = rule_start.position().offset;
+        VERIFY(!m_state.invalid_property_range_in_object_expression.contains(object_expression_offset));
+        m_state.invalid_property_range_in_object_expression.set(object_expression_offset, invalid_object_literal_property_range->start);
+    }
+
     return create_ast_node<ObjectExpression>(
     return create_ast_node<ObjectExpression>(
         { m_source_code, rule_start.position(), position() },
         { m_source_code, rule_start.position(), position() },
-        move(properties),
-        move(invalid_object_literal_property_range));
+        move(properties));
 }
 }
 
 
 NonnullRefPtr<ArrayExpression> Parser::parse_array_expression()
 NonnullRefPtr<ArrayExpression> Parser::parse_array_expression()
@@ -1939,8 +1945,8 @@ NonnullRefPtr<Expression> Parser::parse_expression(int min_precedence, Associati
     auto [expression, should_continue_parsing] = parse_primary_expression();
     auto [expression, should_continue_parsing] = parse_primary_expression();
     auto check_for_invalid_object_property = [&](auto& expression) {
     auto check_for_invalid_object_property = [&](auto& expression) {
         if (is<ObjectExpression>(*expression)) {
         if (is<ObjectExpression>(*expression)) {
-            if (auto range = static_cast<ObjectExpression&>(*expression).invalid_property_range(); range.has_value())
-                syntax_error("Invalid property in object literal", range->start);
+            if (auto start_offset = m_state.invalid_property_range_in_object_expression.get(expression->start_offset()); start_offset.has_value())
+                syntax_error("Invalid property in object literal", start_offset.value());
         }
         }
     };
     };
     if (is<Identifier>(*expression) && m_state.current_scope_pusher) {
     if (is<Identifier>(*expression) && m_state.current_scope_pusher) {

+ 1 - 0
Userland/Libraries/LibJS/Parser.h

@@ -282,6 +282,7 @@ private:
         ScopePusher* current_scope_pusher { nullptr };
         ScopePusher* current_scope_pusher { nullptr };
 
 
         HashMap<StringView, Optional<Position>> labels_in_scope;
         HashMap<StringView, Optional<Position>> labels_in_scope;
+        HashMap<size_t, Position> invalid_property_range_in_object_expression;
         HashTable<StringView>* referenced_private_names { nullptr };
         HashTable<StringView>* referenced_private_names { nullptr };
 
 
         bool strict_mode { false };
         bool strict_mode { false };