LibJS: Make async functions & generators faster with helper types

Instead of returning internal generator results as ordinary JS::Objects
with properties, we now use GeneratorResult and CompletionCell which
both inherit from Cell directly and allow efficient access to state.

1.59x speedup on JetStream3/lazy-collections.js :^)
This commit is contained in:
Andreas Kling 2025-03-31 09:32:39 +01:00 committed by Alexander Kalenik
commit a0bb31f7a0
Notes: github-actions[bot] 2025-04-01 00:31:35 +00:00
12 changed files with 259 additions and 93 deletions

View file

@ -1836,9 +1836,7 @@ static ScopedOperand generate_await(
ScopedOperand argument,
ScopedOperand received_completion,
ScopedOperand received_completion_type,
ScopedOperand received_completion_value,
Bytecode::IdentifierTableIndex type_identifier,
Bytecode::IdentifierTableIndex value_identifier);
ScopedOperand received_completion_value);
// https://tc39.es/ecma262/#sec-return-statement-runtime-semantics-evaluation
Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> ReturnStatement::generate_bytecode(Bytecode::Generator& generator, Optional<ScopedOperand>) const
@ -1863,10 +1861,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> ReturnStatement::genera
auto received_completion = generator.allocate_register();
auto received_completion_type = generator.allocate_register();
auto received_completion_value = generator.allocate_register();
auto type_identifier = generator.intern_identifier("type"_fly_string);
auto value_identifier = generator.intern_identifier("value"_fly_string);
return_value = generate_await(generator, *return_value, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
return_value = generate_await(generator, *return_value, received_completion, received_completion_type, received_completion_value);
}
// 4. Return Completion Record { [[Type]]: return, [[Value]]: exprValue, [[Target]]: empty }.
@ -1888,12 +1883,9 @@ static void get_received_completion_type_and_value(
Bytecode::Generator& generator,
ScopedOperand received_completion,
ScopedOperand received_completion_type,
ScopedOperand received_completion_value,
Bytecode::IdentifierTableIndex type_identifier,
Bytecode::IdentifierTableIndex value_identifier)
ScopedOperand received_completion_value)
{
generator.emit_get_by_id(received_completion_type, received_completion, type_identifier);
generator.emit_get_by_id(received_completion_value, received_completion, value_identifier);
generator.emit<Op::GetCompletionFields>(received_completion_type, received_completion_value, received_completion);
}
enum class AwaitBeforeYield {
@ -1907,8 +1899,6 @@ static void generate_yield(Bytecode::Generator& generator,
ScopedOperand received_completion,
ScopedOperand received_completion_type,
ScopedOperand received_completion_value,
Bytecode::IdentifierTableIndex type_identifier,
Bytecode::IdentifierTableIndex value_identifier,
AwaitBeforeYield await_before_yield)
{
if (!generator.is_in_async_generator_function()) {
@ -1917,14 +1907,14 @@ static void generate_yield(Bytecode::Generator& generator,
}
if (await_before_yield == AwaitBeforeYield::Yes)
argument = generate_await(generator, argument, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
argument = generate_await(generator, argument, received_completion, received_completion_type, received_completion_value);
auto& unwrap_yield_resumption_block = generator.make_block();
generator.emit<Bytecode::Op::Yield>(Bytecode::Label { unwrap_yield_resumption_block }, argument);
generator.switch_to_basic_block(unwrap_yield_resumption_block);
generator.emit_mov(received_completion, generator.accumulator());
get_received_completion_type_and_value(generator, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
get_received_completion_type_and_value(generator, received_completion, received_completion_type, received_completion_value);
// 27.6.3.7 AsyncGeneratorUnwrapYieldResumption ( resumptionValue ), https://tc39.es/ecma262/#sec-asyncgeneratorunwrapyieldresumption
// 1. If resumptionValue.[[Type]] is not return, return ? resumptionValue.
@ -1942,7 +1932,7 @@ static void generate_yield(Bytecode::Generator& generator,
generator.switch_to_basic_block(resumption_value_type_is_return_block);
// 2. Let awaited be Completion(Await(resumptionValue.[[Value]])).
generate_await(generator, received_completion_value, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
generate_await(generator, received_completion_value, received_completion, received_completion_type, received_completion_value);
// 3. If awaited.[[Type]] is throw, return ? awaited.
auto& awaited_type_is_normal_block = generator.make_block();
@ -1960,12 +1950,7 @@ static void generate_yield(Bytecode::Generator& generator,
generator.switch_to_basic_block(awaited_type_is_normal_block);
// 5. Return Completion Record { [[Type]]: return, [[Value]]: awaited.[[Value]], [[Target]]: empty }.
generator.emit<Bytecode::Op::PutById>(
received_completion,
type_identifier,
generator.add_constant(Value(to_underlying(Completion::Type::Return))),
Bytecode::Op::PropertyKind::KeyValue,
generator.next_property_lookup_cache());
generator.emit<Bytecode::Op::SetCompletionType>(received_completion, Completion::Type::Return);
generator.emit<Bytecode::Op::Jump>(continuation_label);
}
@ -1984,9 +1969,6 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
auto received_completion_type = generator.allocate_register();
auto received_completion_value = generator.allocate_register();
auto type_identifier = generator.intern_identifier("type"_fly_string);
auto value_identifier = generator.intern_identifier("value"_fly_string);
if (m_is_yield_from) {
// 15.5.5 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-generator-function-definitions-runtime-semantics-evaluation
// 1. Let generatorKind be GetGeneratorKind().
@ -2048,7 +2030,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
// ii. If generatorKind is async, set innerResult to ? Await(innerResult).
if (generator.is_in_async_generator_function()) {
auto new_inner_result = generate_await(generator, inner_result, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
auto new_inner_result = generate_await(generator, inner_result, received_completion, received_completion_type, received_completion_value);
generator.emit_mov(inner_result, new_inner_result);
}
@ -2096,8 +2078,6 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
received_completion,
received_completion_type,
received_completion_value,
type_identifier,
value_identifier,
AwaitBeforeYield::No);
}
@ -2139,7 +2119,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
// 2. If generatorKind is async, set innerResult to ? Await(innerResult).
if (generator.is_in_async_generator_function()) {
auto new_result = generate_await(generator, inner_result, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
auto new_result = generate_await(generator, inner_result, received_completion, received_completion_type, received_completion_value);
generator.emit_mov(inner_result, new_result);
}
@ -2174,7 +2154,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
// This only matters for non-async generators.
auto yield_value = generator.allocate_register();
generator.emit_iterator_value(yield_value, inner_result);
generate_yield(generator, Bytecode::Label { continuation_block }, yield_value, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier, AwaitBeforeYield::No);
generate_yield(generator, Bytecode::Label { continuation_block }, yield_value, received_completion, received_completion_type, received_completion_value, AwaitBeforeYield::No);
}
generator.switch_to_basic_block(throw_method_is_undefined_block);
@ -2219,7 +2199,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
// 1. If generatorKind is async, set received.[[Value]] to ? Await(received.[[Value]]).
if (generator.is_in_async_generator_function()) {
generate_await(generator, received_completion_value, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
generate_await(generator, received_completion_value, received_completion, received_completion_type, received_completion_value);
}
// 2. Return ? received.
@ -2236,7 +2216,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
// v. If generatorKind is async, set innerReturnResult to ? Await(innerReturnResult).
if (generator.is_in_async_generator_function()) {
auto new_value = generate_await(generator, inner_return_result, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
auto new_value = generate_await(generator, inner_return_result, received_completion, received_completion_type, received_completion_value);
generator.emit_mov(inner_return_result, new_value);
}
@ -2272,7 +2252,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
auto received = generator.allocate_register();
generator.emit_iterator_value(received, inner_return_result);
generate_yield(generator, Bytecode::Label { continuation_block }, received, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier, AwaitBeforeYield::No);
generate_yield(generator, Bytecode::Label { continuation_block }, received, received_completion, received_completion_type, received_completion_value, AwaitBeforeYield::No);
generator.switch_to_basic_block(continuation_block);
@ -2280,7 +2260,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
generator.emit_mov(Bytecode::Operand(Bytecode::Register::exception()), Bytecode::Operand(*saved_exception));
generator.emit_mov(received_completion, generator.accumulator());
get_received_completion_type_and_value(generator, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
get_received_completion_type_and_value(generator, received_completion, received_completion_type, received_completion_value);
generator.emit<Bytecode::Op::Jump>(Bytecode::Label { loop_block });
generator.switch_to_basic_block(loop_end_block);
@ -2300,7 +2280,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
generator.emit_mov(Bytecode::Operand(*saved_exception), Bytecode::Operand(Bytecode::Register::exception()));
}
generate_yield(generator, Bytecode::Label { continuation_block }, *argument, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier, AwaitBeforeYield::Yes);
generate_yield(generator, Bytecode::Label { continuation_block }, *argument, received_completion, received_completion_type, received_completion_value, AwaitBeforeYield::Yes);
generator.switch_to_basic_block(continuation_block);
if (is_in_finalizer)
@ -2308,7 +2288,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> YieldExpression::genera
generator.emit_mov(received_completion, generator.accumulator());
get_received_completion_type_and_value(generator, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
get_received_completion_type_and_value(generator, received_completion, received_completion_type, received_completion_value);
auto& normal_completion_continuation_block = generator.make_block();
auto& throw_completion_continuation_block = generator.make_block();
@ -2957,9 +2937,7 @@ static ScopedOperand generate_await(
ScopedOperand argument,
ScopedOperand received_completion,
ScopedOperand received_completion_type,
ScopedOperand received_completion_value,
Bytecode::IdentifierTableIndex type_identifier,
Bytecode::IdentifierTableIndex value_identifier)
ScopedOperand received_completion_value)
{
VERIFY(generator.is_in_async_function());
@ -2971,7 +2949,7 @@ static ScopedOperand generate_await(
// It ends up there because we "return" from the Await instruction above via the synthetic
// generator function that actually drives async execution.
generator.emit_mov(received_completion, generator.accumulator());
get_received_completion_type_and_value(generator, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
get_received_completion_type_and_value(generator, received_completion, received_completion_type, received_completion_value);
auto& normal_completion_continuation_block = generator.make_block();
auto& throw_value_block = generator.make_block();
@ -3007,10 +2985,7 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> AwaitExpression::genera
generator.emit_mov(received_completion, generator.accumulator());
auto type_identifier = generator.intern_identifier("type"_fly_string);
auto value_identifier = generator.intern_identifier("value"_fly_string);
return generate_await(generator, argument, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
return generate_await(generator, argument, received_completion, received_completion_type, received_completion_value);
}
Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> WithStatement::generate_bytecode(Bytecode::Generator& generator, [[maybe_unused]] Optional<ScopedOperand> preferred_dst) const
@ -3204,11 +3179,8 @@ static Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> for_in_of_body_e
auto received_completion_type = generator.allocate_register();
auto received_completion_value = generator.allocate_register();
auto type_identifier = generator.intern_identifier("type"_fly_string);
auto value_identifier = generator.intern_identifier("value"_fly_string);
generator.emit_mov(received_completion, generator.accumulator());
auto new_result = generate_await(generator, next_result, received_completion, received_completion_type, received_completion_value, type_identifier, value_identifier);
auto new_result = generate_await(generator, next_result, received_completion, received_completion_type, received_completion_value);
generator.emit_mov(next_result, new_result);
}

View file

@ -57,6 +57,7 @@
O(GetByValue) \
O(GetByValueWithThis) \
O(GetCalleeAndThisFromEnvironment) \
O(GetCompletionFields) \
O(GetGlobal) \
O(GetImportMeta) \
O(GetIterator) \
@ -131,6 +132,7 @@
O(RightShift) \
O(ScheduleJump) \
O(SetArgument) \
O(SetCompletionType) \
O(SetLexicalBinding) \
O(SetVariableBinding) \
O(StrictlyEquals) \

View file

@ -18,10 +18,12 @@
#include <LibJS/Runtime/Accessor.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/CompletionCell.h>
#include <LibJS/Runtime/DeclarativeEnvironment.h>
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>
#include <LibJS/Runtime/Environment.h>
#include <LibJS/Runtime/FunctionEnvironment.h>
#include <LibJS/Runtime/GeneratorResult.h>
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Iterator.h>
@ -192,18 +194,10 @@ ALWAYS_INLINE void Interpreter::set(Operand op, Value value)
ALWAYS_INLINE Value Interpreter::do_yield(Value value, Optional<Label> continuation)
{
auto object = Object::create(realm(), nullptr);
object->define_direct_property(m_vm.names.result, value, JS::default_attributes);
if (continuation.has_value())
// FIXME: If we get a pointer, which is not accurately representable as a double
// will cause this to explode
object->define_direct_property(m_vm.names.continuation, Value(continuation->address()), JS::default_attributes);
else
object->define_direct_property(m_vm.names.continuation, js_null(), JS::default_attributes);
object->define_direct_property(m_vm.names.isAwait, Value(false), JS::default_attributes);
return object;
// FIXME: If we get a pointer, which is not accurately representable as a double
// will cause this to explode
auto continuation_value = continuation.has_value() ? Value(continuation->address()) : js_null();
return vm().heap().allocate<GeneratorResult>(value, continuation_value, false).ptr();
}
// 16.1.6 ScriptEvaluation ( scriptRecord ), https://tc39.es/ecma262/#sec-runtime-semantics-scriptevaluation
@ -624,6 +618,7 @@ FLATTEN_ON_CLANG void Interpreter::run_bytecode(size_t entry_point)
HANDLE_INSTRUCTION(GetByValue);
HANDLE_INSTRUCTION(GetByValueWithThis);
HANDLE_INSTRUCTION(GetCalleeAndThisFromEnvironment);
HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(GetCompletionFields);
HANDLE_INSTRUCTION(GetGlobal);
HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(GetImportMeta);
HANDLE_INSTRUCTION(GetIterator);
@ -680,6 +675,7 @@ FLATTEN_ON_CLANG void Interpreter::run_bytecode(size_t entry_point)
HANDLE_INSTRUCTION(ResolveThisBinding);
HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(RestoreScheduledJump);
HANDLE_INSTRUCTION(RightShift);
HANDLE_INSTRUCTION_WITHOUT_EXCEPTION_CHECK(SetCompletionType);
HANDLE_INSTRUCTION(SetLexicalBinding);
HANDLE_INSTRUCTION(SetVariableBinding);
HANDLE_INSTRUCTION(StrictlyEquals);
@ -2840,15 +2836,12 @@ void PrepareYield::execute_impl(Bytecode::Interpreter& interpreter) const
void Await::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto& vm = interpreter.vm();
auto yielded_value = interpreter.get(m_argument).value_or(js_undefined());
auto object = Object::create(interpreter.realm(), nullptr);
object->define_direct_property(vm.names.result, yielded_value, JS::default_attributes);
// FIXME: If we get a pointer, which is not accurately representable as a double
// will cause this to explode
object->define_direct_property(vm.names.continuation, Value(m_continuation_label.address()), JS::default_attributes);
object->define_direct_property(vm.names.isAwait, Value(true), JS::default_attributes);
interpreter.do_return(object);
auto continuation_value = Value(m_continuation_label.address());
auto result = interpreter.vm().heap().allocate<GeneratorResult>(yielded_value, continuation_value, true);
interpreter.do_return(result);
}
ThrowCompletionOr<void> GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
@ -3836,4 +3829,33 @@ ByteString Dump::to_byte_string_impl(Bytecode::Executable const& executable) con
format_operand("value"sv, m_value, executable));
}
void GetCompletionFields::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto const& completion_cell = static_cast<CompletionCell const&>(interpreter.get(m_completion).as_cell());
interpreter.set(m_value_dst, completion_cell.completion().value().value_or(js_undefined()));
interpreter.set(m_type_dst, Value(to_underlying(completion_cell.completion().type())));
}
ByteString GetCompletionFields::to_byte_string_impl(Bytecode::Executable const& executable) const
{
return ByteString::formatted("GetCompletionFields {}, {}, {}",
format_operand("value_dst"sv, m_value_dst, executable),
format_operand("type_dst"sv, m_type_dst, executable),
format_operand("completion"sv, m_completion, executable));
}
void SetCompletionType::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto& completion_cell = static_cast<CompletionCell&>(interpreter.get(m_completion).as_cell());
auto completion = completion_cell.completion();
completion_cell.set_completion(Completion { completion.type(), completion.value() });
}
ByteString SetCompletionType::to_byte_string_impl(Bytecode::Executable const& executable) const
{
return ByteString::formatted("SetCompletionType {}, type={}",
format_operand("completion"sv, m_completion, executable),
to_underlying(m_type));
}
}

View file

@ -962,6 +962,58 @@ private:
u32 m_cache_index { 0 };
};
class GetCompletionFields final : public Instruction {
public:
GetCompletionFields(Operand type_dst, Operand value_dst, Operand completion)
: Instruction(Type::GetCompletionFields)
, m_type_dst(type_dst)
, m_value_dst(value_dst)
, m_completion(completion)
{
}
void execute_impl(Bytecode::Interpreter&) const;
ByteString to_byte_string_impl(Bytecode::Executable const&) const;
void visit_operands_impl(Function<void(Operand&)> visitor)
{
visitor(m_type_dst);
visitor(m_value_dst);
visitor(m_completion);
}
Operand type_dst() const { return m_type_dst; }
Operand value_dst() const { return m_value_dst; }
Operand completion() const { return m_completion; }
private:
Operand m_type_dst;
Operand m_value_dst;
Operand m_completion;
};
class SetCompletionType final : public Instruction {
public:
SetCompletionType(Operand completion, Completion::Type type)
: Instruction(Type::SetCompletionType)
, m_completion(completion)
, m_type(type)
{
}
void execute_impl(Bytecode::Interpreter&) const;
ByteString to_byte_string_impl(Bytecode::Executable const&) const;
void visit_operands_impl(Function<void(Operand&)> visitor)
{
visitor(m_completion);
}
Operand completion() const { return m_completion; }
private:
Operand m_completion;
Completion::Type m_type;
};
class GetByIdWithThis final : public Instruction {
public:
GetByIdWithThis(Operand dst, Operand base, IdentifierTableIndex property, Operand this_value, u32 cache_index)