mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-16 05:51:55 +00:00
LibJS: Add SetGlobal bytecode instruction for cached global writes
Before this change, setting a global would end up as SetLexicalBinding. That instruction always failed to cache the access if the global was a property of the global object. 1.14x speedup on Octane/earley-boyer.js 2.04x speedup on MicroBench/for-of.js Note that MicroBench/for-of.js was more of a "set global" benchmark before this. After this change, it's actually a for..of benchmark. :^)
This commit is contained in:
parent
de6df6f403
commit
ad7c1e147f
Notes:
github-actions[bot]
2025-05-03 23:59:49 +00:00
Author: https://github.com/awesomekling
Commit: ad7c1e147f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4586
4 changed files with 134 additions and 1 deletions
|
@ -892,7 +892,11 @@ void Generator::emit_set_variable(JS::Identifier const& identifier, ScopedOperan
|
||||||
if (initialization_mode == Bytecode::Op::BindingInitializationMode::Initialize) {
|
if (initialization_mode == Bytecode::Op::BindingInitializationMode::Initialize) {
|
||||||
emit<Bytecode::Op::InitializeLexicalBinding>(identifier_index, value);
|
emit<Bytecode::Op::InitializeLexicalBinding>(identifier_index, value);
|
||||||
} else if (initialization_mode == Bytecode::Op::BindingInitializationMode::Set) {
|
} else if (initialization_mode == Bytecode::Op::BindingInitializationMode::Set) {
|
||||||
emit<Bytecode::Op::SetLexicalBinding>(identifier_index, value);
|
if (identifier.is_global()) {
|
||||||
|
emit<Bytecode::Op::SetGlobal>(identifier_index, value, next_global_variable_cache());
|
||||||
|
} else {
|
||||||
|
emit<Bytecode::Op::SetLexicalBinding>(identifier_index, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (environment_mode == Bytecode::Op::EnvironmentMode::Var) {
|
} else if (environment_mode == Bytecode::Op::EnvironmentMode::Var) {
|
||||||
if (initialization_mode == Bytecode::Op::BindingInitializationMode::Initialize) {
|
if (initialization_mode == Bytecode::Op::BindingInitializationMode::Initialize) {
|
||||||
|
|
|
@ -132,6 +132,7 @@
|
||||||
O(RightShift) \
|
O(RightShift) \
|
||||||
O(ScheduleJump) \
|
O(ScheduleJump) \
|
||||||
O(SetCompletionType) \
|
O(SetCompletionType) \
|
||||||
|
O(SetGlobal) \
|
||||||
O(SetLexicalBinding) \
|
O(SetLexicalBinding) \
|
||||||
O(SetVariableBinding) \
|
O(SetVariableBinding) \
|
||||||
O(StrictlyEquals) \
|
O(StrictlyEquals) \
|
||||||
|
|
|
@ -677,6 +677,7 @@ FLATTEN_ON_CLANG void Interpreter::run_bytecode(size_t entry_point)
|
||||||
HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(RestoreScheduledJump);
|
HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(RestoreScheduledJump);
|
||||||
HANDLE_INSTRUCTION(RightShift);
|
HANDLE_INSTRUCTION(RightShift);
|
||||||
HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(SetCompletionType);
|
HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(SetCompletionType);
|
||||||
|
HANDLE_INSTRUCTION(SetGlobal);
|
||||||
HANDLE_INSTRUCTION(SetLexicalBinding);
|
HANDLE_INSTRUCTION(SetLexicalBinding);
|
||||||
HANDLE_INSTRUCTION(SetVariableBinding);
|
HANDLE_INSTRUCTION(SetVariableBinding);
|
||||||
HANDLE_INSTRUCTION(StrictlyEquals);
|
HANDLE_INSTRUCTION(StrictlyEquals);
|
||||||
|
@ -2278,6 +2279,98 @@ ThrowCompletionOr<void> GetGlobal::execute_impl(Bytecode::Interpreter& interpret
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ThrowCompletionOr<void> SetGlobal::execute_impl(Bytecode::Interpreter& interpreter) const
|
||||||
|
{
|
||||||
|
auto& vm = interpreter.vm();
|
||||||
|
auto& binding_object = interpreter.global_object();
|
||||||
|
auto& declarative_record = interpreter.global_declarative_environment();
|
||||||
|
|
||||||
|
auto& cache = interpreter.current_executable().global_variable_caches[m_cache_index];
|
||||||
|
auto& shape = binding_object.shape();
|
||||||
|
auto src = interpreter.get(m_src);
|
||||||
|
|
||||||
|
if (cache.environment_serial_number == declarative_record.environment_serial_number()) {
|
||||||
|
// OPTIMIZATION: For global var bindings, if the shape of the global object hasn't changed,
|
||||||
|
// we can use the cached property offset.
|
||||||
|
if (&shape == cache.shape) {
|
||||||
|
auto value = binding_object.get_direct(cache.property_offset.value());
|
||||||
|
if (value.is_accessor())
|
||||||
|
TRY(call(vm, value.as_accessor().setter(), js_undefined(), value));
|
||||||
|
else
|
||||||
|
binding_object.put_direct(cache.property_offset.value(), src);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTIMIZATION: For global lexical bindings, if the global declarative environment hasn't changed,
|
||||||
|
// we can use the cached environment binding index.
|
||||||
|
if (cache.has_environment_binding_index) {
|
||||||
|
if (cache.in_module_environment) {
|
||||||
|
auto module = vm.running_execution_context().script_or_module.get_pointer<GC::Ref<Module>>();
|
||||||
|
TRY((*module)->environment()->set_mutable_binding_direct(vm, cache.environment_binding_index, src, vm.in_strict_mode()));
|
||||||
|
} else {
|
||||||
|
TRY(declarative_record.set_mutable_binding_direct(vm, cache.environment_binding_index, src, vm.in_strict_mode()));
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.environment_serial_number = declarative_record.environment_serial_number();
|
||||||
|
|
||||||
|
auto& identifier = interpreter.current_executable().get_identifier(m_identifier);
|
||||||
|
|
||||||
|
if (auto* module = vm.running_execution_context().script_or_module.get_pointer<GC::Ref<Module>>()) {
|
||||||
|
// NOTE: GetGlobal is used to access variables stored in the module environment and global environment.
|
||||||
|
// The module environment is checked first since it precedes the global environment in the environment chain.
|
||||||
|
auto& module_environment = *(*module)->environment();
|
||||||
|
Optional<size_t> index;
|
||||||
|
if (TRY(module_environment.has_binding(identifier, &index))) {
|
||||||
|
if (index.has_value()) {
|
||||||
|
cache.environment_binding_index = static_cast<u32>(index.value());
|
||||||
|
cache.has_environment_binding_index = true;
|
||||||
|
cache.in_module_environment = true;
|
||||||
|
return TRY(module_environment.set_mutable_binding_direct(vm, index.value(), src, vm.in_strict_mode()));
|
||||||
|
}
|
||||||
|
return TRY(module_environment.set_mutable_binding(vm, identifier, src, vm.in_strict_mode()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<size_t> offset;
|
||||||
|
if (TRY(declarative_record.has_binding(identifier, &offset))) {
|
||||||
|
cache.environment_binding_index = static_cast<u32>(offset.value());
|
||||||
|
cache.has_environment_binding_index = true;
|
||||||
|
cache.in_module_environment = false;
|
||||||
|
TRY(declarative_record.set_mutable_binding(vm, identifier, src, vm.in_strict_mode()));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TRY(binding_object.has_property(identifier))) {
|
||||||
|
CacheablePropertyMetadata cacheable_metadata;
|
||||||
|
auto success = TRY(binding_object.internal_set(identifier, src, &binding_object, &cacheable_metadata));
|
||||||
|
if (!success && vm.in_strict_mode()) {
|
||||||
|
// Note: Nothing like this in the spec, this is here to produce nicer errors instead of the generic one thrown by Object::set().
|
||||||
|
|
||||||
|
auto property_or_error = binding_object.internal_get_own_property(identifier);
|
||||||
|
if (!property_or_error.is_error()) {
|
||||||
|
auto property = property_or_error.release_value();
|
||||||
|
if (property.has_value() && !property->writable.value_or(true)) {
|
||||||
|
return vm.throw_completion<TypeError>(ErrorType::DescWriteNonWritable, identifier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return vm.throw_completion<TypeError>(ErrorType::ObjectSetReturnedFalse);
|
||||||
|
}
|
||||||
|
if (cacheable_metadata.type == CacheablePropertyMetadata::Type::OwnProperty) {
|
||||||
|
cache.shape = shape;
|
||||||
|
cache.property_offset = cacheable_metadata.property_offset.value();
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto reference = TRY(vm.resolve_binding(identifier, &declarative_record));
|
||||||
|
TRY(reference.put_value(vm, src));
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const
|
ThrowCompletionOr<void> DeleteVariable::execute_impl(Bytecode::Interpreter& interpreter) const
|
||||||
{
|
{
|
||||||
auto& vm = interpreter.vm();
|
auto& vm = interpreter.vm();
|
||||||
|
@ -3198,6 +3291,13 @@ ByteString GetGlobal::to_byte_string_impl(Bytecode::Executable const& executable
|
||||||
executable.identifier_table->get(m_identifier));
|
executable.identifier_table->get(m_identifier));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ByteString SetGlobal::to_byte_string_impl(Bytecode::Executable const& executable) const
|
||||||
|
{
|
||||||
|
return ByteString::formatted("SetGlobal {}, {}",
|
||||||
|
executable.identifier_table->get(m_identifier),
|
||||||
|
format_operand("src"sv, src(), executable));
|
||||||
|
}
|
||||||
|
|
||||||
ByteString DeleteVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
|
ByteString DeleteVariable::to_byte_string_impl(Bytecode::Executable const& executable) const
|
||||||
{
|
{
|
||||||
return ByteString::formatted("DeleteVariable {}", executable.identifier_table->get(m_identifier));
|
return ByteString::formatted("DeleteVariable {}", executable.identifier_table->get(m_identifier));
|
||||||
|
|
|
@ -861,6 +861,34 @@ private:
|
||||||
u32 m_cache_index { 0 };
|
u32 m_cache_index { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SetGlobal final : public Instruction {
|
||||||
|
public:
|
||||||
|
SetGlobal(IdentifierTableIndex identifier, Operand src, u32 cache_index)
|
||||||
|
: Instruction(Type::SetGlobal)
|
||||||
|
, m_src(src)
|
||||||
|
, m_identifier(identifier)
|
||||||
|
, m_cache_index(cache_index)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
|
||||||
|
ByteString to_byte_string_impl(Bytecode::Executable const&) const;
|
||||||
|
|
||||||
|
Operand src() const { return m_src; }
|
||||||
|
IdentifierTableIndex identifier() const { return m_identifier; }
|
||||||
|
u32 cache_index() const { return m_cache_index; }
|
||||||
|
|
||||||
|
void visit_operands_impl(Function<void(Operand&)> visitor)
|
||||||
|
{
|
||||||
|
visitor(m_src);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Operand m_src;
|
||||||
|
IdentifierTableIndex m_identifier;
|
||||||
|
u32 m_cache_index { 0 };
|
||||||
|
};
|
||||||
|
|
||||||
class DeleteVariable final : public Instruction {
|
class DeleteVariable final : public Instruction {
|
||||||
public:
|
public:
|
||||||
explicit DeleteVariable(Operand dst, IdentifierTableIndex identifier)
|
explicit DeleteVariable(Operand dst, IdentifierTableIndex identifier)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue