From eafbf372d0bb0013f50f0952f4115dd359772095 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 9 Oct 2021 00:42:10 +0200 Subject: LibJS: Elide empty declarative environments inside switch statements Most switch statements don't have any lexically scoped declarations, so let's avoid allocating an environment in the common case where we don't have to. --- Userland/Libraries/LibJS/AST.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'Userland/Libraries/LibJS') diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 99803c807e..6e82d9bc7f 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -2856,14 +2856,15 @@ Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_ob if (interpreter.exception()) return {}; - auto* old_environment = interpreter.lexical_environment(); - ScopeGuard restore_environment = [&] { - interpreter.vm().running_execution_context().lexical_environment = old_environment; - }; - auto* block_environment = new_declarative_environment(*old_environment); - block_declaration_instantiation(global_object, block_environment); + // Optimization: Avoid creating a lexical environment if there are no lexical declarations. + Optional> lexical_environment_changer; + if (has_lexical_declarations()) { + auto* old_environment = interpreter.lexical_environment(); + auto* block_environment = new_declarative_environment(*old_environment); + block_declaration_instantiation(global_object, block_environment); + lexical_environment_changer.emplace(interpreter.vm().running_execution_context().lexical_environment, block_environment); + } - interpreter.vm().running_execution_context().lexical_environment = block_environment; Optional first_passing_case; for (size_t i = 0; i < m_cases.size(); ++i) { auto& switch_case = m_cases[i]; -- cgit v1.2.3