Skip to content

perf(worker): defer workflow promise stack formatting - #2542

Draft
eamsden wants to merge 1 commit into
mainfrom
eamsden/defer-promise-stack-formatting
Draft

eamsden wants to merge 1 commit into
mainfrom
eamsden/defer-promise-stack-formatting

Conversation

@eamsden

@eamsden eamsden commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

Capture raw V8 call sites when a Workflow Promise is created and defer source-map lookup and stack formatting unless and until the stack is actually queried. Cache the formatted and structured results together, and keep Promise aggregation relationships tracked from raw call sites. A WeakSet marks individual capture errors so VM timeouts cannot leave a global formatting mode active and corrupt later error stacks.

Why?

Most replay promises settle without a stack query. Deferring source-map lookup and formatting removes repeated work from activation processing while retaining diagnostic detail.

Benchmark results

These are prior measurements of the standalone deferred-formatting variant against main at f2b6b0fe, using main's pinned Core revision 2deeab98d64a6bc1fac4eab5a942e7889acf8077 in a release build (opt-level 3, LTO), with Core chunking retained. The measured variant includes deferred formatting alone. This PR is based on a newer main revision.

Single-history replay: Worker.runReplayHistory

Workload Main (ms) Deferred formatting (ms) Replay time saved 95% interval for time saved
Empty 16.11 14.73 8.6% 1.4–12.0%
Sparse signals 25.28 20.89 17.4% −12.1–21.3%
Dense signals 508.71 426.07 16.2% 14.7–16.4%
Scheduled signals 192.76 99.81 48.2% 45.0–49.5%
Local-activity heartbeat signals 191.73 100.84 47.4% 45.1–49.6%

Replay worker reused across ten histories: Worker.runReplayHistories

Times below are the whole API call divided by ten; percentage savings are calculated from the whole call.

Workload Main per history (ms) Deferred formatting per history (ms) Replay time saved 95% interval for time saved
Empty 8.01 6.94 13.3% 11.4–19.6%
Sparse signals 12.80 11.23 12.3% 3.3–15.2%
Dense signals 489.01 418.01 14.5% 14.1–16.4%
Scheduled signals 163.51 94.86 42.0% 41.2–42.2%
Local-activity heartbeat signals 170.95 95.48 44.1% 41.6–44.5%

(See below for methodology)

Checklist

  1. How was this tested:

    • Full workspace TypeScript build; repository ESLint (8 existing warnings, no errors) and Prettier checks.
    • 104 tests across promise stacks, Workflow execution, reusable VM disposal, and replay, including regression coverage for deferred lookup, cached diagnostics, native and user-defined aggregators, ordinary errors, and interrupted captures.
    • 93 Workflow/replay tests with REUSE_V8_CONTEXT=false; both diagnostic query formats are covered in both VM modes.
    • 50 public Worker.runReplayHistories replays (ten histories per synthetic workload) against a release build of main's pinned Core, asserting the expected chunked activation counts, signal counts, eviction counts, and successful replay results.
  2. Any docs updates needed?

    Added an entry under Unreleased → Changed in CHANGELOG.md; the public API and diagnostic output are preserved.

Benchmark Methodology

Medians of nine runs after two warmups, rotating variant order, sequential calls, and a separate persistent Node process per variant. Intervals use a paired bootstrap with 5,000 resamples. Timing covers the whole public replay API, including worker/Core setup, history encoding/decoding, VM creation, activation processing, eviction, and shutdown; initial workflow bundling and history file loading are excluded. Configuration: Node v24.19.0 on Darwin arm64, public replay defaults, reuseV8Context: true, prebuilt bundles, default data converter, and error-only logging.

Each synthetic history has 1,000 intervening Workflow Tasks. Empty has no progress signals, sparse has ten, and the other workloads have 1,000. Progress handlers perform two await Promise.resolve() calls. Core produces 2, 12, 1,002, 4, and 4 language activations respectively; replay results, activation counts, signal counts, and evictions are asserted. These results describe these synthetic workloads and do not establish a universal production speedup.

@eamsden
eamsden force-pushed the eamsden/defer-promise-stack-formatting branch from 387be75 to e08d67d Compare October 14, 2026 23:12
@eamsden

eamsden commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Issue #1587 (live Workflow execution): A local comparison of 046dcef5 with this PR (e08d67d9) using the issue's 10,000-await reproducer. Results measure formatting alone; times are median client-to-result latency.

Workflow Baseline (ms) Deferred formatting (ms) Time saved Speedup
Sync helpers (control) 4.33 4.49 -3.7% 0.96×
Async helpers 257.18 124.04 51.8% 2.07×

The sync-helper control’s change is within measured noise.

(Five warmups and 30 paired measurements per workflow and variant; Node v24.19.0, Apple M5 Max, local Server 1.31.0, identical release Core and Workflow bundle, and ordinary Workflow worker threads with stack diagnostics enabled. This result is specific to the tight async-loop workload.)

This branch has not been deployed

No deployments
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