From 2c26ee89acd183770907e58e231d2bc8a125d0df Mon Sep 17 00:00:00 2001 From: davidot Date: Sun, 27 Nov 2022 02:24:38 +0100 Subject: [PATCH] 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. --- Userland/Libraries/LibJS/AST.h | 6 +----- Userland/Libraries/LibJS/Parser.cpp | 14 ++++++++++---- Userland/Libraries/LibJS/Parser.h | 1 + 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibJS/AST.h b/Userland/Libraries/LibJS/AST.h index 123905d6d8..e898132ba2 100644 --- a/Userland/Libraries/LibJS/AST.h +++ b/Userland/Libraries/LibJS/AST.h @@ -1691,10 +1691,9 @@ private: class ObjectExpression final : public Expression { public: - explicit ObjectExpression(SourceRange source_range, NonnullRefPtrVector properties = {}, Optional first_invalid_property_range = {}) + explicit ObjectExpression(SourceRange source_range, NonnullRefPtrVector properties = {}) : Expression(source_range) , 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 Bytecode::CodeGenerationErrorOr generate_bytecode(Bytecode::Generator&) const override; - 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; }; class ArrayExpression final : public Expression { diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp index 8e41d8882f..6614094f4b 100644 --- a/Userland/Libraries/LibJS/Parser.cpp +++ b/Userland/Libraries/LibJS/Parser.cpp @@ -1799,10 +1799,16 @@ NonnullRefPtr Parser::parse_object_expression() } 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( { m_source_code, rule_start.position(), position() }, - move(properties), - move(invalid_object_literal_property_range)); + move(properties)); } NonnullRefPtr Parser::parse_array_expression() @@ -1939,8 +1945,8 @@ NonnullRefPtr Parser::parse_expression(int min_precedence, Associati auto [expression, should_continue_parsing] = parse_primary_expression(); auto check_for_invalid_object_property = [&](auto& expression) { if (is(*expression)) { - if (auto range = static_cast(*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(*expression) && m_state.current_scope_pusher) { diff --git a/Userland/Libraries/LibJS/Parser.h b/Userland/Libraries/LibJS/Parser.h index 2f0e73db68..25b7513cec 100644 --- a/Userland/Libraries/LibJS/Parser.h +++ b/Userland/Libraries/LibJS/Parser.h @@ -282,6 +282,7 @@ private: ScopePusher* current_scope_pusher { nullptr }; HashMap> labels_in_scope; + HashMap invalid_property_range_in_object_expression; HashTable* referenced_private_names { nullptr }; bool strict_mode { false };