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)
```
This commit is contained in:
Luke Wilde 2022-03-27 19:59:25 +01:00 committed by Andreas Kling
parent 88901182b8
commit 589c3771e9
Notes: sideshowbarker 2024-07-17 16:38:14 +09:00

View file

@ -137,10 +137,10 @@ CodeGenerationErrorOr<void> Generator::emit_load_from_reference(JS::ASTNode cons
auto& expression = static_cast<MemberExpression const&>(node);
TRY(expression.object().generate_bytecode(*this));
auto object_reg = allocate_register();
emit<Bytecode::Op::Store>(object_reg);
if (expression.is_computed()) {
auto object_reg = allocate_register();
emit<Bytecode::Op::Store>(object_reg);
TRY(expression.property().generate_bytecode(*this));
emit<Bytecode::Op::GetByValue>(object_reg);
} else if (expression.property().is_identifier()) {