Repository navigation
fix(octane): run useDeferredValue's deferred render in a later task - #1867
Merged
Merged
Conversation
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]>
trueadm
marked this pull request as ready for review
October 7, 2026 21:30
This was referenced Oct 7, 2026
Contributor
Benchmark report
Compares js-framework: 1k/10k row operationsAll 20 timed operations (20 without a verdict)
All 64 work counters are unchanged. Application bundle size (bytes)
54 other measured values are unchanged. Public-import reachability bundles (bytes)No changes across 111 measured values. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,
spawnDeferredSwapqueued the swap withqueueMicrotask. 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,useDeferredValuegave 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 throughscheduler.postTask, then a freshMessageChannelper post, thensetTimeout(0).MessageChannelis 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.schedulePostPaint,actCheckpoint,resize-observer.ts, andresumeOnSettle.spawnDeferredSwap(runtime.ts) now adds slots to aDEFERRED_SWAPSqueue. A microtask after the spawning commit posts one task that swaps every queued slot.s.next, so a fast typist's skipped values never render deferred.s.scheduledgating, theDEFERRED_SPAWNtagging, the hidden/Activity reveal callers, and thehasHeldDeferredSwapSuspense hold.act(): the async drain loops also wait whileDEFERRED_SWAPSis set.hasPendingWork()stays synchronous-only, because@octanejs/testing-library'ssettleSyncloops on it.core-apis.mdxuseDeferredValueprose.SlowListis now wrapped inmemo, 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.octanepatch.Validation
packages/octane/tests/deferred-value-task.test.ts(new, 8 tests). Six failed againstmainbefore the fix.MessageChanneltask armed in the stale commit's layout effect runs before the deferred commit (commit 0/0, commit 1/0, task, commit 1/1).1/0 → 2/0 → 2/2, and1/1never renders.act()drains the posted swap.scheduler.postTaskis used when present.setTimeoutfallback works on hosts withoutscheduler.postTaskorMessageChannel.act()wait, the new act test and 6conformance/deferred-valuetests fail.--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:fileson the changed source, tests and fixtures;pnpm format:fileson 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.Performance audit
durationThreshold: 16and the maximum duration perinteractionId.useDeferredValue, and amemo'dSlowListthat busy-waits 40 ms. The app typed "quartz" (6 keys) in headless Chromium 149.0.7827.55, 3 trials per row.main'sruntime.ts.SlowListrenders whenever input is paced.main: interactions ≥ 16 ms (durations, ms)SlowListrenders (main→ PR)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.
useDeferredValuegrows 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 useuseDeferredValuepay nothing.MessageChannel(or onepostTaskcall) 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
act(). Asyncact()and testing-library'swaitFor/findBy*already do this.universal-core.ts'suseDeferredValue(Lynx, ink, and other non-DOM hosts) defers through a layout effect plusstartTransitionon the host's own microtask scheduler. Giving it a posted task belongs with the transition-flush step (G1), which needs a host hook.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Changes scheduling semantics for
useDeferredValueand asyncact()draining; code that awaited only microtasks for the deferred value must await a task oract(), 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→ freshMessageChannel→setTimeout(0)).spawnDeferredSwapqueues slots onDEFERRED_SWAPS, arms posting via one microtask after layout, then runs all swaps in a single task; urgent updates before the task only updates.next(skipped values never render deferred). Asyncact()drain loops also wait whileDEFERRED_SWAPSis 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; playgroundSlowListwrapped inmemo; 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.