From 80f090056515d8bb29be1f9bd1f005a4e8ec84a9 Mon Sep 17 00:00:00 2001 From: Jonne Ransijn Date: Thu, 31 Oct 2024 22:46:44 +0100 Subject: [PATCH] LibJS: Share argument values buffer between calls This buffer is being created and then immediately destroyed, let's reuse it to reduce memory allocations. --- Userland/Libraries/LibJS/Bytecode/Interpreter.cpp | 5 ++--- Userland/Libraries/LibJS/Bytecode/Interpreter.h | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 824ca3b6fa0..5495d5b5dc5 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -2545,10 +2545,9 @@ ThrowCompletionOr Call::execute_impl(Bytecode::Interpreter& interpreter) c return {}; } - Vector argument_values; - argument_values.ensure_capacity(m_argument_count); + auto argument_values = interpreter.allocate_argument_values(m_argument_count); for (size_t i = 0; i < m_argument_count; ++i) - argument_values.unchecked_append(interpreter.get(m_arguments[i])); + argument_values[i] = interpreter.get(m_arguments[i]); interpreter.set(dst(), TRY(perform_call(interpreter, interpreter.get(m_this_value), call_type(), callee, argument_values))); return {}; } diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.h b/Userland/Libraries/LibJS/Bytecode/Interpreter.h index 1da7aa77cb4..98d6598ce01 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.h +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.h @@ -77,6 +77,11 @@ public: Executable& current_executable() { return *m_current_executable; } Executable const& current_executable() const { return *m_current_executable; } Optional program_counter() const { return m_program_counter; } + Span allocate_argument_values(size_t argument_count) + { + m_argument_values_buffer.resize_and_keep_capacity(argument_count); + return m_argument_values_buffer.span(); + } ExecutionContext& running_execution_context() { return *m_running_execution_context; } @@ -98,6 +103,7 @@ private: Optional m_program_counter; Span m_arguments; Span m_registers_and_constants_and_locals; + Vector m_argument_values_buffer; ExecutionContext* m_running_execution_context { nullptr }; };