Skip to content

perf(octane): compile a catch-only @try to the error boundary - #2271

Merged
domgan-oai merged 2 commits into
mainfrom
perf/catch-only-try-error-block
Oct 28, 2026
Merged

domgan-oai merged 2 commits into
mainfrom
perf/catch-only-try-error-block

Conversation

@trueadm

@trueadm trueadm commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • A @try without @pending now compiles to errorBlock, the catch-only boundary <ErrorBoundary> already lowers to, instead of tryBlock. An app whose boundaries only catch errors no longer ships the Suspense runtime or the root render rollback.
  • errorBlock now matches tryBlock in two places it fell short, which also fixes <ErrorBoundary> on main:

Why

@octanejs/codemod (#2264) turns React class error boundaries into @try/@catch. On the weather benchmark's migrated React port, that idiomatic form cost 10.1 kB gzip more than <ErrorBoundary fallback>: 63,868 B against 53,742 B.

The runtime already treated the two forms the same. tryBlockImpl sets propagateSuspense ||= pendingBody === null (since #833), so a catch-only tryBlock passes suspension on exactly as errorBlock does. The compiler gated errorBlock on tc.propagateSuspense, though, and only the <ErrorBoundary> lowering sets that. Every authored catch-only @try therefore went through tryBlock, and tryBlock = rollbackEntry(tryBlockImpl) installs the render rollback at module load. That retains the root journal, its replay, suspension, the hidden-primary and transition-hold code, and the visibility driver.

The fix belongs in the compiler, not in the codemod choosing <ErrorBoundary> to dodge the cost.

Changes

  • Compiler (compile.js, planJsx): catchOnly is any @try with a catch arm and no @pending. The one exception is a presentation-binding view's catch-only @try, which stays on tryBlock. There, ssrTry receives propagateSuspense: false and owns the suspension, so the client must still be able to claim a streamed arm, which only tryBlock's claimClientArm does. The rule is now: errorBlock exactly where the server passes suspension on. makeTryCall records bindingSite, as makeIfCall already does.
  • Runtime (suspense.ts, switchErrorToCatchInner): calls the existing dropBodyOps(transaction, previous) before unmounting the failed body, mirroring switchToCatchInner. Without it, errorBlock committed the failed body's attribute writes, new @if arm and stylesheet <link> before the catch arm replaced it. Routing @try through errorBlock turned this up: 4 cases in try-failed-body-ops.test.ts went red. The same leak exists today for <ErrorBoundary>.
  • Runtime (suspense.ts, mountErrorBoundary): a boundary the server left to the client (its body threw on the server) now renders on the client in full, catch switch included, inside hydration.suspend, as tryBlock's clientRenderDehydratedTry does. Before, errorBlock suspended hydration only for the body render, so when that body threw, the catch switch treated the body's client-built DOM as adopted server DOM and left it in place. CI found this in hydration/try-clone-thrower.test.ts: a nested pending-only @try left its <section> beside the catch arm. It is also a main bug for <ErrorBoundary>, now covered by an UncaughtBoundary twin.
  • Test adaptation (same-file-void-root.test.ts): the fixture suspended its root with a raw throw gate in a production bundle whose only boundaries are catch-only @try. Under the behavior change below, that bundle no longer ships suspension, as an ErrorBoundary-only one never did. The test's subject is when error callbacks are read, so it now suspends through use(gate).
  • Docs (differences-from-react.md): the client suspension-entry list now says @try with @pending, and a bundle whose only boundaries are ErrorBoundary or catch-only @try ships no rollback.

Behavior change to review

In a bundle with no entry that can suspend (use, lazy, Suspense, @try … @pending, Hydrate/hydration, native signal reads, ViewTransition, renderer regions), a thenable thrown during render inside a catch-only @try is now an ordinary render error that the @catch arm catches. Before, the root suspended. This is the documented ErrorBoundary behavior, and the brief asked for catch-only @try to take the ErrorBoundary path. It is a pay-for-what-you-use divergence from React, which suspends on that thenable. An app with any suspending entry is unaffected. benchmarks/bundle-size render-errors now pins it: WaitsGuarded is a catch-only @try.

Validation

  • pnpm format:check: not run repo-wide; pnpm format:files:check on every changed file passes

  • pnpm typecheck: not run repo-wide; pnpm typecheck:files reports no error on changed lines (suspense.ts clean; the test files carry pre-existing TS4111/TS7006 errors on untouched lines)

  • pnpm test: not run locally; left to CI

  • targeted tests, --project octane: try-catch, boundary, boundary-unwind, try-failed-body-ops, root-error-callbacks, replayed-try-slots, compiled-boundaries, hydration/{try-catch-arm,trycatch-client-error,catch-destructured-param,boundary-fallback-error,rangeless-error-boundary}: 12 files, 255 tests pass. behavior-root binding @try hydration cases pass. node --test benchmarks/bundle-size/{minimal,hydration-free}-gates.test.mjs: 14 pass.

  • Tests seen failing before the fix:

    • bundler-compiler.test.ts › "lowers a @Try without @pending to the catch-only boundary" fails with the old gate (no errorBlock) and fails with the binding exclusion removed (no tryBlock in the binding view).
    • try-failed-body-ops.test.ts: without the dropBodyOps call, 6 cases fail in dev and prod, including the new FailingBoundary (<ErrorBoundary>) twin, which fails the same way on main's errorBlock.
    • run-minimal.mjs render-errors: with the old gate, the scenario retains all 23 render-rollback declarations plus tryBlockImpl, and the reachability gate fails.
    • hydration/try-clone-thrower.test.ts › "…inner pending-only boundary throws": with the catch switch outside suspend, both Uncaught (@try) and the new UncaughtBoundary (<ErrorBoundary>) leave <section></section> beside the catch arm.
  • After the CI round on 6f47340692 (failures in try-clone-thrower and same-file-void-root; shards 1, 2 and 4 were cancelled mid-run), the changed files pass locally in octane and octane-prod. So do the other production-bundle suites that use @catch: optional-driver-bundle, production-error-bundle, hydration/split-hydrate-local-bindings, compiler/signal-declarations.

  • Size A/B (same session, vite build, esbuild minify):

    Bundle main raw / gzip head raw / gzip Δ gzip
    weather port, @try (varG) 191,473 / 63,868 159,244 / 53,840 −10,028
    weather port, <ErrorBoundary> (varH) 158,962 / 53,742 159,244 / 53,840 +98 (both errorBlock fixes)
    run-minimal render-errors 105,418 / 34,426 105,693 / 34,538 +112 (both fixes; fixture now also has a @try)
    run-minimal suspense-transition 150,022 / 49,060 150,022 / 49,060 0

    After the change, the @try and <ErrorBoundary> weather bundles are the same size. The only growth is the two errorBlock correctness fixes, which every bundle that keeps errorBlock pays. Head numbers are from 528628508a; the perf A/B below ran on 6f47340692, whose only difference from head is the error-path-only hydration fix.

  • CI benchmark report (4956093ee, the merge of 6f47340692):

    • All 64 js-framework work counters are unchanged, and every timed operation is within noise.
    • The byte rows that grew all come from the dropBodyOps correctness fix in errorBlock:
      • weather_js +258 raw / +91 gzip and weather_fw +258 / +90. The in-repo weather app uses <ErrorBoundary>, so it gains the fix's bytes without the @try saving.
      • full_fw +46 raw / +6 gzip.
      • render-errors +258 / +99 / +152 brotli.
    • The hydration fix in 528628508a adds 17 B raw / 13 B gzip more to bundles that keep errorBlock.
  • Perf A/B (same session, alternating, production builds):

    • Boundary update hot path: a scratch production bundle with a catch-only boundary around an 8-row keyed list, 20,000 flushSync updates × 7 rounds alternating main/head under jsdom, run twice.
      • @try: 3.31 → 3.09 µs/update (−6.5%), then 3.46 → 3.30 (−4.7%).
      • <ErrorBoundary> in the same bundle: −5.0% and −3.7%, since the bundle no longer journals root renders.
      • Sample ranges do not overlap (main 65–78 ms, head 60–68 ms per round).
    • benchmarks/weather-app/run.mjs 12 over four builds (@try/<ErrorBoundary> × main/head), two rounds in reversed target order. The <ErrorBoundary> pair has the same render path on both sides and sets the noise floor (−1.5% to −6.2%). @try stays inside it: initial_ready −5.2%/−2.3%, forecast_cycle −2.4%/0.0%, search_* within 0.1 ms timer quantization (rme 5–15%). No regression is measurable. No app-level speedup is claimed.
  • perf-review: node scripts/perf-review-scan.mjs → 0 candidates.

    • Hot path touched: the compiled boundary call. A catch-only @try now emits a 6-argument errorBlock(…) instead of a 7-argument tryBlock(…, null, …), and errorBlock's update path is a direct renderBlock with no journal arm, capture window or effect-dep snapshot.
    • dropBodyOps runs only when a body throws (cold path, one scan of the root's queued ops, the same as tryBlock).
    • No new Block/Scope fields, records, allocations, DOM reads or scheduling.
  • concise-code: src +30 −11 (runtime +20 −7); no new files or exports outside test fixtures. The one new function, renderErrorBody, is mountErrorBoundary's existing try/catch, lifted so the fresh boundary can run it inside suspend.

    • Alternatives considered:
      • A separate catch-only driver: rejected, since errorBlock already is one.
      • Making tryBlock skip rollbackEntry when catch-only: impossible, because the entry installs at module load and tryBlockImpl names the whole Suspense implementation.
      • Having the codemod emit <ErrorBoundary>: masks the defect.
    • Bytes: rows above.

Risk / follow-ups

  • Binding views: a catch-only @try in a 'use dom bindings' view keeps tryBlock and its cost. Moving it would need errorBlock to claim a streamed or client-rendered arm, as claimClientArm does.
  • Dead code, untouched here: planJsx's trailing-argument conditions on !catchOnly && tc.propagateSuspense were already unreachable, because only the <ErrorBoundary> lowering sets propagateSuspense, and that lowering is always catch-only.
  • CI covers the wide suites (octane-prod, hydration, browser) that were not run locally.

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.


Note

Medium Risk
Touches compiler boundary selection and error-boundary commit paths; behavior shifts for catch-only @try in non-suspending bundles when render throws thenables.

Overview
Catch-only @try { … } @catch (no @pending) now lowers to errorBlock, the same runtime as <ErrorBoundary>, instead of tryBlock. Apps that only need error catching no longer pull in Suspense machinery or the root render rollback (~10 kB gzip on the weather benchmark’s codemod-style @try port).

Compiler: catchOnly is any @try with @catch and no @pending (plus propagateSuspense or no binding site). Presentation 'use dom bindings' views keep tryBlock so the client can still claim streamed SSR arms.

Runtime: When a catch-only boundary’s try body throws on update, dropBodyOps runs before unmount (matching full @try), so queued attribute writes, new nodes, and stylesheet resources from the failed render never commit. This also fixes the same leak on <ErrorBoundary>.

Semantics: In bundles with no suspending entry, a thenable thrown inside catch-only @try/ErrorBoundary is a normal render error caught by @catch (not root suspend)—aligned with documented ErrorBoundary behavior and Octane’s pay-for-what-you-use model.

Docs, bundle-size render-errors fixture/gates, and tests cover codegen, failed-body ops, and hydration.

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

A `@try` without `@pending` passes suspension on, so it is the same
boundary as `<ErrorBoundary>`. The compiler only sent the imported
`<ErrorBoundary>` lowering to `errorBlock`; an authored catch-only `@try`
went through `tryBlock`, whose module-load `rollbackEntry` and Suspense
machinery kept the root render journal and hidden-primary code in every
app that used it (+10.1 kB gzip on the weather app's migrated port).

Lower every catch-only `@try` to `errorBlock`, except in a presentation-
binding view, whose server render owns that suspension and whose client
must still claim a streamed arm.

`errorBlock` committed the work a failed body queued when the body threw
during an update: its attribute writes, new structure and stylesheet
resources reached the page before the catch arm replaced it (#2043).
It now drops them with `dropBodyOps`, as `tryBlock` does. This also
fixes `<ErrorBoundary>`.

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 perf PR: performance work labels Oct 28, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website 5286285 Commit Preview URL

Branch Preview URL
Oct 28 2026, 10:47 PM

@trueadm
trueadm marked this pull request as ready for review October 28, 2026 22:21
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website-mcp 5286285 Commit Preview URL

Branch Preview URL
Oct 28 2026, 10:45 PM

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-size: 11 value(s) increased
  • 🔴 bundle-reachability: 3 value(s) increased

Compares 722b602ae, the merge commit's first parent with c719d7101 on the same runner. Any increase in a js-framework work counter fails this check; byte changes are a report. 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.958 3.600 ±0.0% −10.8% … +4.9% ⚪ within noise
octane-tsrx replace 8.258 8.200 +1.2% −9.0% … +5.6% ⚪ within noise
octane-tsrx add 3.108 3.508 +2.6% −4.4% … +11.5% ⚪ within noise
octane-tsrx update 0.090 0.097 −1.2% −4.6% … +5.6% ⚪ within noise
octane-tsrx select 0.007 0.008 +10.7% +2.0% … +21.5% ⚪ within noise
octane-tsrx swap 0.022 0.022 +7.5% −2.4% … +10.9% ⚪ within noise
octane-tsrx remove 0.023 0.023 −0.0% −6.8% … +8.5% ⚪ within noise
octane-tsrx runlots 29.958 29.458 −4.8% −16.9% … +5.7% ⚪ within noise
octane-tsrx select_lots 0.007 0.008 +1.3% −7.0% … +11.7% ⚪ within noise
octane-tsrx clear 53.825 53.608 −1.9% −5.0% … +5.4% ⚪ within noise
octane-jsx run 3.925 3.867 −2.7% −7.7% … +5.3% ⚪ within noise
octane-jsx replace 6.958 7.683 +5.1% ±0.0% … +15.9% ⚪ within noise
octane-jsx add 3.458 3.408 +1.4% −4.5% … +4.7% ⚪ within noise
octane-jsx update 0.170 0.175 −1.9% −5.1% … +1.6% ⚪ within noise
octane-jsx select 0.094 0.088 −0.4% −3.2% … +3.7% ⚪ within noise
octane-jsx swap 0.142 0.141 +2.0% −4.7% … +3.9% ⚪ within noise
octane-jsx remove 0.127 0.120 −2.8% −7.2% … −0.5% ⚪ within noise
octane-jsx runlots 28.408 27.642 −1.6% −6.1% … +3.1% ⚪ within noise
octane-jsx select_lots 0.912 0.924 −0.9% −3.9% … +3.1% ⚪ within noise
octane-jsx clear 45.092 46.275 +1.0% −1.1% … +4.4% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

target metric base head Δ
octane-tsrx weather_js_raw 119,739 120,014 +275 (+0.2%) 🔴
octane-tsrx weather_js_gzip 40,449 40,543 +94 (+0.2%) 🔴
octane-tsrx weather_js_brotli 35,999 36,043 +44 (+0.1%) 🔴
octane-tsrx weather_app_brotli 7,125 7,132 +7 (+0.1%) 🔴
octane-tsrx weather_fw_raw 95,756 96,031 +275 (+0.3%) 🔴
octane-tsrx weather_fw_gzip 32,065 32,159 +94 (+0.3%) 🔴
octane-tsrx weather_fw_brotli 28,874 28,911 +37 (+0.1%) 🔴
octane-tsrx full_fw_raw 535,517 535,580 +63 (+0.0%) 🔴
octane-tsrx full_fw_gzip 168,175 168,183 +8 (+0.0%) 🔴
octane-tsrx full_fw_brotli 143,073 143,069 −4 (−0.0%) 🟢
octane-jsx full_fw_raw 535,517 535,580 +63 (+0.0%) 🔴
octane-jsx full_fw_gzip 168,175 168,183 +8 (+0.0%) 🔴
octane-jsx full_fw_brotli 143,073 143,069 −4 (−0.0%) 🟢

98 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
render-errors raw 105,418 105,693 +275 (+0.3%) 🔴
render-errors gzip 34,426 34,538 +112 (+0.3%) 🔴
render-errors brotli 30,998 31,138 +140 (+0.5%) 🔴

138 other measured values are unchanged.

… arm included

A boundary the server left to the client (its body threw on the server)
rendered its body with hydration suspended, but switched to its catch arm
after hydration resumed. The catch switch then treated the failed body's
client-built DOM as adopted server DOM and left it in place: a nested
pending-only @Try that threw left its `<section>` beside the catch arm.
Run the whole fresh render inside `suspend`, as tryBlock's
clientRenderDehydratedTry does. This also fixes `<ErrorBoundary>`.

same-file-void-root suspended its root with a raw `throw gate` in a bundle
whose only boundaries are catch-only @Try. That bundle no longer ships
suspension, as an ErrorBoundary-only one never did, so the test now
suspends through use().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@domgan-oai
domgan-oai merged commit 74de956 into main Oct 28, 2026
34 checks passed
@domgan-oai
domgan-oai deleted the perf/catch-only-try-error-block branch October 28, 2026 23:25
@github-actions github-actions Bot mentioned this pull request Oct 28, 2026
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 perf PR: performance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants