Skip to content

perf(core): drop the per-op .catch wrapper promise#36155

Open
bartlomieju wants to merge 1 commit into
denoland:mainfrom
bartlomieju:perf/drop-op-catch-wrapper-promise
Open

perf(core): drop the per-op .catch wrapper promise#36155
bartlomieju wants to merge 1 commit into
denoland:mainfrom
bartlomieju:perf/drop-op-catch-wrapper-promise

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

Every pending async op currently allocates two promises: the op promise created in setPromise, plus a second, derived promise from PromisePrototypeCatch(promise, __opRejectHandler) whose only purpose is to recapture the error stack (stripping internal event-loop frames) on the rare rejection path. This doubles the promise objects the GC sweeps per in-flight op and registers promise-reaction machinery on every op.

This PR returns the raw op promise (still carrying promiseIdSymbol, so refOp/unrefOp keep working) and drops the recapture.

Impact

  • ~6.5% faster across all tick-path ops on the async op microbench (libs/core/benches/ops/async.rs), measured back-to-back:

    bench (1000 ops/iter) before after Δ
    op_async_void_deferred 429,487 401,318 −6.6%
    op_async_void_deferred_nofast 433,513 403,950 −6.8%
    op_async_void_lazy 425,649 394,058 −7.4%
    op_async_yield_deferred 423,214 396,355 −6.3%
    op_async_yield_lazy_nofast 535,074 498,536 −6.8%
  • One fewer promise + reaction allocated per in-flight async op → less GC pressure on op-heavy servers.

Stack traces

The recapture only ever affected a bare op stub awaited directly (no JS wrapper). Every real Deno async API (Deno.readFile, fetch, …) is an async function that awaits the op, so V8's async stack traces already include the call site without the recapture. Verified caught, uncaught, and dynamic-import error stacks via deno run are byte-identical to before.

The one raw-runtime test that exercised the bare-op pattern (test_dynamic_import_module_error_stack) is updated: it now asserts on the await import(...) site in main.js rather than the innermost bare-op frame. The user-facing #20034 guarantee stays covered by the CLI specs error_023_stack_async and fetch_async_error_stack (both pass).

Tests

  • cargo test -p deno_core (441/441 relevant, incl. updated dynamic-import stack test)
  • spec: error_02* stack tests, fetch_async_error_stack, promise/rejection tests — all pass

Every pending async op allocated two promises: the op promise plus a
derived promise from PromisePrototypeCatch(promise, __opRejectHandler)
whose only job was to recapture the error stack (stripping internal event
loop frames) on the rare rejection path. This doubled the promise objects
the GC sweeps per in-flight op and registered promise-reaction machinery
on every op.

Return the raw op promise (still carrying promiseIdSymbol) and drop the
recapture. On the async op microbench this is ~6.5% faster across all
tick-path ops (e.g. op_async_void_deferred 429.5k -> 401.3k ns/iter for
1000 ops), plus one fewer promise + reaction per in-flight op.

The recapture only ever affected a *bare* op stub awaited directly (no JS
wrapper): every real Deno async API (Deno.readFile, fetch, ...) is an
async function that awaits the op, so V8's async stack traces already
include the call site without the recapture. Verified caught, uncaught,
and dynamic-import error stacks via the CLI are unchanged. Updates the
one raw-runtime test that exercised the bare-op pattern.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant