From c833885fb59a260e3003ac66d892aa126b47dc26 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 26 Sep 2023 14:42:30 +0200 Subject: [PATCH] LibJS: Keep cached `this` value in a call frame register Just moving more things to call frame registers.. --- Userland/Libraries/LibJS/Bytecode/Generator.h | 2 +- Userland/Libraries/LibJS/Bytecode/Interpreter.cpp | 2 -- Userland/Libraries/LibJS/Bytecode/Interpreter.h | 3 --- Userland/Libraries/LibJS/Bytecode/Op.cpp | 7 ++++--- Userland/Libraries/LibJS/Bytecode/Register.h | 8 ++++++++ 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.h b/Userland/Libraries/LibJS/Bytecode/Generator.h index 9fdff81bf8..9a086aed2b 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.h +++ b/Userland/Libraries/LibJS/Bytecode/Generator.h @@ -252,7 +252,7 @@ private: NonnullOwnPtr m_identifier_table; NonnullOwnPtr m_regex_table; - u32 m_next_register { 3 }; + u32 m_next_register { Register::reserved_register_count }; u32 m_next_block { 1 }; u32 m_next_property_lookup_cache { 0 }; u32 m_next_global_variable_cache { 0 }; diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 27e4084841..c5495762f4 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -179,8 +179,6 @@ Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable& executa else push_call_frame(make(), executable.number_of_registers); - TemporaryChange restore_this_value { m_this_value, {} }; - for (;;) { auto pc = InstructionStreamIterator { m_current_block->instruction_stream(), m_current_executable }; TemporaryChange temp_change { m_pc, Optional(pc) }; diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.h b/Userland/Libraries/LibJS/Bytecode/Interpreter.h index 4ca53a6e0f..b27e5a6a5a 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.h +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.h @@ -90,8 +90,6 @@ public: auto& instruction_stream_iterator() const { return m_pc; } DeprecatedString debug_position() const; - Optional& this_value() { return m_this_value; } - void visit_edges(Cell::Visitor&); private: @@ -116,7 +114,6 @@ private: Span m_current_call_frame; Optional m_pending_jump; BasicBlock const* m_scheduled_jump { nullptr }; - Optional m_this_value; Optional m_return_value; Executable* m_current_executable { nullptr }; BasicBlock const* m_current_block { nullptr }; diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index 903201d33a..bafe87a65a 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -767,13 +767,14 @@ ThrowCompletionOr Jump::execute_impl(Bytecode::Interpreter& interpreter) c ThrowCompletionOr ResolveThisBinding::execute_impl(Bytecode::Interpreter& interpreter) const { - if (!interpreter.this_value().has_value()) { + auto& cached_this_value = interpreter.reg(Register::this_value()); + if (cached_this_value.is_empty()) { // OPTIMIZATION: Because the value of 'this' cannot be reassigned during a function execution, it's // resolved once and then saved for subsequent use. auto& vm = interpreter.vm(); - interpreter.this_value() = TRY(vm.resolve_this_binding()); + cached_this_value = TRY(vm.resolve_this_binding()); } - interpreter.accumulator() = interpreter.this_value().value(); + interpreter.accumulator() = cached_this_value; return {}; } diff --git a/Userland/Libraries/LibJS/Bytecode/Register.h b/Userland/Libraries/LibJS/Bytecode/Register.h index e79f67da71..bb951852cc 100644 --- a/Userland/Libraries/LibJS/Bytecode/Register.h +++ b/Userland/Libraries/LibJS/Bytecode/Register.h @@ -33,6 +33,14 @@ public: return Register(exception_index); } + static constexpr Register this_value() + { + constexpr u32 this_value_index = 3; + return Register(this_value_index); + } + + static constexpr u32 reserved_register_count = 4; + constexpr explicit Register(u32 index) : m_index(index) {