Skip to content

perf(signals): install the public Scope surface only from createScope - #1851

Merged
domgan-oai merged 15 commits into
mainfrom
perf/lighter-signal-scope
Oct 7, 2026
Merged

domgan-oai merged 15 commits into
mainfrom
perf/lighter-signal-scope

Conversation

@trueadm

@trueadm trueadm commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This is part of the size work tracked in #1801. Apps that use signals are 0.49 to 0.65 kB smaller gzipped. Apps without signals are byte-identical. The Scope API is unchanged. One public type changes: SignalOwnerEnvironment carries owners as SignalOwnerIdentity (see below), so the changeset is minor.

The problem. The runtime creates owner scopes for compiled signal$, derived$, query$ and useSignal$ declarations. Most apps never hold one. But every app with signals still shipped the whole public Scope implementation as methods on ScopeImpl:

  • serialization and inspection;
  • trace recording;
  • the imperative derived$, get, set, isPending, batch and action methods.

Bundlers never remove class methods, so even an app that never calls createScope paid for all of it.

What changed.

ScopeImpl keeps one class shape, so the hot read and write paths are unchanged.

  • Opt-in public methods. installPublicScope puts serialize, inspect, derived$, get, set, isPending, batch and action on the prototype. Runtime-created scopes come from createOwnerScope, createSeededOwnerScope and createLocalScope.
  • Every public route to a scope installs them first. Runtime-created scopes do reach application code: derived$ and query$ run authored callbacks under them.
    • createScope and the octane/signals exports of currentSignalOwner and installSignalOwnerEnvironment (signals/public-owner.ts) call installPublicScope before an owner can escape. Only apps that use them pay.
    • The runtime keeps the engine-free versions in owner-context.ts.
  • Host carriers see identities. octane/server exports installSignalOwnerEnvironment engine-free, and app-core installs it for every SSR request, so it cannot install the methods.
    • SignalOwnerEnvironment now carries owners as SignalOwnerIdentity. A carrier stores and returns them but cannot narrow them to Scope.
    • SignalOwnerIdentity is a member of SignalOwner, so carriers that store SignalOwner values (app-core, the tests and benchmarks here) compile unchanged.
  • Trace recording. Only a createScope({ debug }) scope installs it. trace() keeps its existing traceLimit check.
  • serializeRead and beginAdoption stay methods. Server native reads serialize internal owner scopes, and hydration adopts seeds into them.
    • Both protocols (native-read-server.ts, native-read-seeds.ts) load in apps without signals, so neither can import the engine.
    • An engine import from native-read-seeds.ts grew the React-parity kitchen sink by 8.5 kB gzip.
    • In the first push, the server driver imported the engine to install the serializer. That loaded the engine on every server render, and Node's strip-only loader rejected it (register-hook.test.ts). The second commit puts the method back, so graph.ts and native-read-server.ts match main.
  • signal$ stays a method. useSignal$ calls it on its local scope. The facade's isScope guard also identifies a scope by it.
  • Internal type. A new OwnerScope type omits the installed members. Internal code that calls one of them on a runtime-created scope now fails to typecheck, instead of failing only in apps that never call createScope.

Measurements

Framework bytes, Vite + oxc production builds, on aaac26c and then on this branch:

App gzip before gzip after gzip change raw change
minimal signal$ (createRoot) 39,122 38,620 −502 −2,024
useSignal$ only 36,177 35,525 −652 −2,600
query$ 55,405 54,861 −544 −2,025
hydrating signal$ 58,441 57,952 −489 −2,026
hydrating query$ 75,597 75,084 −513 −2,025
kitchen sink with signals (SPA) 152,474 151,967 −507 −2,030
kitchen sink with signals (hydrate) 154,044 153,511 −533 −2,030
rows, todo, React-parity kitchen sink, ceiling — — 0 0

Validation

  • pnpm format:files:check on the changed files (repo-wide pnpm format:check not run)
  • pnpm typecheck (not run repo-wide). octane-tsc over packages/octane/tsconfig.json, its typetests and typetests/signals-engine-node configs, and packages/app-core/tsconfig.typecheck.json is clean, as is pnpm typecheck:files.
  • pnpm test (not run locally; left to CI)
  • targeted tests:
    • octane-signals-node: 273/273
    • octane-signals, octane-signals-prod and octane-signals-strong: 327/327 each
    • octane-signals-profile: 3/3
    • native-read, SSR-seed, query, action, owner-retirement and devtools files: 295/295 in octane and 320/320 in octane-prod
    • These ran on the code before the OwnerScope type change. That change touches only types, and the production bundles match byte for byte.
    • After the second commit I reran octane-signals-node (273/273), register-hook.test.ts (3/3), the SSR-seed, streamed-cancellation and native-read files (16/16), and the regression test.
  • New regression test in benchmarks/scoped-signals/bundle-boundaries.test.mjs: compiled signal declarations omit the public Scope surface until createScope.
    • A compiled-declaration program must not ship the public-only implementations. The check uses the inspection field names, which survive minification.
    • The control adds one createScope. It checks derived$, get, set, action, batch, isPending, serialize, inspect and trace recording, and that owner scopes still work before and after the methods are installed.
    • It fails on origin/main source with "A program without createScope retained the public Scope surface."
  • Owner-route regressions, added for the review:
    • bundle-boundaries.test.mjs builds each route in its own program without createScope. The review's currentSignalOwner probe returns 'probe', and a carrier that calls the owner it carries sees inspect().
    • Each route fails on its own without the fix, with inspect is not a function.
    • typetests/signal-owner-environment.test-d.ts pins the identity-typed carrier. Its @ts-expect-error fails against main's SignalOwner signature.
    • Carrier and owner tests: event-signal-owner, stable-event-authority, hydration/signal-instance-identity and hydration/document-signal-owner (249/249), app-core request-signal-lifetime (3/3), and published-errors.test.mjs against a local dist build (3/3).
  • node benchmarks/bundle-size/run-minimal.mjs --budgets --ratchet and node benchmarks/bundle-size/run.mjs --budgets --ratchet octane-tsrx octane-jsx with BUDGET_BASE=origin/main

Risk / follow-ups

  • Measurement scope. The runtime benchmark ran on the first commit. The second only puts back main's serializeRead method.
  • The first createScope call changes ScopeImpl.prototype. V8 then resets the inline caches for scope method lookups once. If an app calls createScope only after it has warmed up (for example, from a lazily loaded route), that costs one re-warm.
  • getSignalScope and OptimisticDescriptor now use the internal OwnerScope type. The public routes that return an owner install the Scope methods first.
  • Behavior change for code that uses internals. Untyped code that reaches through a handle's internal owner and calls inspect() (or another installed method) now throws TypeError unless the program has called createScope. native-deferred-local-hook-diagnostics.test.ts did this in its useSignal$ cases. The third commit makes it read the leases and node count from the scope directly. No other test, package, example or site code does this.

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. Autofix is disabled.


Note

Medium Risk
Changes signal runtime initialization and owner typing; apps that relied on calling public Scope methods on runtime-created owners without createScope will throw at runtime.

Overview
Defers shipping the full public Scope API until an app actually needs it. Runtime owner scopes for compiled signal$ / derived$ / query$ / useSignal$ no longer always carry serialize, inspect, imperative get/set/batch/action, and related code; installPublicScope attaches those methods on ScopeImpl.prototype only when createScope runs or when currentSignalOwner / installSignalOwnerEnvironment are imported from octane/signals (via new public-owner.ts). Internal code uses OwnerScope and createOwnerScope / createSeededOwnerScope so missing the public surface is a type error; debug trace recording installs only for createScope({ debug }).

SignalOwnerEnvironment now passes SignalOwnerIdentity so SSR/host carriers (e.g. octane/server) cannot narrow owners to Scope and call inspect()—a minor public typing change. Bundle budgets for hydrate-frame drop ~0.5 kB gzip; a bundle-boundaries regression test asserts compiled-declaration bundles omit inspection-related code until createScope.

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

Owner scopes that the runtime creates for compiled signal declarations never
reach application code, yet every signal application shipped the whole public
Scope implementation: serialization, inspection, trace recording and the
imperative derived$/get/set/isPending/batch/action methods.

ScopeImpl keeps one class shape. createScope installs the public-only methods
on its prototype the first time it runs, and only a debug scope installs trace
recording. The server's native-read driver installs the read serializer, so
clients never ship it. beginAdoption stays a method: hydration resolves it
through the owner, and importing the engine from the hydration path would pull
the engine into applications without signals.

An internal OwnerScope type omits the installed members, so internal code that
reaches for one fails to typecheck.

Signal applications are 871 to 1,118 B smaller gzipped; applications without
signals are byte-identical.

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 7, 2026
@github-actions
github-actions Bot marked this pull request as draft October 7, 2026 17:15
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No budget breaches, no added work, and no timing changes beyond ±3%.

Compares 43b60d94f, the merge commit's first parent with 6982fbc79 on the same runner. A byte value over its committed budget or any increase in a js-framework work counter fails this check. 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.975 3.817 −1.2% −9.7% … +11.3% ⚪ within noise
octane-tsrx replace 10.842 10.542 +1.3% −4.9% … +7.2% ⚪ within noise
octane-tsrx add 2.892 3.225 +3.7% −1.4% … +8.8% ⚪ within noise
octane-tsrx update 0.164 0.165 −1.6% −5.0% … +6.9% ⚪ within noise
octane-tsrx select 0.006 0.006 +0.2% −5.8% … +7.3% ⚪ within noise
octane-tsrx swap 0.036 0.035 −0.9% −9.9% … +4.9% ⚪ within noise
octane-tsrx remove 0.020 0.022 +1.9% −9.6% … +10.5% ⚪ within noise
octane-tsrx runlots 28.892 30.100 +2.3% −3.5% … +7.9% ⚪ within noise
octane-tsrx select_lots 0.008 0.008 +3.7% −1.3% … +9.0% ⚪ within noise
octane-tsrx clear 83.283 87.092 +6.7% −4.0% … +19.3% ⚪ within noise
octane-jsx run 5.833 5.183 −2.7% −15.2% … +9.9% ⚪ within noise
octane-jsx replace 14.642 15.783 +3.5% −7.1% … +7.4% ⚪ within noise
octane-jsx add 4.975 4.533 −9.7% −18.5% … +4.6% ⚪ within noise
octane-jsx update 0.399 0.390 −0.9% −9.5% … +1.8% ⚪ within noise
octane-jsx select 0.186 0.189 −2.2% −6.9% … +9.0% ⚪ within noise
octane-jsx swap 0.323 0.313 −3.9% −10.5% … +1.1% ⚪ within noise
octane-jsx remove 0.254 0.267 +5.2% +1.3% … +12.0% ⚪ within noise
octane-jsx runlots 35.783 34.400 −5.2% −8.0% … −1.4% ⚪ within noise
octane-jsx select_lots 1.794 1.868 +4.6% −3.4% … +10.4% ⚪ within noise
octane-jsx clear 68.600 69.217 +4.3% ±0.0% … +16.3% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 60 measured values.

Public-import reachability bundles (bytes)

target metric base head Δ
hydrate-frame raw 366,329 364,299 −2,030 (−0.6%) 🟢
hydrate-frame gzip 113,382 112,873 −509 (−0.4%) 🟢
hydrate-frame brotli 96,884 96,370 −514 (−0.5%) 🟢

108 other measured values are unchanged.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 7, 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 41eeeea Commit Preview URL

Branch Preview URL
Oct 07 2026, 06:10 PM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 7, 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 Updated (UTC)
🔵 In progress
View logs
octane-website b684fb9 Oct 07 2026, 08:08 PM

…path

The server's native-read driver imported the engine to install the read
serializer. That loaded the engine, and its top-level batch-hook registration,
on every server render, including applications without signals, and Node's
strip-only TypeScript loader rejects the engine's parameter property
(register-hook direct server scripts failed).

serializeRead is a ScopeImpl method again, reached through the owner as on
main, so the server path and graph.ts match main. The server-render budget
returns to main's value. Signal applications are now 489 to 652 B smaller
gzipped; applications without signals stay byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm marked this pull request as ready for review October 7, 2026 17:36
native-deferred-local-hook-diagnostics reached through a useSignal$ handle's
internal owner and called inspect(). Runtime-created scopes no longer carry the
public Scope surface unless the program calls createScope, and these cases
never do. Read the adoption leases and node count that inspect() reported
from the scope directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@domgan-oai domgan-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation: do not merge this head yet. There is one reproducible public API regression (inline). The bundle-size design is otherwise contained: retaining native-read serialization/adoption on the common scope and splitting cold public methods is defensible, and I found no separate internal SSR/hydration defect.

Validation: all 32 reported current-head checks are green (including CI tests/parity/typecheck and PR bench); git diff --check passes. I ran the same renderer-free probe on the exact base 83eaaf4 and head 41eeeea, using Node TypeScript transform and the lock-matched alien-signals 3.2.0: base returned probe, head threw TypeError. This is a direct source/API reproduction, not a production bundler measurement. I could not independently rerun the full workspace/bundle suite: the isolated checkout has no dependencies and its pinned pnpm was unavailable from the configured registry. The exact quoted size savings remain the PR author's measurements.

Please fix the exposed owner contract and cover the no-createScope case, then rerun CI. GitHub also currently reports BEHIND / REVIEW_REQUIRED, so repository merge requirements still apply.

Comment thread packages/octane/src/signals/facade.ts Resolved
trueadm and others added 5 commits October 7, 2026 20:52
Runtime-created owner scopes reach application code: derived$ and query$ run
authored callbacks under them, so the public currentSignalOwner() returns one,
and a host SignalOwnerEnvironment carries them. Both are typed SignalOwner,
which narrows to Scope, but without an earlier createScope the scope lacked
inspect(), get() and the other installed methods.

currentSignalOwner and installSignalOwnerEnvironment exported from
octane/signals now install the public Scope methods before an owner can
escape; the runtime keeps owner-context's engine-free versions.
octane/server's carrier must stay engine-free, so SignalOwnerEnvironment now
carries owners as SignalOwnerIdentity values: a carrier stores and returns
them but cannot narrow them to Scope. Carriers that store SignalOwner values
compile unchanged.

bundle-boundaries builds each route in its own program without createScope
(the review's currentSignalOwner probe and a carrier that calls the owner it
carries); both fail with "inspect is not a function" without this change. A
type test pins the identity-typed carrier.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nal scope

hydrate-frame (added on main by #1855) hydrates signal reads, so it ships
the signal engine. Against 188d60b's sources it is 2,030 B raw, 488 B gzip
and 437 B brotli smaller with this branch's engine, and no other minimal
scenario moves. The new budget is main's budget minus that delta, because
this machine measures main's own hydrate-frame 4 B over its budget.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm

trueadm commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator Author

Following up on the benchmark report:

  • 🔴 server-render brotli +8 B (first report, 320cc03). This came from the first commit's native-read serializer slot in graph.ts. 7f6ed8c reverted it, so graph.ts and native-read-server.ts match main. The reports for 7f6ed8c and 41eeeea show no byte changes across 60 application and 108 reachability values.
  • 🟡 octane-jsx swap +12% (17:49 report). The rows bundle is byte-identical in this PR, and the next report put every operation within ±3%, so this was noise.
  • Current head. I merged main up to 0f3abca, which picked up perf(octane): keep the transition and hidden-Activity graphs out of Suspense-only bundles #1855's new hydrate-frame scenario, and compared all 37 minimal scenarios against 188d60b's sources.
    • Only hydrate-frame moves: −2,030 B raw, −488 B gzip, −437 B brotli. 3333d96 lowers its budget by exactly that delta.
    • The 6 application budgets hold, and both ratchets pass.

🤖 Posted by Claude Code

trueadm and others added 6 commits October 7, 2026 21:55
With #1857 (in-place Block signal owners) and #1862 (unchanged adoption reads)
on main, this branch's hydrate-frame measures 364,278 raw, 112,866 gzip and
96,483 brotli. The budget is that measurement plus headroom: 364,310 /
112,898 / 96,616. That is below main's 366,064 / 113,343 / 97,053, so it lowers
rather than raises.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@domgan-oai
domgan-oai merged commit 37a1c14 into main Oct 7, 2026
26 of 27 checks passed
@domgan-oai
domgan-oai deleted the perf/lighter-signal-scope branch October 7, 2026 22:13
trueadm added a commit that referenced this pull request Oct 8, 2026
…he trace-retention guard (#1916)

#1907 removed every committed budget gate but missed one. The chromium
browser lane runs view-transitions/effect-cleanup.mjs on every PR, and it
failed when total or idle-extra function calls exceeded
effect-cleanup-budget.json (measured + 32 calls). Delete that file and both
ceiling checks. The run still requires zero stageTeardown entries, the
shared lifecycle snapshots, completed native primers and unchanged
provenance hashes, and it still reports total_calls and idle_extra_calls.

#1907 also deleted the scoped-signals-trace guard (wrapped-max-budget over
unfilled-max-budget, trace_event, 1.5x) only because its reference name
ends in -budget. That name refers to the 10,000-event trace retention
limit; the guard compares two configurations timed in the same run.
Restore it verbatim.

run-trace.mjs has not produced numbers since #1851, because it built
ScopeImpl with the old (key, options) constructor and #1851 installs trace
recording and inspect() only through createScope. Build scopes through
createScope so the suite runs and the restored guard is evaluated again.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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