Skip to content

fix(octane): run useDeferredValue's deferred render in a later task - #1867

Merged
domgan-oai merged 1 commit into
mainfrom
fix/deferred-swap-task
Oct 7, 2026
Merged

domgan-oai merged 1 commit into
mainfrom
fix/deferred-swap-task

Conversation

@trueadm

@trueadm trueadm commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Step 1 of the split proposed in the investigation comment on #1864: useDeferredValue's deferred render now runs in a later host task instead of a microtask.

Before this change, spawnDeferredSwap queued the swap with queueMicrotask. The stale urgent commit (1/0) and the deferred commit (1/1) therefore landed in the same microtask checkpoint. The browser could neither paint the keystroke nor deliver the next one until the slow deferred render had also finished. As a result, useDeferredValue gave no responsiveness benefit, despite what the runtime comment, the Core APIs docs and the playground example promised.

Changes

  • packages/octane/src/host-task.ts (new): postHostTask(callback) posts a host task through scheduler.postTask, then a fresh MessageChannel per post, then setTimeout(0).
    • The MessageChannel is fresh for each post because Node delivers pending port messages in port-creation order, not post order. (libuv walks its async handles in init order; I checked this on Node 24.18.) A long-lived port would run ahead of a task a newer port posted first.
    • It is a standalone module so the later Investigate microtask megatasks in streams, hydration, actions, and deferred rendering #1864 steps (transitions and action results, signal producers, island activation) can reuse it. I left the existing ad-hoc posters alone because each has its own semantics: schedulePostPaint, actCheckpoint, resize-observer.ts, and resumeOnSettle.
  • spawnDeferredSwap (runtime.ts) now adds slots to a DEFERRED_SWAPS queue. A microtask after the spawning commit posts one task that swaps every queued slot.
    • The post happens after the commit's layout effects, so the swap queues behind any task that commit posted, as React's Scheduler task does.
    • Urgent renders that arrive before the task only retarget s.next, so a fast typist's skipped values never render deferred.
    • Sibling deferred values still commit together.
    • Unchanged: the s.scheduled gating, the DEFERRED_SPAWN tagging, the hidden/Activity reveal callers, and the hasHeldDeferredSwap Suspense hold.
  • act(): the async drain loops also wait while DEFERRED_SWAPS is set. hasPendingWork() stays synchronous-only, because @octanejs/testing-library's settleSync loops on it.
  • Docs and wording:
    • Corrected the runtime comment that said the swap "can be interrupted by urgent updates".
    • Rewrote the core-apis.mdx useDeferredValue prose.
    • The playground's SlowList is now wrapped in memo, so the urgent render skips it; the example comment describes the timing.
    • differences-from-react.md § Scheduler documents the task-based swap and the remaining gap: there is no time-slicing, so a keystroke that arrives during a deferred render waits for it.
    • Updated stale "microtask" wording in deferred-value test comments and audit tables.
  • Changeset: octane patch.

Validation

  • packages/octane/tests/deferred-value-task.test.ts (new, 8 tests). Six failed against main before the fix.
    • A MessageChannel task armed in the stale commit's layout effect runs before the deferred commit (commit 0/0, commit 1/0, task, commit 1/1).
    • A keystroke delivered as a task before the swap retargets it: 1/0 → 2/0 → 2/2, and 1/1 never renders.
    • The stale value survives further microtask-only checkpoints.
    • Sibling deferred values commit together.
    • An unmounted slot's pending swap is dropped silently.
    • Async act() drains the posted swap.
    • scheduler.postTask is used when present.
    • The setTimeout fallback works on hosts without scheduler.postTask or MessageChannel.
  • Mutation checks (each reverted):
    • Without the act() wait, the new act test and 6 conformance/deferred-value tests fail.
    • Posting directly at spawn, without the microtask hop, fails the ordering and coalescing tests.
    • A long-lived channel fails the coalescing test in Node.
    • One task per slot fails the sibling test.
  • Targeted suites, all green:
    • --project octane: conformance/deferred-value, conformance/suspense-blocked, conformance/transitions-extra, suspense, transitions, hook-bugfixes, memo-value-position, hook-optional-arguments, slot-ergonomics, act, and the new file (162 tests).
    • profiling-runtime.test.tsrx (--project octane-profile).
    • website/tests/playground-examples.test.ts (--project website-unit).
  • pnpm typecheck:files on the changed source, tests and fixtures; pnpm format:files on every changed file; pnpm sync, which produced no generated changes.
  • pnpm format:check: not run locally (scoped check only).
  • pnpm typecheck: not run locally (scoped check only).
  • pnpm test: not run locally, per the repo's local-load rule; CI runs it.
  • targeted tests: listed above.

Performance audit

  • Target: keystroke-to-paint, using Event Timing with durationThreshold: 16 and the maximum duration per interactionId.
  • Workload: production Vite builds of the playground pattern: a controlled input, useDeferredValue, and a memo'd SlowList that busy-waits 40 ms. The app typed "quartz" (6 keys) in headless Chromium 149.0.7827.55, 3 trials per row.
  • Baseline: the same app built against main's runtime.ts.
  • Semantic control: both builds end with the same input value and list contents, and run the same number of SlowList renders whenever input is paced.
Input main: interactions ≥ 16 ms (durations, ms) This PR SlowList renders (main → PR)
Keys 120 ms apart, CPU 1× 6/6 (64–72, then 40 ×5) 1/6 (first key 24–32) 6 → 6
Keys 120 ms apart, CPU 4× 6/6 (80, then 40–48 ×5) 1/6 (first key 32–40) 6 → 6
Burst: unawaited CDP key events 15 ms apart 6/6, growing 184 → 272 5/6, 16–40 6 → 3 (coalesced)

The paced rows match React 19.2.7's profile from the investigation (1/6, 24 ms at 1× and 32–40 ms at 4×). The burst row is the no-time-slicing gap: a key that lands during a 40 ms deferred render waits for the rest of it. Keys that arrive before the next swap coalesce into it.

  • Bundle size: an app that uses useDeferredValue grows by 423 B raw and 154 B gzip. That covers the poster and the queue. In CI, the whole-framework bundle (full_fw, every export) grows by 439 B raw and 182 B gzip, within budget. The other 54 size values and all 111 public-import reachability bundles are unchanged, so apps that don't use useDeferredValue pay nothing.
  • Per-swap cost: one microtask, one MessageChannel (or one postTask call) per batch of deferred swaps, instead of one microtask closure per slot. That is once per urgent commit that changes a deferred value, not per render.

Risk / follow-ups

  • Timing change for code that awaited the deferred value. Code that awaited only microtasks after an urgent update and then expected the deferred value must now await a task or act(). Async act() and testing-library's waitFor/findBy* already do this.
  • Universal renderers are unchanged. universal-core.ts's useDeferredValue (Lynx, ink, and other non-DOM hosts) defers through a layout effect plus startTransition on the host's own microtask scheduler. Giving it a posted task belongs with the transition-flush step (G1), which needs a host hook.
  • Not in this PR: transition and action-result flushes in a task (G1 in full) and the G2 policy decision. Those are the next steps on Investigate microtask megatasks in streams, hydration, actions, and deferred rendering #1864.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Changes scheduling semantics for useDeferredValue and async act() draining; code that awaited only microtasks for the deferred value must await a task or act(), but core deferral/coalescing behavior is heavily regression-tested.

Overview
Fixes #1864 by moving useDeferredValue's deferred swap out of the urgent commit's microtask checkpoint into a later host task, so the browser can paint the stale urgent UI and take input before the slow deferred render runs.

Runtime: New postHostTask (scheduler.postTask → fresh MessageChannel → setTimeout(0)). spawnDeferredSwap queues slots on DEFERRED_SWAPS, arms posting via one microtask after layout, then runs all swaps in a single task; urgent updates before the task only update s.next (skipped values never render deferred). Async act() drain loops also wait while DEFERRED_SWAPS is set.

Tests & docs: New deferred-value-task.test.ts (ordering, coalescing, siblings, unmount, act, poster fallbacks). Wording updated from "microtask" to "later task" in conformance/audit/docs; playground SlowList wrapped in memo; Core APIs prose describes the timing contract (still no time-slicing once the deferred render starts).

Reviewed by Cursor Bugbot for commit f5bdeb7. Bugbot is set up for automated code reviews on this repo. Configure here.

The deferred swap was queued with queueMicrotask, so the stale urgent commit
and the deferred commit landed in the same microtask checkpoint. The browser
could neither paint the keystroke nor deliver the next one before the slow
deferred render finished, so useDeferredValue gave no responsiveness benefit
(#1864, step 1).

spawnDeferredSwap now queues slots and, from a microtask after the spawning
commit, posts one host task that swaps them all. Urgent renders before that
task only retarget s.next, so skipped values never render deferred, and
sibling deferred values still commit together. The poster lives in
host-task.ts: scheduler.postTask, then a fresh MessageChannel per post (Node
delivers port messages in port-creation order), then setTimeout. Async act()
waits for a pending swap; hasPendingWork() stays synchronous-only.

The runtime comment, core-apis docs, the playground example (now with a
memo'd SlowList), and differences-from-react.md now describe the new timing
and the remaining gap: there is no time-slicing, so input that arrives during
a deferred render waits for it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 7, 2026
@trueadm
trueadm marked this pull request as ready for review October 7, 2026 21:30
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-size: 6 value(s) increased within budget

Compares 31724a3f9, the merge commit's first parent with c3c8eaa0c on the same runner. A byte value over its committed budget or any increase in a js-framework work counter fails this check. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

All 20 timed operations (20 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 3.433 3.467 +0.0% −7.9% … +8.4% ⚪ within noise
octane-tsrx replace 9.992 9.600 −2.1% −8.6% … +10.6% ⚪ within noise
octane-tsrx add 2.825 2.817 −3.8% −8.3% … ±0.0% ⚪ within noise
octane-tsrx update 0.147 0.145 −1.8% −5.4% … +2.8% ⚪ within noise
octane-tsrx select 0.006 0.006 −4.4% −8.6% … −1.5% ⚪ within noise
octane-tsrx swap 0.038 0.037 +5.2% −1.9% … +9.3% ⚪ within noise
octane-tsrx remove 0.018 0.018 −1.4% −3.9% … +5.5% ⚪ within noise
octane-tsrx runlots 29.067 28.733 −1.0% −5.6% … +0.9% ⚪ within noise
octane-tsrx select_lots 0.007 0.007 −1.1% −3.2% … −0.3% ⚪ within noise
octane-tsrx clear 49.967 50.592 +1.9% −0.9% … +4.4% ⚪ within noise
octane-jsx run 3.067 3.125 +0.0% ±0.0% … +3.2% ⚪ within noise
octane-jsx replace 5.942 6.017 −1.7% −3.4% … ±0.0% ⚪ within noise
octane-jsx add 3.158 3.133 ±0.0% −3.1% … +3.3% ⚪ within noise
octane-jsx update 0.270 0.276 −0.3% −2.1% … +1.8% ⚪ within noise
octane-jsx select 0.120 0.124 +1.1% +0.5% … +2.1% ⚪ within noise
octane-jsx swap 0.216 0.216 +0.3% −0.5% … +1.1% ⚪ within noise
octane-jsx remove 0.161 0.159 −0.9% −2.0% … +0.0% ⚪ within noise
octane-jsx runlots 27.908 27.100 −0.2% −1.9% … +4.3% ⚪ within noise
octane-jsx select_lots 1.415 1.449 +1.8% +0.4% … +3.5% ⚪ within noise
octane-jsx clear 42.650 42.492 −0.2% −0.9% … +1.2% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

target metric base head Δ
octane-tsrx full_fw_raw 564,890 565,329 +439 (+0.1%) 🔴
octane-tsrx full_fw_gzip 169,881 170,063 +182 (+0.1%) 🔴
octane-tsrx full_fw_brotli 142,886 143,086 +200 (+0.1%) 🔴
octane-jsx full_fw_raw 564,890 565,329 +439 (+0.1%) 🔴
octane-jsx full_fw_gzip 169,881 170,063 +182 (+0.1%) 🔴
octane-jsx full_fw_brotli 142,886 143,086 +200 (+0.1%) 🔴

54 other measured values are unchanged.

Public-import reachability bundles (bytes)

No changes across 111 measured values.

@domgan-oai
domgan-oai merged commit 3a94771 into main Oct 7, 2026
74 of 78 checks passed
@domgan-oai
domgan-oai deleted the fix/deferred-swap-task branch October 7, 2026 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants