Skip to content

Reduce client runtime bundle size while preserving the API #1801

Closed as not planned

Description

@o-alexandrov

Goal

Make Octane's client runtime smaller without changing its public API.

Same production app, same minifier settings (oxc, then swc), gzip:

Size
Preact + hooks + preact/compat + signals core 11.4 kB
React + react-dom 59.4 kB
Octane, what that app ships 133.4 kB

The bundle-size benchmark shows the same picture: Octane's full client runtime is about 173–179 kB gzip there. Small demo apps look fine (the rows demo is 38 kB), because unused features get tree-shaken out. Real apps that use hydration, Suspense, transitions, view transitions, forms and signals end up close to the full size.

Not the cause: development-only code is already stripped from production builds, error text is under 300 bytes, and a stronger minifier gives about 11% at most (for every framework equally).

Questions

The size mostly comes from a few design choices. Each one needs a decision before work can start.

1. Should Octane render off-screen and commit, like React?
Octane writes to the real DOM while rendering. To support transitions that get abandoned, it records every write so it can undo it, and that logic is spread across hundreds of places. React renders into a separate in-memory tree and simply throws it away, which costs no extra code. This is the largest structural cost.
Question: is changing this on the table (a big rewrite, and the speed trade-offs need measuring), or should we treat it as a fixed floor and optimise around it?

2. Can size budgets require an equal removal?
Several recent PRs exist only to raise size budgets (#1793, #1794, #1797, #1806).
Question: can we adopt a rule that a size increase must be offset by an equal removal, unless a maintainer explicitly approves the feature it pays for?

3. Can the near-identical fast-path copies be merged?
Many functions exist in several versions for speed, for example hookMemoPublish0 through hookMemoPublish4, or componentSlotLite/Impl/Void.
Question: may I merge copies where benchmarks show no measurable slowdown? I'll post numbers per pair before changing anything.

4. Should signals have a size budget?
Signals are about 20 kB gzip in this app, versus about 1.6 kB for Preact's signals core. Some of that is real extra capability (async data, server seeding).
Question: is there a target size for the signals runtime, and are any of its features candidates for a separate import?

5. Small design calls that unlock small savings

Idea Saving (gzip) Decision needed
Skip signal checks in apps without signals about 540 B Should passing RootOptions.signalOwner switch the signal machinery on? Today any root may receive it, so every app keeps the checks.
Restore server signal values only in apps that use signals 485 B – 1.5 kB in hydrating apps If the first page using signals is lazy-loaded after hydration starts, it would lose its server values. Fix by having the compiler mark entry points that read signals, or by keeping the raw seed text for the signals engine to read later.
Make transition undo, focus preservation and portal events opt-in about 7 kB, 4.5 kB and 1.7 kB minified Every root keeps them because any update could be a transition. An opt-in would be an API change.

In progress, no decision needed

Ruled out

Idea Why
Move setHTML's hydration code into the hydration class (#1785) Saves 250 B in client-only apps but adds 210–270 B to every hydrating app
Skip Fragment ref code in createRoot createElement(Fragment, { ref }) is public API and needs it
One helper for the 907 STAGED_DOM?.view(x) ?? x wrappers About 100 B, and it adds a function call to most DOM writes
Share duplicated code between functions Gzip already compresses repeats; the best case measured was −81 B
Shorten internal property names About 3%

How to read the numbers

  • Demo app size: what one benchmark app ships after tree-shaking.
  • Full size: everything Octane exports, with nothing tree-shaken. Only making the code itself smaller lowers it.
  • Runtime only; the compiler isn't included.
  • Don't compare gzip with brotli. Brotli is about 16% smaller on the same code.

Activity

  1. trueadm commented on Oct 6, 2026

    @trueadm
    Collaborator

    Posted by Claude Code (an agent) at @trueadm's request. This is a measurement report, not maintainer decisions. The two calls that need a maintainer are listed at the end.

    I measured each question on current main, ed57b1ea52. #1784, #1792, #1802 and #1803 have all merged since the issue was opened.

    Headline:

    • A set of gates that change no API cuts rows by 15.5% and TodoMVC by 13%, at no runtime cost.
    • The single largest lever is not in the issue's list: the generic childSlot renderer, a one-step cliff of about 33 kB gzip.
    • The undo journal from Q1 is also the largest runtime cost on ordinary updates, not only a byte cost.

    How it was measured:

    • Bytes: framework chunk only, Vite 8 with oxc, gzip -9, built exactly like benchmarks/bundle-size/run.mjs. On main: rows 33,866; todo 36,788; ceiling 168,939.
    • I also built a kitchen-sink app that uses every React-parity API: 98,140 B. Adding signals and <Hydrate> takes it to 151,886 B, which is close to the 133 kB app in the issue.
    • Runtime: benchmarks/js-framework/pair.mjs against main, for both the .tsrx and the .tsx rows apps. It reports exact production call counts and DOM mutation counts from jitless Chromium, plus paired wall time over 30 pairs with a 95% interval. Wall time only counts as faster or slower outside ±3%.
    • "Prototype" below means a working local patch; "knockout" means the feature was deliberately removed to bound its bytes. No PRs are open yet.

    Q1: render off-screen and commit, like React?

    Recommendation: keep rendering in place, but make the journal cheaper.

    Bytes, by knockout (the knockouts break the feature; they are upper bounds):

    Removed rows todo kitchen sink ceiling
    Transition + root journal −5,679 (−16.8%) −6,865 −8,240 −9,085
    Swap driver (held transitions, off-screen swaps) −192 −215 −1,872 −1,836
    STAGED_DOM wrappers + DOMStage (ViewTransition staging) −620 −741 −6,146 −6,633
    • The issue's "transition undo ≈ 7 kB minified" is the swap driver (6.5–6.7 kB raw). It already ships only to apps that use Suspense or transitions.
    • The cost every app pays is the root journal. Every scheduled render opens one (beginRootRender in the drain loop), so writes are journaled on ordinary synchronous updates too.

    Runtime: the journal is the biggest per-update cost. Turning it off for urgent renders, measured on a quiet machine:

    op .tsrx wall .tsx wall .tsrx calls
    update every 10th −31% −48% −14%
    remove −27% −55% −0.3%
    swap −21% −42% −0.4%
    replace 1k −10% −10% −19%
    create 1k −6% −8% −21%
    select / select_lots −5% / −6% −65% / −70% −12%
    clear 10k ~ ~ 10,097 → 87 calls

    DOM mutation counts are unchanged, apart from +4 on replace. Behind these numbers:

    • About 14 allocations on every scheduled update.
    • 4,005 journal entries to create 1k rows; most are writes into rows created in that same render, which rollback discards anyway.
    • Every keyed swap or remove copies the whole row chain (about 2n references).

    Why an off-screen rewrite is not the fix:

    • It would not be smaller. Octane already has an off-screen path, DOMStage, used for ViewTransition, and it alone is 5.3 kB gzip. Deferred deletion, effect capture and keyed-list snapshots make up about two thirds of the journal, and any design that can abandon a render needs them.
    • It would not be faster. Compiled templates write bindings inline, so deferring writes means a queue that is the journal turned inside out, at the same cost per write. Reads during render would also need projected DOM views.
    • React's commit is not free either. React pays about 26 kB gzip for its fiber and commit machinery (estimated from react-dom 19.2.7), on top of the in-memory tree Octane deliberately doesn't have.

    Why reachability gating doesn't work: dropping the journal from apps that import no Suspense API is not sound. A thrown promise in any component, or a promise rendered as a child, can suspend a root, and both live on paths every app reaches.

    Sound improvements that change no behavior:

    • Skip journaling writes into rows created in the same render. Entries drop from 4,005 to 1,005 for create 1k, and create is 5.6% faster. As prototyped it adds a call per write (update +2.8%), which the PR call-count gate rejects, so the check needs inlining.
    • Snapshot only what a keyed swap or remove changes.
    • Make clearing a list O(1) instead of one call per row.

    Maintainer call: treating a suspension at an unprotected root, in an app that imports no Suspense API, as an error would save about 4.9 kB on rows plus the runtime above. That is an API divergence. The root journal was previously kept always on by choice.

    Q2: should a size increase require an equal removal?

    The data argues for a different rule.

    Proposed rule:

    1. A fix may raise only the budgets of scenarios that use its feature. A raise anywhere else is a reachability defect to fix in the same PR. The per-feature minimal scenarios already make this checkable.
    2. Allowed raises accumulate and are paid back periodically, for example once per release.

    Q3: can the near-identical fast-path copies be merged?

    Mostly no. Gzip already absorbs the duplication, and the copies exist for measured reasons.

    Family Saved by merging (gzip) Why keep
    hookMemoPublish0..4 0 to −84 in apps, −107 in the ceiling Breaches allocation guard [425] in benchmarks/baselines/ratios.json; 4–11% slower on misses
    memoTake/puTake/nativePuTake 0..4 −47 to −119 in apps Breaches guard [423] (an extra array on every hit)
    evt*u shared tail rows +6, kitchen sink +22 Apps ship one updater, so sharing the tail adds bytes
    componentSlotVoid into componentSlot −1 to −34 +24.8 kB in weather-app, which ships only Void
    componentSlotLite −266 to −283 where used Not a copy: no Block or markers. Update 1.6× slower, and 800 extra comment nodes
    bag0..16 — No general version keeps one shape per arity

    The one worthwhile merge is an exact duplicate: hydrationMarkerMultiplicity appears in both runtime.ts and stream-protocol.ts. The two copies sit more than 32 KB apart, outside gzip's window, so merging saves 140–191 B in hydrating apps at zero runtime cost.

    Q4: should signals have a size budget?

    Yes. Proposed targets are listed after the breakdown.

    Kitchen-sink breakdown:

    Variant Framework gzip vs no signals
    Every React-parity API 98,140 —
    + signal$/derived$ 120,141 +22,001
    + all signals 122,663 +24,523
    + <Hydrate> only, no signals 128,176 +30,036
    + both 151,886 +53,746
    • <Hydrate> costs more than signals do: +30 kB in a createRoot app, because it pulls in the hydration capability.
    • A minimal synchronous signals app (signal$ + derived$) costs +12.6–14.5 kB. Built the same way, @preact/signals-core is 1.63 kB and alien-signals is 1.55 kB.
    • Of the +14.5 kB (estimated):
      • graph core 5.7 kB
      • scope engine 3.4 kB, including about 2.2 kB of seeding, serialization and inspection
      • native-read driver 2.1 kB
      • facades 1.6 kB
      • server seeding 0.9 kB
    • Transition coordination (5.3 kB) already ships only to apps that use both signals and transitions. Splitting it into its own import would change when writes inside a transition become visible, so I'd keep it.

    Proposed targets:

    • ≤0.3 kB of signal code in apps without signals. Today it is 1.8–3.75 kB; see Q5.
    • ≤10 kB for a minimal synchronous signals app. The first step is a lean engine class for facade, document and local scopes, while public createScope keeps the full surface: −2.2 kB, measured by knockout. A module-level declaration path would save about 1.3 kB (estimated).
    • query$ at ≤4 kB marginal, about where it is now.

    A Preact-sized ~1.6 kB isn't realistic with owner scoping, tracked reads during render, server adoption and transitions.

    Q5: the small design calls

    • Signal checks in apps without signals: worth 1.8–3.75 kB, not 540 B.

      • What remains: the native-read driver variable never folds, 23 renderOwner?.signalOwner checks remain, and childSlot activates signal bindings whenever it sees a handle.
      • Gating on RootOptions.signalOwner can't remove the code. A check reached from createRoot keeps the activation in the bundle.
      • What works: two import-free flag modules, in the same pattern as hydration-flag.ts, set only where owners or handles are created.
      • Savings: rows −2,107 B, todo −2,105, kitchen sink −3,752. In run-minimal, hooks-state −1,886 and hydrate-root −1,773. Apps that use signals pay +84 to +222 B.
      • Runtime: pair.mjs call counts are strictly lower on every operation.
    • Restoring server signal values only in apps that use signals: keep the raw seed text. Capture and remove the sidecar unconditionally, and parse it from the signals side on first use, which is what <Hydrate> already does.

      • Savings (knockout, not yet prototyped): −2.1 kB in a minimal hydrating app, or −4.0 kB together with the flags above.
      • The compiler-marker idea is unsound. A per-module compiler can't know at hydrateRoot time that a route loaded later will read signals.
    • Transition undo: this is the swap driver from Q1, and it already ships only where it's used.

    • Focus preservation can't be gated soundly.

      • It is driven by document.activeElement.
      • Libraries set tabindex and contenteditable imperatively.
      • Chrome 130+ makes scroll containers focusable.
      • dangerouslySetInnerHTML and delegatesFocus create focusable elements the compiler can't see.

      Only the ViewTransition-only staged move can be gated (−187 B). That branch is covered only by a browser test.

    • Portal events can be gated soundly, and so can four other features. Each is switched on by the only code that creates its state:

    Gate rows todo kitchen sink (uses it)
    Refs (queueRefAttach/queueRefDetach) −1,044 −995 +98
    Effect hooks (enqueueEffect) −822 −808 +62
    Controlled-input restore (armControlledBase) −539 +12 +29
    Portal events (renderPortalState) −468 −465 +38
    Capture-handler queue (delegateCaptureEvents) −280 −272 +63
    ViewTransition-only focus move −187 −219 +36
    All six −3,392 (−10%) −2,882 +198

    With all six combined, the full octane and octane-prod suites pass: 1,476 files, 42,719 tests.

    Stacked: what lands with no API change

    The six gates, the signal flags and the marker duplicate merge, stacked:

    Scenario Main Stacked Δ
    rows 33,866 28,612 −5,254 (−15.5%)
    todo 36,788 32,005 −4,783 (−13.0%)
    kitchen sink 98,140 94,625 −3,515 (−3.6%)
    kitchen sink + signals + Hydrate 151,886 152,174 +288 (+0.2%)
    ceiling 168,939 169,242 +303 (+0.2%)
    • Runtime: pair.mjs call counts are strictly lower on every operation (−3 or −4), DOM mutation counts are identical, and wall time is within ±3% everywhere.
    • Testing: each piece was tested on its own. The full stack has not been through the full suite as one run.

    Not in the issue: the childSlot cliff

    Any generic renderable hole pulls in the descriptor renderer, about 33 kB gzip at once. That means a bare {count}, {children}, c && <p/>, JSX stored in a variable, or .map in .tsx.

    • A minimal .tsrx app goes from 26,714 to 60,201 B with one bare hole.
    • The same js-framework rows app ships 62,427 B written in .tsx vs 33,866 B in .tsrx.
    • Most real apps pay this. It is probably a large part of the 133 kB in the issue.

    Fixes:

    • A. A real bug: .tsx roots never specialize.
      • The void-root proof compares a byte fingerprint (packages/octane/src/compiler/vite.js:420). Vite's TypeScript transform reprints every .tsx file after Octane, so that check always fails.
      • Comparing the parsed program, ignoring positions, literal spellings and comments, fixes it. It still fails closed when another transform changes the program.
      • A .tsx component that already compiles to a template, rendered by a root: 61,650 → 29,407 B (−32.2 kB). Low risk; tests pass.
    • B. Form actions and Fragment refs as descriptor capabilities.
      • Public createElement, createElementAt and cloneElement install both.
      • Compiled JSX installs them only when its props name action/formAction, a Fragment ref, or a spread.
      • Rows .tsx: 62,427 → 55,991 B (−6.4 kB); any app that reaches childSlot saves the same. Rows .tsrx and todo are unchanged; the kitchen sink pays +60 B and the ceiling +106 B.
      • Runtime: neutral (pair.mjs counters unchanged).
      • Doing the same for signal children and portals would bring the total to about −8.1 kB (knockout).
    • C. Compile a .tsx component's single trailing return <jsx/> to a template (experimental).
      • −25.7 kB once nothing else retains childSlot.
      • On the .tsx rows app: update −44%, select −68%, swap −56%, remove −67%, select_lots −69% wall time; calls on update −63% and on swap −77%.
      • It currently breaks 29 tests: direct component calls, defaultProps observation, HMR and Strong-mode wrappers, and hydration under a value-position ViewTransition. It needs real work.
    • D. Lower {c && <jsx/>} to an @if. Not prototyped; estimated −31 kB where it is the only retainer.

    Baselines from #1668 show .tsx running 3× slower than .tsrx on select and 10.5× on select_lots. The "~1.0 ratio" note in benchmarks/js-framework/README.md is out of date.

    Open for a maintainer

    1. Q1: should a suspension at an unprotected root, in an app that imports no Suspense API, become an error? That would save about 4.9 kB on rows plus the runtime gains above. The no-divergence alternatives (skipping fresh rows, change-only snapshots, O(1) clear) need no decision.
    2. Q2: the scoped raise rule above, instead of an equal-removal rule.

    Ready to become PRs, none of which changes the API:

    • fix A (.tsx root proof)
    • the six gates
    • the two signal flag modules
    • the marker duplicate merge
    • fix B (descriptor capabilities)

    Measured but not yet prototyped:

    • the raw-seed restore
    • the lean signals engine
    • the journal improvements (skipping fresh rows needs its check inlined first)
  2. o-alexandrov commented on Oct 12, 2026

    @o-alexandrov
    ContributorAuthor

    @trueadm I think you should have closed it "Closed as not planned", because the bundle size is still the same issue when using the whole client API.
    The whole client API actually grew by 0.1% in current state of main branch comparing to the date when this issue was opened

  3. trueadm commented on Oct 12, 2026

    @trueadm
    Collaborator

    If you want to try and get the bundle size down more please go for it. I did a bunch of savings already and in most apps that don't use everything the gzip size is better than React when using Vite in my testing. I've been focusing on performance and correctness too so we can't regress there. Maybe if you could add another example app to the apps that demonstrates your issue more. It's not a fair apples to apples to compare Octane with React when using signals. Adding alien-signals to React is more fair

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions