From 4ba2eb8fe531094ed783c0267a9603c91dfc645e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 7 Jun 2021 15:19:48 +0200 Subject: [PATCH] LibJS: Cache generated bytecode for ScriptFunction It's silly to generate new bytecode every time you call a function. Let's just cache the code instead. :^) --- .../Libraries/LibJS/Runtime/ScriptFunction.cpp | 14 ++++++++------ Userland/Libraries/LibJS/Runtime/ScriptFunction.h | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp index 9447544d407..3a4e83f4cbf 100644 --- a/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -151,13 +151,15 @@ Value ScriptFunction::execute_function_body() if (bytecode_interpreter) { prepare_arguments(); - auto block = Bytecode::Generator::generate(m_body); - VERIFY(block); - if constexpr (JS_BYTECODE_DEBUG) { - dbgln("Compiled Bytecode::Block for function '{}':", m_name); - block->dump(); + if (!m_bytecode_block) { + m_bytecode_block = Bytecode::Generator::generate(m_body); + VERIFY(m_bytecode_block); + if constexpr (JS_BYTECODE_DEBUG) { + dbgln("Compiled Bytecode::Block for function '{}':", m_name); + m_bytecode_block->dump(); + } } - return bytecode_interpreter->run(*block); + return bytecode_interpreter->run(*m_bytecode_block); } else { OwnPtr local_interpreter; ast_interpreter = vm.interpreter_if_exists(); diff --git a/Userland/Libraries/LibJS/Runtime/ScriptFunction.h b/Userland/Libraries/LibJS/Runtime/ScriptFunction.h index 3e55cd276d6..d866c788786 100644 --- a/Userland/Libraries/LibJS/Runtime/ScriptFunction.h +++ b/Userland/Libraries/LibJS/Runtime/ScriptFunction.h @@ -47,6 +47,7 @@ private: FlyString m_name; NonnullRefPtr m_body; const Vector m_parameters; + OwnPtr m_bytecode_block; ScopeObject* m_parent_scope { nullptr }; i32 m_function_length { 0 }; bool m_is_strict { false };