Skip to content

Batch updates in flushSync#5173

Open
JoviDeCroock wants to merge 1 commit into
v10.xfrom
flush-sync-batch
Open

Batch updates in flushSync#5173
JoviDeCroock wants to merge 1 commit into
v10.xfrom
flush-sync-batch

Conversation

@JoviDeCroock

Copy link
Copy Markdown
Member

Summary

Make preact/compat’s flushSync batch updates performed inside its callback.

Previously, flushSync temporarily configured options.debounceRendering to execute every scheduled render immediately. This made updates synchronous, but it also caused each state setter to render independently:

flushSync(() => {
	setA(1);
	setB(1);
});

The example above could render twice instead of batching both updates into one synchronous commit.

This change captures the scheduled render callback while the user callback runs, then flushes it once before returning. The previous debounce function is restored in a finally block.

Results

Measured with Octane’s nested update benchmark:

Operation Before After Change
Batched sweep, ancestor-first 0.322 ms 0.081 ms −75%
Batched sweep, descendant-first 0.301 ms 0.079 ms −74%
Suite geomean 0.161 ms 0.116 ms −28%

Non-batched update timings remain unchanged.

@github-actions

Copy link
Copy Markdown

Size Change: +59 B (+0.07%)

Total Size: 79.5 kB

📦 View Changed
Filename Size Change
compat/dist/compat.js 4.2 kB +14 B (+0.33%)
compat/dist/compat.mjs 4.12 kB +15 B (+0.37%)
compat/dist/compat.module.js 4.12 kB +15 B (+0.37%)
compat/dist/compat.umd.js 4.27 kB +15 B (+0.35%)
ℹ️ View Unchanged
Filename Size
debug/dist/debug.js 3.85 kB
debug/dist/debug.mjs 3.85 kB
debug/dist/debug.module.js 3.85 kB
debug/dist/debug.umd.js 3.93 kB
devtools/dist/devtools.js 260 B
devtools/dist/devtools.mjs 274 B
devtools/dist/devtools.module.js 274 B
devtools/dist/devtools.umd.js 346 B
dist/preact.js 4.83 kB
dist/preact.min.js 4.84 kB
dist/preact.min.module.js 4.85 kB
dist/preact.min.umd.js 4.86 kB
dist/preact.mjs 4.85 kB
dist/preact.module.js 4.85 kB
dist/preact.umd.js 4.87 kB
hooks/dist/hooks.js 1.51 kB
hooks/dist/hooks.mjs 1.54 kB
hooks/dist/hooks.module.js 1.54 kB
hooks/dist/hooks.umd.js 1.58 kB
jsx-runtime/dist/jsxRuntime.js 1.01 kB
jsx-runtime/dist/jsxRuntime.mjs 985 B
jsx-runtime/dist/jsxRuntime.module.js 985 B
jsx-runtime/dist/jsxRuntime.umd.js 1.08 kB
test-utils/dist/testUtils.js 473 B
test-utils/dist/testUtils.mjs 477 B
test-utils/dist/testUtils.module.js 477 B
test-utils/dist/testUtils.umd.js 555 B

compressed-size-action

@github-actions

Copy link
Copy Markdown

📊 Tachometer Benchmark Results

Summary

duration

  • create10k: unsure 🔍 -0% - +1% (-2.76ms - +6.31ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -2% - +1% (-0.40ms - +0.11ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -4% - +1% (-2.23ms - +0.80ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -0% - +1% (-0.08ms - +0.17ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -1% - +2% (-0.64ms - +1.04ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -3% - +6% (-0.05ms - +0.10ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -2% - +1% (-0.58ms - +0.31ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -5% - +2% (-1.47ms - +0.68ms)
    preact-local vs preact-main

usedJSHeapSize

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

Results

create10k

duration

VersionAvg timevs preact-localvs preact-main
preact-local810.03ms - 816.08ms-unsure 🔍
-0% - +1%
-2.76ms - +6.31ms
preact-main807.90ms - 814.66msunsure 🔍
-1% - +0%
-6.31ms - +2.76ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local19.21ms - 19.21ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-main19.21ms - 19.21msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
filter-list

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.56ms - 16.68ms-unsure 🔍
-2% - +1%
-0.40ms - +0.11ms
preact-main16.52ms - 17.01msunsure 🔍
-1% - +2%
-0.11ms - +0.40ms
-

usedJSHeapSize

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

duration

VersionAvg timevs preact-localvs preact-main
preact-local57.20ms - 58.94ms-unsure 🔍
-4% - +1%
-2.23ms - +0.80ms
preact-main57.55ms - 60.03msunsure 🔍
-1% - +4%
-0.80ms - +2.23ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local5.02ms - 5.03ms-unsure 🔍
-3% - +1%
-0.17ms - +0.03ms
preact-main5.00ms - 5.20msunsure 🔍
-1% - +3%
-0.03ms - +0.17ms
-
many-updates

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.40ms - 16.57ms-unsure 🔍
-0% - +1%
-0.08ms - +0.17ms
preact-main16.35ms - 16.53msunsure 🔍
-1% - +0%
-0.17ms - +0.08ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.73ms - 3.73ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-main3.73ms - 3.73msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
replace1k
  • Browser: chrome-headless
  • Sample size: 100
  • Built by: CI #5708
  • Commit: 3393c0b

duration

VersionAvg timevs preact-localvs preact-main
preact-local55.38ms - 56.65ms-unsure 🔍
-1% - +2%
-0.64ms - +1.04ms
preact-main55.26ms - 56.37msunsure 🔍
-2% - +1%
-1.04ms - +0.64ms
-

usedJSHeapSize

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

run-warmup-0

VersionAvg timevs preact-localvs preact-main
preact-local27.52ms - 28.29ms-unsure 🔍
-1% - +3%
-0.17ms - +0.95ms
preact-main27.11ms - 27.93msunsure 🔍
-3% - +1%
-0.95ms - +0.17ms
-

run-warmup-1

VersionAvg timevs preact-localvs preact-main
preact-local31.64ms - 32.71ms-unsure 🔍
-3% - +2%
-0.87ms - +0.65ms
preact-main31.74ms - 32.83msunsure 🔍
-2% - +3%
-0.65ms - +0.87ms
-

run-warmup-2

VersionAvg timevs preact-localvs preact-main
preact-local31.59ms - 32.84ms-unsure 🔍
-4% - +1%
-1.24ms - +0.49ms
preact-main31.99ms - 33.19msunsure 🔍
-2% - +4%
-0.49ms - +1.24ms
-

run-warmup-3

VersionAvg timevs preact-localvs preact-main
preact-local25.89ms - 26.56ms-unsure 🔍
-1% - +2%
-0.27ms - +0.59ms
preact-main25.80ms - 26.33msunsure 🔍
-2% - +1%
-0.59ms - +0.27ms
-

run-warmup-4

VersionAvg timevs preact-localvs preact-main
preact-local22.87ms - 24.01ms-unsure 🔍
-1% - +6%
-0.25ms - +1.30ms
preact-main22.38ms - 23.44msunsure 🔍
-6% - +1%
-1.30ms - +0.25ms
-

run-final

VersionAvg timevs preact-localvs preact-main
preact-local21.04ms - 21.48ms-unsure 🔍
-1% - +1%
-0.24ms - +0.30ms
preact-main21.07ms - 21.39msunsure 🔍
-1% - +1%
-0.30ms - +0.24ms
-
text-update
  • Browser: chrome-headless
  • Sample size: 220
  • Built by: CI #5708
  • Commit: 3393c0b

duration

VersionAvg timevs preact-localvs preact-main
preact-local1.72ms - 1.82ms-unsure 🔍
-3% - +6%
-0.05ms - +0.10ms
preact-main1.69ms - 1.80msunsure 🔍
-5% - +3%
-0.10ms - +0.05ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.02ms - 1.07ms-unsure 🔍
-2% - +4%
-0.02ms - +0.04ms
preact-main1.01ms - 1.06msunsure 🔍
-4% - +2%
-0.04ms - +0.02ms
-
todo

duration

VersionAvg timevs preact-localvs preact-main
preact-local33.25ms - 33.79ms-unsure 🔍
-2% - +1%
-0.58ms - +0.31ms
preact-main33.30ms - 34.01msunsure 🔍
-1% - +2%
-0.31ms - +0.58ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.26ms - 1.26ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-main1.26ms - 1.26msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
update10th1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local30.33ms - 31.83ms-unsure 🔍
-5% - +2%
-1.47ms - +0.68ms
preact-main30.70ms - 32.25msunsure 🔍
-2% - +5%
-0.68ms - +1.47ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.99ms - 3.00ms-unsure 🔍
-0% - +0%
-0.01ms - +0.01ms
preact-main2.99ms - 3.00msunsure 🔍
-0% - +0%
-0.01ms - +0.01ms
-

tachometer-reporter-action v2 for CI

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