From 589c3771e9e0d3afb70caccd080491221925d524 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Sun, 27 Mar 2022 19:59:25 +0100 Subject: [PATCH] LibJS: Only store MemberExpression object when loading a computed prop When calling emit_load_from_reference with a MemberExpression, it is only necessary to store the result of evaluating MemberExpression's object when performing computed property lookup. This allows us to skip unnecessary stores for identifier lookup. For example, this would generate 3 unnecessary stores: ``` > Temporal.PlainDateTime.prototype.add JS::Bytecode::Executable (REPL) 1: [ 0] GetVariable 0 (Temporal) [ 28] Store $2 [ 30] GetById 1 (PlainDateTime) [ 40] Store $3 [ 48] GetById 2 (prototype) [ 58] Store $4 [ 60] GetById 3 (add) ``` With this, it generates: ``` > Temporal.PlainDateTime.prototype.add JS::Bytecode::Executable (REPL) 1: [ 0] GetVariable 0 (Temporal) [ 28] GetById 1 (PlainDateTime) [ 38] GetById 2 (prototype) [ 48] GetById 3 (add) ``` --- Userland/Libraries/LibJS/Bytecode/Generator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.cpp b/Userland/Libraries/LibJS/Bytecode/Generator.cpp index f6221072b79..36d6b54c413 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Generator.cpp @@ -137,10 +137,10 @@ CodeGenerationErrorOr Generator::emit_load_from_reference(JS::ASTNode cons auto& expression = static_cast(node); TRY(expression.object().generate_bytecode(*this)); - auto object_reg = allocate_register(); - emit(object_reg); - if (expression.is_computed()) { + auto object_reg = allocate_register(); + emit(object_reg); + TRY(expression.property().generate_bytecode(*this)); emit(object_reg); } else if (expression.property().is_identifier()) {