Created
October 8, 2021 22:44
-
-
Save awesomekling/9c7c28d36f538da691eda778e5fe57d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit eafbf372d0bb0013f50f0952f4115dd359772095 | |
Author: Andreas Kling <kling@serenityos.org> | |
Date: Sat Oct 9 00:42:10 2021 +0200 | |
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. | |
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]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment