Repository navigation
perf(signals): allocate optional scope capabilities on first use - #1288
Merged
Merged
Conversation
A ScopeImpl eagerly allocated eleven collections for requests, query definitions, resources, streamed selections/results/failures/waiters, derived bindings, adoption leases, seeds and trace events, and kept the streamed-result methods on the class, so every scope paid for them. Streamed selections now live in a ScopeStreams capability that only the existing stream ingress functions create. The other optional collections allocate on first use. Lifecycle order is unchanged: producers stop before channels clear, and adoption leases release before the graph retires. A useSignal$ hook scope retains 785 instead of 2,649 bytes, stream-free signal entries are about 700 bytes smaller after gzip, and a bundle guard keeps the capability out of those entries. Refs #1284 (proposal 1). Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm
marked this pull request as ready for review
September 23, 2026 20:02
12 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ScopeImpl. These now allocate only when the feature is used.ScopeStreamscapability that only the existing stream ingress functions create. Stream-free consumers stop retaining that code.useSignal$hook scope retains 785 instead of 2,649 bytes (−70%), and stream-free signals entries are ~700 B gzip smaller (−3.7% to −5%).Refs #1284 (proposal 1; triage in this comment).
Changes
signals/scope-streams.ts(new):ScopeStreamsholds the selection, result, failure and waiter maps and their operations. They were moved fromScopeImplunchanged, apart from the owner prefix. The capability participates explicitly in the owner lifecycle:suspend: expire unfinished channels;resume: rebind refreshed resources;flush: deliver results received before the declaration ran;clear: on dispose.signals/engine.ts:requests,queryDefinitions,resources,derivedBindings,frames,seedEntriesandeventsstart asundefinedand allocate on first write. They are initialized in the class body, so the object shape stays stable.bindScopeStreamedSelectionis the only function that creates the capability. Accept and fail keep their liveness check and returnfalsewhen nothing was bound.signals/requests.tsandsignals/facade.tsuse the lazy maps and the capability. There are no public API or type changes.Measurements
Baseline is
main @ afc0bee88, archived. The candidate is this branch. The repo's ownrun-bundles.mjscomparison reports every boundary check and export smoke test as passed.run-bundles.mjs)engine(createScope,query)native-client(useSignal$)native-server(useSignal$)compiled-plain-signalsstreamed-signals-bootstrapstreamed-signal-results-bootstrapordinary-client,ordinary-server,binding-*Stream-using entries are +11 raw bytes, but gzip +213/+222. Moving the stream code out of
ScopeImplcosts some compression locality; no work was added. That is the tradeoff against −670 to −740 B for every stream-free consumer.Retained heap: Node 24, 50,000 scopes kept alive, post-GC
heapUseddelta, deterministic across runs:useSignal$shape: local scope + onesignal$signal$, onederived$, one subscriptionTiming (one process, both builds alternating, 10 samples after 2 warmups):
Validation
New test in
signals-streams.test.ts. On a scope with live ordinary signals, it activates a streamed selection (results buffered before the declaration), then a resource, an adoption lease and tracing. It checks that:dispose()is idempotent and clears every capability;The test passes on both the baseline and the candidate: it pins preserved behavior.
Mutation checks on the new code. Caught:
Four mutants survive, all equivalent through the public API:
selectionReadyflushes the same results;New bundle guard in
bundle-boundaries.mjs/.test.mjs(runs inci:workflow:test).engine,native-client,native-serverandcompiled-plain-signalsmust emit zero bytes fromscope-streams.ts. This covers a synthetic negative case and the real engine and native-client closures. Making every scope create the capability eagerly fails it.All four signal projects (node, dev, prod, strong): 814 passed across 51 files.
Stream, hydration and behavior suites in dev and prod (streamed cancellation, automatic signal streams, document lifecycle, stream receiver/delivery, streamed query hydrate, signal instance/owner identity, early handoff/intent, fetched regions/history, universal transport, behavior roots): 852 passed across 30 files.
benchmarks/scoped-signalsCI tests (bundle boundaries, workloads, async retainers, deferred/split boundaries): 57 passed.pnpm typecheck:fileson changed files,tsgo -p packages/octane/typetests/signals-engine-node,pnpm format:files:check,pnpm changeset:checkandpnpm sync.pnpm test,pnpm typecheckandpnpm format:check(repo-wide): left to CI.Risk / follow-ups
observersandnodesstay eager. Nearly every scope uses them, andobserversis on the subscription path.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Touches core scope lifecycle (suspend/resume/dispose) and streamed-result delivery order; behavior is heavily tested but mistakes in lazy init could affect queries and hydration.
Overview
Signal scopes no longer eagerly create maps and bookkeeping for requests, resources, derived bindings, adoption frames, trace events, or streamed results. Those structures are allocated on first use, which cuts retained memory for simple hook scopes (e.g.
useSignal$) and shrinks stream-free bundles by roughly 700 B gzip.Streamed selections are refactored into a new
ScopeStreamscapability (scope-streams.ts). Stream ingress (bindScopeStreamedSelection/attachStreamedSignalResult) is the only path that creates it; accept/fail stay no-ops until a selection is bound. Suspend, resume, flush, and dispose behavior is preserved on the capability.Bundle guards assert that
engine,native-client,native-server, andcompiled-plain-signalsemit zero bytes fromscope-streams.tswhen they never bind streams. A new integration test verifies late activation of streams, resources, adoption, and tracing on a scope that already has ordinary signals.Reviewed by Cursor Bugbot for commit 0799472. Bugbot is set up for automated code reviews on this repo. Configure here.