Skip to content

Use MessageChannel fallback in browser scheduler - #11344

Open
effect-bot wants to merge 5 commits into
mainfrom
agent/effect-bot/3782136eaf58
Open

effect-bot wants to merge 5 commits into
mainfrom
agent/effect-bot/3782136eaf58

Conversation

@effect-bot

@effect-bot effect-bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In browsers without setImmediate or scheduler.postTask (notably Safari/WebKit), MixedScheduler fell through to setTimeout(f, 0) and hit the browser nested-timer clamp (~4–8 ms per yield).

This PR adds a browser-gated MessageChannel fallback between scheduler.postTask and setTimeout:

  • Keeps scheduler.postTask ahead of MessageChannel so Chromium/Firefox stay on postTask.
  • Gates MessageChannel on browser globals (window or importScripts) so non-browser runtimes without setImmediate (such as Cloudflare Workers) stay on setTimeout and preserve the global-scope microtask fallback without leaving an open port.
  • Lazily creates a single shared MessageChannel on first use, with an O(1) linked-list FIFO, per-task cancellation that clears the queued callback, and one postMessage per callback so a throwing task does not drop the ones behind it.
  • Adds unit tests in packages/effect/test/Scheduler.test.ts covering both window and importScripts browser contexts, FIFO ordering across dispatchers, and cancellation on flush.

Benchmarks (pnpm hostperf --host chromium,webkit --sample-ms 100)

Measured over 3 rounds against origin/main (14218bb7d7):

WebKit (Playwright Linux WPE build 2370 / Safari 27.2)

Case main PR Speedup (95% CI) Verdict
Effect/yield 8.00 ms 10.44 µs 766.0x (702.4x–774.2x) faster
Pool/poolUseContended 3.88 ms 13.62 µs 284.5x (256.5x–290.1x) faster
Effect/forkJoin 80.00 µs 766.7 ns 104.3x (100.4x–113.2x) faster
Pool/get and release (fixed pool) 16.00 µs 399.2 ns 40.1x (39.8x–41.0x) faster
Queue/queueTake 16.00 µs 488.9 ns 32.7x (29.4x–33.1x) faster
HashMap/set n fresh string keys + get all 383.9 ns 362.1 ns 1.06x (1.00x–1.09x) inconclusive
Schema/decode struct (valid) 422.2 ns 414.8 ns 1.02x (0.93x–1.06x) inconclusive
app/app-untraced 1.02 µs 1.03 µs 0.99x (0.92x–1.01x) inconclusive

Chromium (HeadlessChrome/156.0.8078.4, still on scheduler.postTask)

Case main PR Speedup (95% CI) Verdict
Effect/yield 4.93 µs 4.50 µs 1.09x (1.03x–1.24x) inconclusive
Effect/forkJoin 592.0 ns 558.9 ns 1.06x (0.95x–1.13x) inconclusive
Queue/queueTake 438.5 ns 440.2 ns 1.00x (0.92x–1.04x) inconclusive
Pool/get and release (fixed pool) 411.0 ns 419.7 ns 0.98x (0.86x–1.00x) inconclusive
Pool/poolUseContended 11.20 µs 11.51 µs 0.97x (0.95x–1.05x) inconclusive
HashMap/set n fresh string keys + get all 151.3 ns 152.0 ns 1.00x (0.94x–1.16x) inconclusive
Schema/decode struct (valid) 447.6 ns 451.1 ns 0.99x (0.90x–1.12x) inconclusive
app/app-untraced 1.23 µs 1.25 µs 0.99x (0.92x–1.01x) inconclusive

Bundle Size (pnpm bundle-compare 14218bb7d7)

  • basic.ts: 6.58 KB -> 6.73 KB (+0.14 KB, +2.19%)
  • Other runtime fixtures: about +0.13 KB
  • Non-runtime fixtures (brand.ts, optic.ts): 0.00 KB

Validation

  • pnpm test run packages/effect/test/Scheduler.test.ts packages/effect/test/ui/html/HtmlScheduler.test.ts (25 passed)
  • pnpm check-tsc (passed)
  • oxlint -f unix (passed)
  • pnpm hostperf --host chromium,webkit --sample-ms 100 against origin/main
  • pnpm bundle-compare origin/main

Closes EFF-5083

@changeset-bot

changeset-bot Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6fcf38c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 43 packages
Name Type
effect Patch
@effect/opentelemetry Patch
@effect/vitest Patch
@effect/ai-amazon-bedrock Patch
@effect/ai-anthropic Patch
@effect/ai-cloudflare Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/ai-typesafe Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/chat-discord Patch
@effect/chat-slack Patch
@effect/chat-teams Patch
@effect/chat-telegram Patch
@effect/chat-whatsapp Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-cloudflare Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/graphql-generator Patch
@effect/openapi-generator Patch
@effect/ui-devtools Patch
@effect/ui-native Patch
@effect/ui-opentui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@effect-janitor effect-janitor Bot added the 4.0 label Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 40.50 KB 40.36 KB +0.14 KB (+0.33%)
basic.ts 6.71 KB 6.57 KB +0.14 KB (+2.19%)
batching.ts 11.94 KB 11.80 KB +0.14 KB (+1.19%)
brand.ts 6.91 KB 6.91 KB 0.00 KB (0.00%)
cache.ts 12.21 KB 12.06 KB +0.15 KB (+1.22%)
config.ts 23.93 KB 23.78 KB +0.15 KB (+0.62%)
differ.ts 22.80 KB 22.65 KB +0.15 KB (+0.66%)
http-client.ts 28.95 KB 28.82 KB +0.12 KB (+0.43%)
http-router.ts 39.62 KB 39.49 KB +0.13 KB (+0.32%)
logger.ts 12.39 KB 12.25 KB +0.14 KB (+1.13%)
metric.ts 10.21 KB 10.07 KB +0.14 KB (+1.34%)
optic.ts 7.14 KB 7.14 KB 0.00 KB (0.00%)
pubsub.ts 16.94 KB 16.81 KB +0.14 KB (+0.80%)
queue.ts 13.76 KB 13.61 KB +0.14 KB (+1.05%)
schedule.ts 12.51 KB 12.37 KB +0.14 KB (+1.10%)
schema-bigdecimal.ts 14.55 KB 14.40 KB +0.15 KB (+1.08%)
schema-binary.ts 43.49 KB 43.36 KB +0.13 KB (+0.30%)
schema-class.ts 22.55 KB 22.41 KB +0.14 KB (+0.63%)
schema-codegen.ts 33.91 KB 33.77 KB +0.14 KB (+0.42%)
schema-fromJsonSchemaDocument.ts 31.00 KB 30.87 KB +0.13 KB (+0.43%)
schema-representation-roundtrip.ts 28.75 KB 28.59 KB +0.15 KB (+0.54%)
schema-string-transformation.ts 16.31 KB 16.17 KB +0.14 KB (+0.87%)
schema-string.ts 12.82 KB 12.69 KB +0.13 KB (+1.04%)
schema-template-literal.ts 16.91 KB 16.77 KB +0.14 KB (+0.83%)
schema-toArbitrary.ts 40.05 KB 39.91 KB +0.14 KB (+0.35%)
schema-toCodecJson.ts 21.70 KB 21.54 KB +0.16 KB (+0.73%)
schema-toEquivalence.ts 21.86 KB 21.71 KB +0.15 KB (+0.71%)
schema-toFormatter.ts 21.95 KB 21.81 KB +0.14 KB (+0.64%)
schema-toJsonSchemaDocument.ts 26.99 KB 26.83 KB +0.15 KB (+0.58%)
schema-toRepresentation.ts 22.01 KB 21.88 KB +0.14 KB (+0.62%)
schema.ts 21.66 KB 21.52 KB +0.14 KB (+0.65%)
stm.ts 15.02 KB 14.87 KB +0.14 KB (+0.97%)
stream.ts 10.75 KB 10.62 KB +0.13 KB (+1.23%)
supervisor.ts 12.17 KB 12.04 KB +0.14 KB (+1.13%)

UI Bundle Size

Vite 8.4.0/Rolldown, production, default Oxc minification, es2023, ES modules, gzip level 9 per chunk. effect 4.2.0, Node v26.12.1. Effect-only client: 27456 B gzip (Δ +131 B vs base).

App Chunks Raw bytes Gzip bytes Added gzip B Δ raw bytes Δ gzip bytes
counter 1 175790 60634 33178 +355 +144
features 1 185442 64097 36641 +355 +113
menu 1 289938 98749 71293 +355 +130

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant