Skip to content

Investigate the possibility for a yielding hydration step - #5164

Draft
JoviDeCroock wants to merge 3 commits into
mainfrom
JoviDeCroock/issue-407-investigation
Draft

Investigate the possibility for a yielding hydration step#5164
JoviDeCroock wants to merge 3 commits into
mainfrom
JoviDeCroock/issue-407-investigation

Conversation

@JoviDeCroock

Copy link
Copy Markdown
Member

This would allow a self-serve kind of yield mechanism, an example can be seen below

function installSlicing() {
	// Published builds mangle option/vnode internals:
	//   options._yield -> __y, options._catchError -> __e,
	//   vnode._parent -> __, vnode._component -> __c
	options.__y = vnode => {
		if (vnode.__ && performance.now() > deadline) throw sentinel;
	};
	const prevCatchError = options.__e;
	options.__e = (error, vnode, oldVNode, errorInfo) => {
		if (error === sentinel) {
			pending.push(vnode.__c);
			schedule();
			return;
		}
		prevCatchError(error, vnode, oldVNode, errorInfo);
	};
}

Now my concern is that this is quite open-ended and the main thing this hook gives is the point of invocation as well as only invoking during hydration. Another thing that would warrant change if we introduce this is to enable yielding during updates i.e. if our render queue contains 5 items and the first takes up 30ms we should be able to pause and then resume the 4 next items.

This PR is very much tentative but wanted to show what I've been thinking about.

Freshly mounting components during hydration now invoke options._yield
right before rendering. Throwing a thenable from the hook suspends the
component through the existing suspended-hydration machinery
(MODE_SUSPENDED | MODE_HYDRATE + _excess) and forceUpdate() resumes the
subtree in place, letting an opt-in scheduler slice the hydration walk
into budgeted tasks.

Also clear COMPONENT_DIRTY when a component suspends before its first
render: the bit is set at instantiation and only cleared during render,
so a pre-render suspension left it set and the resuming forceUpdate was
swallowed by enqueueRender.

Costs +10 B brotli on core (4392 -> 4402).
Core: context provided across slices, refs on resumed subtrees,
shouldComponentUpdate bypass on resume, inline resume via parent
re-render racing a pending slice, the realistic deadline+pump scheduler,
and pinned-down single-DOM-root limitations (fragment-root node loss,
null-root sibling duplication) together with a shape-learning scheduler
policy that avoids both.

Compat: slicing composes with real Suspense/lazy suspensions, and hooks
components resumed in a slice run effects exactly once.

The deadline scheduler tests encode an important finding: resumed
components render unconditionally, so a scheduler must pump the resume
queue one component per microtask with deadline checks in between --
force-updating the whole queue re-hydrates all flat siblings in a
single long task.
The excess array rebuilt from _excess during a resume is owned solely by
the resuming component, so any nodes its subtree did not adopt are SSR
leftovers. Element frames already sweep those (diffElementNodes), but
the component resume path never did, so a node claimed by a component
that then rendered null stayed in the DOM forever next to the duplicate
its deopted sibling created.

This downgrades the null-root pitfall from permanent duplication to a
transient duplicate with lost node identity, matching the severity of
the fragment-root case. The sound fix for both remains span markers
emitted by render-to-string for components whose output is not exactly
one element.

Costs +20 B brotli on core (4402 -> 4422, +30 B total for the feature).
@github-actions

github-actions Bot commented Jul 17, 2026

Copy link
Copy Markdown

📊 Tachometer Benchmark Results

Summary

duration

  • create10k: unsure 🔍 -0% - +1% (-4.17ms - +12.61ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -1% - +0% (-0.15ms - +0.02ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -2% - +2% (-1.18ms - +1.12ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -5% - +2% (-0.77ms - +0.27ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -0% - +2% (-0.23ms - +1.22ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -4% - +5% (-0.09ms - +0.10ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -2% - +3% (-0.46ms - +0.64ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -2% - +4% (-0.54ms - +1.38ms)
    preact-local vs preact-main

usedJSHeapSize

  • create10k: unsure 🔍 +0% - +0% (+0.00ms - +0.01ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 +0% - +0% (+0.00ms - +0.01ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -4% - +2% (-0.19ms - +0.13ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 +0% - +0% (+0.00ms - +0.01ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 +0% - +0% (+0.00ms - +0.01ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -3% - +5% (-0.03ms - +0.05ms)
    preact-local vs preact-main
  • todo: unsure 🔍 +0% - +0% (+0.00ms - +0.01ms)
    preact-local vs preact-main
  • update10th1k: slower ❌ 0% - 1% (0.01ms - 0.02ms)
    preact-local vs preact-main

Results

create10k

duration

VersionAvg timevs preact-localvs preact-main
preact-local851.00ms - 861.79ms-unsure 🔍
-0% - +1%
-4.17ms - +12.61ms
preact-main845.74ms - 858.60msunsure 🔍
-1% - +0%
-12.61ms - +4.17ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local19.08ms - 19.08ms-unsure 🔍
+0% - +0%
+0.00ms - +0.01ms
preact-main19.08ms - 19.08msunsure 🔍
-0% - -0%
-0.01ms - -0.00ms
-
filter-list

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.55ms - 16.61ms-unsure 🔍
-1% - +0%
-0.15ms - +0.02ms
preact-main16.56ms - 16.72msunsure 🔍
-0% - +1%
-0.02ms - +0.15ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.56ms - 1.57ms-unsure 🔍
+0% - +0%
+0.00ms - +0.01ms
preact-main1.56ms - 1.56msunsure 🔍
-0% - -0%
-0.01ms - -0.00ms
-
hydrate1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local58.16ms - 59.76ms-unsure 🔍
-2% - +2%
-1.18ms - +1.12ms
preact-main58.16ms - 59.82msunsure 🔍
-2% - +2%
-1.12ms - +1.18ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local4.98ms - 5.19ms-unsure 🔍
-4% - +2%
-0.19ms - +0.13ms
preact-main5.00ms - 5.23msunsure 🔍
-2% - +4%
-0.13ms - +0.19ms
-
many-updates

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.51ms - 16.56ms-unsure 🔍
-5% - +2%
-0.77ms - +0.27ms
preact-main16.27ms - 17.31msunsure 🔍
-2% - +5%
-0.27ms - +0.77ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.75ms - 3.75ms-unsure 🔍
+0% - +0%
+0.00ms - +0.01ms
preact-main3.74ms - 3.74msunsure 🔍
-0% - -0%
-0.01ms - -0.00ms
-
replace1k
  • Browser: chrome-headless
  • Sample size: 100
  • Built by: CI #5687
  • Commit: c218192

duration

VersionAvg timevs preact-localvs preact-main
preact-local55.58ms - 56.66ms-unsure 🔍
-0% - +2%
-0.23ms - +1.22ms
preact-main55.14ms - 56.11msunsure 🔍
-2% - +0%
-1.22ms - +0.23ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.02ms - 3.02ms-unsure 🔍
+0% - +0%
+0.00ms - +0.01ms
preact-main3.01ms - 3.01msunsure 🔍
-0% - -0%
-0.01ms - -0.00ms
-

run-warmup-0

VersionAvg timevs preact-localvs preact-main
preact-local29.52ms - 30.39ms-unsure 🔍
-1% - +3%
-0.44ms - +0.77ms
preact-main29.37ms - 30.21msunsure 🔍
-3% - +1%
-0.77ms - +0.44ms
-

run-warmup-1

VersionAvg timevs preact-localvs preact-main
preact-local34.41ms - 35.90ms-unsure 🔍
-5% - +1%
-1.71ms - +0.39ms
preact-main35.08ms - 36.56msunsure 🔍
-1% - +5%
-0.39ms - +1.71ms
-

run-warmup-2

VersionAvg timevs preact-localvs preact-main
preact-local30.06ms - 31.35ms-unsure 🔍
-5% - +1%
-1.55ms - +0.28ms
preact-main30.70ms - 31.98msunsure 🔍
-1% - +5%
-0.28ms - +1.55ms
-

run-warmup-3

VersionAvg timevs preact-localvs preact-main
preact-local26.48ms - 26.97ms-unsure 🔍
-1% - +2%
-0.19ms - +0.48ms
preact-main26.35ms - 26.81msunsure 🔍
-2% - +1%
-0.48ms - +0.19ms
-

run-warmup-4

VersionAvg timevs preact-localvs preact-main
preact-local23.56ms - 25.04ms-unsure 🔍
-5% - +3%
-1.29ms - +0.82ms
preact-main23.79ms - 25.29msunsure 🔍
-3% - +5%
-0.82ms - +1.29ms
-

run-final

VersionAvg timevs preact-localvs preact-main
preact-local20.73ms - 21.11ms-unsure 🔍
-2% - +1%
-0.43ms - +0.12ms
preact-main20.87ms - 21.28msunsure 🔍
-1% - +2%
-0.12ms - +0.43ms
-
text-update
  • Browser: chrome-headless
  • Sample size: 140
  • Built by: CI #5687
  • Commit: c218192

duration

VersionAvg timevs preact-localvs preact-main
preact-local2.05ms - 2.19ms-unsure 🔍
-4% - +5%
-0.09ms - +0.10ms
preact-main2.05ms - 2.19msunsure 🔍
-5% - +4%
-0.10ms - +0.09ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.02ms - 1.08ms-unsure 🔍
-3% - +5%
-0.03ms - +0.05ms
preact-main1.01ms - 1.06msunsure 🔍
-5% - +3%
-0.05ms - +0.03ms
-
todo

duration

VersionAvg timevs preact-localvs preact-main
preact-local23.09ms - 24.05ms-unsure 🔍
-2% - +3%
-0.46ms - +0.64ms
preact-main23.21ms - 23.76msunsure 🔍
-3% - +2%
-0.64ms - +0.46ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.27ms - 1.27ms-unsure 🔍
+0% - +0%
+0.00ms - +0.01ms
preact-main1.27ms - 1.27msunsure 🔍
-0% - -0%
-0.01ms - -0.00ms
-
update10th1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local35.51ms - 36.88ms-unsure 🔍
-2% - +4%
-0.54ms - +1.38ms
preact-main35.10ms - 36.46msunsure 🔍
-4% - +1%
-1.38ms - +0.54ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.98ms - 2.99ms-slower ❌
0% - 1%
0.01ms - 0.02ms
preact-main2.97ms - 2.98msfaster ✔
0% - 1%
0.01ms - 0.02ms
-

tachometer-reporter-action v2 for CI

@github-actions

Copy link
Copy Markdown

Size Change: +34 B (+0.22%)

Total Size: 15.8 kB

📦 View Changed
Filename Size Change
dist/preact.mjs 4.92 kB +34 B (+0.7%)
ℹ️ View Unchanged
Filename Size
compat/dist/compat.mjs 3.87 kB
debug/dist/debug.mjs 3.89 kB
devtools/dist/devtools.mjs 274 B
hooks/dist/hooks.mjs 1.55 kB
jsx-runtime/dist/jsxRuntime.mjs 861 B
test-utils/dist/testUtils.mjs 473 B

compressed-size-action

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 98.534%. remained the same — JoviDeCroock/issue-407-investigation into main

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.

2 participants