LibJS: Optimize reading known-to-be-initialized var bindings
Some checks are pending
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

`var` bindings are never in the temporal dead zone (TDZ), and so we
know accessing them will not throw.

We now take advantage of this by having a specialized environment
binding value getter that doesn't check for exceptional cases.

1.08x speedup on JetStream.
This commit is contained in:
Andreas Kling 2025-05-04 01:41:49 +02:00 committed by Andreas Kling
commit bf1b754e91
Notes: github-actions[bot] 2025-05-04 00:32:11 +00:00
8 changed files with 90 additions and 15 deletions

View file

@ -1158,6 +1158,8 @@ void VariableDeclaration::dump(int indent) const
{
char const* declaration_kind_string = nullptr;
switch (m_declaration_kind) {
case DeclarationKind::None:
VERIFY_NOT_REACHED();
case DeclarationKind::Let:
declaration_kind_string = "Let";
break;

View file

@ -159,6 +159,13 @@ private:
u32 m_tail_size { 0 };
};
enum class DeclarationKind {
None,
Var,
Let,
Const,
};
class Statement : public ASTNode {
public:
explicit Statement(SourceRange source_range)
@ -702,6 +709,9 @@ public:
bool is_global() const { return m_is_global; }
void set_is_global() { m_is_global = true; }
[[nodiscard]] DeclarationKind declaration_kind() const { return m_declaration_kind; }
void set_declaration_kind(DeclarationKind kind) { m_declaration_kind = kind; }
virtual void dump(int indent) const override;
virtual Bytecode::CodeGenerationErrorOr<Optional<Bytecode::ScopedOperand>> generate_bytecode(Bytecode::Generator&, Optional<Bytecode::ScopedOperand> preferred_dst = {}) const override;
@ -712,6 +722,7 @@ private:
Optional<Local> m_local_index;
bool m_is_global { false };
DeclarationKind m_declaration_kind { DeclarationKind::None };
};
struct FunctionParameter {
@ -1764,12 +1775,6 @@ private:
bool m_prefixed;
};
enum class DeclarationKind {
Var,
Let,
Const,
};
class VariableDeclarator final : public ASTNode {
public:
VariableDeclarator(SourceRange source_range, NonnullRefPtr<Identifier const> id)

View file

@ -481,9 +481,13 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> Identifier::generate_by
auto dst = choose_dst(generator, preferred_dst);
if (is_global()) {
generator.emit<Bytecode::Op::GetGlobal>(dst, generator.intern_identifier(m_string), generator.next_global_variable_cache());
} else {
if (declaration_kind() == DeclarationKind::Var) {
generator.emit<Bytecode::Op::GetInitializedBinding>(dst, generator.intern_identifier(m_string));
} else {
generator.emit<Bytecode::Op::GetBinding>(dst, generator.intern_identifier(m_string));
}
}
return dst;
}

View file

@ -69,6 +69,7 @@
O(GetObjectPropertyIterator) \
O(GetPrivateById) \
O(GetBinding) \
O(GetInitializedBinding) \
O(GreaterThan) \
O(GreaterThanEquals) \
O(HasPrivateId) \

View file

@ -631,6 +631,7 @@ FLATTEN_ON_CLANG void Interpreter::run_bytecode(size_t entry_point)
HANDLE_INSTRUCTION(GetObjectPropertyIterator);
HANDLE_INSTRUCTION(GetPrivateById);
HANDLE_INSTRUCTION(GetBinding);
HANDLE_INSTRUCTION(GetInitializedBinding);
HANDLE_INSTRUCTION(GreaterThan);
HANDLE_INSTRUCTION(GreaterThanEquals);
HANDLE_INSTRUCTION(HasPrivateId);
@ -2239,29 +2240,51 @@ ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interp
return {};
}
ThrowCompletionOr<void> GetBinding::execute_impl(Bytecode::Interpreter& interpreter) const
enum class BindingIsKnownToBeInitialized {
No,
Yes,
};
template<BindingIsKnownToBeInitialized binding_is_known_to_be_initialized>
static ThrowCompletionOr<void> get_binding(Interpreter& interpreter, Operand dst, IdentifierTableIndex identifier, EnvironmentCoordinate& cache)
{
auto& vm = interpreter.vm();
auto& executable = interpreter.current_executable();
if (m_cache.is_valid()) {
if (cache.is_valid()) {
auto const* environment = interpreter.running_execution_context().lexical_environment.ptr();
for (size_t i = 0; i < m_cache.hops; ++i)
for (size_t i = 0; i < cache.hops; ++i)
environment = environment->outer_environment();
if (!environment->is_permanently_screwed_by_eval()) {
interpreter.set(dst(), TRY(static_cast<DeclarativeEnvironment const&>(*environment).get_binding_value_direct(vm, m_cache.index)));
Value value;
if constexpr (binding_is_known_to_be_initialized == BindingIsKnownToBeInitialized::No) {
value = TRY(static_cast<DeclarativeEnvironment const&>(*environment).get_binding_value_direct(vm, cache.index));
} else {
value = static_cast<DeclarativeEnvironment const&>(*environment).get_initialized_binding_value_direct(cache.index);
}
interpreter.set(dst, value);
return {};
}
m_cache = {};
cache = {};
}
auto reference = TRY(vm.resolve_binding(executable.get_identifier(m_identifier)));
auto reference = TRY(vm.resolve_binding(executable.get_identifier(identifier)));
if (reference.environment_coordinate().has_value())
m_cache = reference.environment_coordinate().value();
interpreter.set(dst(), TRY(reference.get_value(vm)));
cache = reference.environment_coordinate().value();
interpreter.set(dst, TRY(reference.get_value(vm)));
return {};
}
ThrowCompletionOr<void> GetBinding::execute_impl(Bytecode::Interpreter& interpreter) const
{
return get_binding<BindingIsKnownToBeInitialized::No>(interpreter, m_dst, m_identifier, m_cache);
}
ThrowCompletionOr<void> GetInitializedBinding::execute_impl(Bytecode::Interpreter& interpreter) const
{
return get_binding<BindingIsKnownToBeInitialized::Yes>(interpreter, m_dst, m_identifier, m_cache);
}
ThrowCompletionOr<void> GetCalleeAndThisFromEnvironment::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto callee_and_this = TRY(get_callee_and_this_from_environment(
@ -3285,6 +3308,13 @@ ByteString GetBinding::to_byte_string_impl(Bytecode::Executable const& executabl
executable.identifier_table->get(m_identifier));
}
ByteString GetInitializedBinding::to_byte_string_impl(Bytecode::Executable const& executable) const
{
return ByteString::formatted("GetInitializedBinding {}, {}",
format_operand("dst"sv, dst(), executable),
executable.identifier_table->get(m_identifier));
}
ByteString GetGlobal::to_byte_string_impl(Bytecode::Executable const& executable) const
{
return ByteString::formatted("GetGlobal {}, {}", format_operand("dst"sv, dst(), executable),

View file

@ -833,6 +833,32 @@ private:
mutable EnvironmentCoordinate m_cache;
};
class GetInitializedBinding final : public Instruction {
public:
explicit GetInitializedBinding(Operand dst, IdentifierTableIndex identifier)
: Instruction(Type::GetInitializedBinding)
, m_dst(dst)
, m_identifier(identifier)
{
}
ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
ByteString to_byte_string_impl(Bytecode::Executable const&) const;
Operand dst() const { return m_dst; }
IdentifierTableIndex identifier() const { return m_identifier; }
void visit_operands_impl(Function<void(Operand&)> visitor)
{
visitor(m_dst);
}
private:
Operand m_dst;
IdentifierTableIndex m_identifier;
mutable EnvironmentCoordinate m_cache;
};
class GetGlobal final : public Instruction {
public:
GetGlobal(Operand dst, IdentifierTableIndex identifier, u32 cache_index)

View file

@ -292,6 +292,12 @@ public:
auto const& identifier_group_name = it.key;
auto& identifier_group = it.value;
if (identifier_group.declaration_kind.has_value()) {
for (auto& identifier : identifier_group.identifiers) {
identifier->set_declaration_kind(identifier_group.declaration_kind.value());
}
}
bool scope_has_declaration = false;
if (is_top_level() && m_var_names.contains(identifier_group_name))
scope_has_declaration = true;

View file

@ -59,6 +59,7 @@ public:
ThrowCompletionOr<void> initialize_binding_direct(VM&, size_t index, Value, InitializeBindingHint);
ThrowCompletionOr<void> set_mutable_binding_direct(VM&, size_t index, Value, bool strict);
ThrowCompletionOr<Value> get_binding_value_direct(VM&, size_t index) const;
Value get_initialized_binding_value_direct(size_t index) const { return m_bindings[index].value; }
void shrink_to_fit();