LibJS: Emit PutById instead of PutByValue when key is string literal
Some checks failed
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Build Dev Container Image / build (push) Has been cancelled

Basically convert o["foo"]=x into o.foo=x when emitting bytecode.
These are effectively the same thing, and the latter format opts
into using an inline cache for the property lookups.
This commit is contained in:
Andreas Kling 2025-04-03 14:29:44 +02:00 committed by Andreas Kling
commit 3169747989
Notes: github-actions[bot] 2025-04-03 16:48:32 +00:00
3 changed files with 34 additions and 7 deletions

View file

@ -625,9 +625,9 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> AssignmentExpression::g
if (expression.is_computed()) { if (expression.is_computed()) {
if (!lhs_is_super_expression) if (!lhs_is_super_expression)
generator.emit<Bytecode::Op::PutByValue>(*base, *computed_property, rval, Bytecode::Op::PropertyKind::KeyValue, move(base_identifier)); generator.emit_put_by_value(*base, *computed_property, rval, Bytecode::Op::PropertyKind::KeyValue, move(base_identifier));
else else
generator.emit<Bytecode::Op::PutByValueWithThis>(*base, *computed_property, *this_value, rval); generator.emit_put_by_value_with_this(*base, *computed_property, *this_value, rval, Op::PropertyKind::KeyValue);
} else if (expression.property().is_identifier()) { } else if (expression.property().is_identifier()) {
auto identifier_table_ref = generator.intern_identifier(as<Identifier>(expression.property()).string()); auto identifier_table_ref = generator.intern_identifier(as<Identifier>(expression.property()).string());
if (!lhs_is_super_expression) if (!lhs_is_super_expression)
@ -1180,7 +1180,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> ObjectExpression::gener
auto property_name = TRY(property->key().generate_bytecode(generator)).value(); auto property_name = TRY(property->key().generate_bytecode(generator)).value();
auto value = TRY(property->value().generate_bytecode(generator)).value(); auto value = TRY(property->value().generate_bytecode(generator)).value();
generator.emit<Bytecode::Op::PutByValue>(object, property_name, value, property_kind); generator.emit_put_by_value(object, property_name, value, property_kind, {});
} }
} }

View file

@ -760,7 +760,7 @@ CodeGenerationErrorOr<void> Generator::emit_store_to_reference(JS::ASTNode const
if (super_reference.referenced_name.has_value()) { if (super_reference.referenced_name.has_value()) {
// 5. Let propertyKey be ? ToPropertyKey(propertyNameValue). // 5. Let propertyKey be ? ToPropertyKey(propertyNameValue).
// FIXME: This does ToPropertyKey out of order, which is observable by Symbol.toPrimitive! // FIXME: This does ToPropertyKey out of order, which is observable by Symbol.toPrimitive!
emit<Bytecode::Op::PutByValueWithThis>(*super_reference.base, *super_reference.referenced_name, *super_reference.this_value, value); emit_put_by_value_with_this(*super_reference.base, *super_reference.referenced_name, *super_reference.this_value, value, Op::PropertyKind::KeyValue);
} else { } else {
// 3. Let propertyKey be StringValue of IdentifierName. // 3. Let propertyKey be StringValue of IdentifierName.
auto identifier_table_ref = intern_identifier(as<Identifier>(expression.property()).string()); auto identifier_table_ref = intern_identifier(as<Identifier>(expression.property()).string());
@ -771,7 +771,7 @@ CodeGenerationErrorOr<void> Generator::emit_store_to_reference(JS::ASTNode const
if (expression.is_computed()) { if (expression.is_computed()) {
auto property = TRY(expression.property().generate_bytecode(*this)).value(); auto property = TRY(expression.property().generate_bytecode(*this)).value();
emit<Bytecode::Op::PutByValue>(object, property, value); emit_put_by_value(object, property, value, Op::PropertyKind::KeyValue, {});
} else if (expression.property().is_identifier()) { } else if (expression.property().is_identifier()) {
auto identifier_table_ref = intern_identifier(as<Identifier>(expression.property()).string()); auto identifier_table_ref = intern_identifier(as<Identifier>(expression.property()).string());
emit<Bytecode::Op::PutById>(object, identifier_table_ref, value, Bytecode::Op::PropertyKind::KeyValue, next_property_lookup_cache()); emit<Bytecode::Op::PutById>(object, identifier_table_ref, value, Bytecode::Op::PropertyKind::KeyValue, next_property_lookup_cache());
@ -809,9 +809,9 @@ CodeGenerationErrorOr<void> Generator::emit_store_to_reference(ReferenceOperands
return {}; return {};
} }
if (reference.base == reference.this_value) if (reference.base == reference.this_value)
emit<Bytecode::Op::PutByValue>(*reference.base, *reference.referenced_name, value); emit_put_by_value(*reference.base, *reference.referenced_name, value, Op::PropertyKind::KeyValue, {});
else else
emit<Bytecode::Op::PutByValueWithThis>(*reference.base, *reference.referenced_name, *reference.this_value, value); emit_put_by_value_with_this(*reference.base, *reference.referenced_name, *reference.this_value, value, Op::PropertyKind::KeyValue);
return {}; return {};
} }
@ -1146,6 +1146,30 @@ void Generator::emit_get_by_value_with_this(ScopedOperand dst, ScopedOperand bas
emit<Op::GetByValueWithThis>(dst, base, property, this_value); emit<Op::GetByValueWithThis>(dst, base, property, this_value);
} }
void Generator::emit_put_by_value(ScopedOperand base, ScopedOperand property, ScopedOperand src, Bytecode::Op::PropertyKind kind, Optional<IdentifierTableIndex> base_identifier)
{
if (property.operand().is_constant() && get_constant(property).is_string()) {
auto property_key = MUST(get_constant(property).to_property_key(vm()));
if (property_key.is_string()) {
emit<Op::PutById>(base, intern_identifier(property_key.as_string()), src, kind, m_next_property_lookup_cache++, base_identifier);
return;
}
}
emit<Op::PutByValue>(base, property, src, kind, base_identifier);
}
void Generator::emit_put_by_value_with_this(ScopedOperand base, ScopedOperand property, ScopedOperand this_value, ScopedOperand src, Bytecode::Op::PropertyKind kind)
{
if (property.operand().is_constant() && get_constant(property).is_string()) {
auto property_key = MUST(get_constant(property).to_property_key(vm()));
if (property_key.is_string()) {
emit<Op::PutByIdWithThis>(base, this_value, intern_identifier(property_key.as_string()), src, kind, m_next_property_lookup_cache++);
return;
}
}
emit<Bytecode::Op::PutByValueWithThis>(base, property, this_value, src, kind);
}
void Generator::emit_iterator_value(ScopedOperand dst, ScopedOperand result) void Generator::emit_iterator_value(ScopedOperand dst, ScopedOperand result)
{ {
emit_get_by_id(dst, result, intern_identifier("value"_fly_string)); emit_get_by_id(dst, result, intern_identifier("value"_fly_string));

View file

@ -327,6 +327,9 @@ public:
void emit_get_by_value(ScopedOperand dst, ScopedOperand base, ScopedOperand property, Optional<IdentifierTableIndex> base_identifier = {}); void emit_get_by_value(ScopedOperand dst, ScopedOperand base, ScopedOperand property, Optional<IdentifierTableIndex> base_identifier = {});
void emit_get_by_value_with_this(ScopedOperand dst, ScopedOperand base, ScopedOperand property, ScopedOperand this_value); void emit_get_by_value_with_this(ScopedOperand dst, ScopedOperand base, ScopedOperand property, ScopedOperand this_value);
void emit_put_by_value(ScopedOperand base, ScopedOperand property, ScopedOperand src, Bytecode::Op::PropertyKind, Optional<IdentifierTableIndex> base_identifier);
void emit_put_by_value_with_this(ScopedOperand base, ScopedOperand property, ScopedOperand this_value, ScopedOperand src, Bytecode::Op::PropertyKind);
void emit_iterator_value(ScopedOperand dst, ScopedOperand result); void emit_iterator_value(ScopedOperand dst, ScopedOperand result);
void emit_iterator_complete(ScopedOperand dst, ScopedOperand result); void emit_iterator_complete(ScopedOperand dst, ScopedOperand result);