diff options
author | Andreas Kling <kling@serenityos.org> | 2021-10-09 00:42:10 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-09 00:42:10 +0200 |
commit | eafbf372d0bb0013f50f0952f4115dd359772095 (patch) | |
tree | 678286668920a191ad45967be51b599fa8a59f2c /Userland/Libraries/LibJS | |
parent | 9d06448bc727738f3347ed29033eb4356ac3e916 (diff) | |
download | serenity-eafbf372d0bb0013f50f0952f4115dd359772095.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r-- | Userland/Libraries/LibJS/AST.cpp | 15 |
1 files changed, 8 insertions, 7 deletions
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<TemporaryChange<Environment*>> 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<size_t> first_passing_case; for (size_t i = 0; i < m_cases.size(); ++i) { auto& switch_case = m_cases[i]; |