LibJS: Skip iteration result allocation in AsyncFunctionDriverWrapper

- Create less GC pressure by making each `await` in async function skip
  iteration result object allocation.
- Skip uncached `Object::get()` calls to extract `value` and `done` from
  the iteration result object.

With this change, following function goes 30% faster on my computer:
```js
(async () => {
    const resolved = Promise.resolve();
    for (let i = 0; i < 5_000_000; i++) {
        await resolved;
    }
})();
```
This commit is contained in:
Aliaksandr Kalenik 2025-05-09 04:23:30 +03:00 committed by Andreas Kling
commit 4c789ac689
Notes: github-actions[bot] 2025-05-09 10:31:18 +00:00
7 changed files with 47 additions and 28 deletions

View file

@ -46,24 +46,27 @@ ThrowCompletionOr<Value> IteratorHelper::close_result(VM& vm, Completion complet
return TRY(iterator_close(vm, underlying_iterator(), move(completion)));
}
ThrowCompletionOr<Value> IteratorHelper::execute(VM& vm, JS::Completion const& completion)
ThrowCompletionOr<GeneratorObject::IterationResult> IteratorHelper::execute(VM& vm, JS::Completion const& completion)
{
ScopeGuard guard { [&] { vm.pop_execution_context(); } };
if (completion.is_abrupt()) {
if (m_abrupt_closure)
return m_abrupt_closure->function()(vm, *this, completion);
return close_result(vm, completion);
if (m_abrupt_closure) {
auto abrupt_result = TRY(m_abrupt_closure->function()(vm, *this, completion));
return IterationResult(abrupt_result, true);
}
auto close_result = TRY(this->close_result(vm, completion));
return IterationResult(close_result, true);
}
auto result_value = m_closure->function()(vm, *this);
if (result_value.is_throw_completion()) {
set_generator_state(GeneratorState::Completed);
return result_value;
return result_value.throw_completion();
}
return create_iterator_result_object(vm, result(result_value.release_value()), generator_state() == GeneratorState::Completed);
return IterationResult(result(result_value.release_value()), generator_state() == GeneratorState::Completed);
}
}