Skip to content

Commit 922fe1c

Browse files
ryansolidClaude via Cursorcursoragent
committed
size(web): trim the element-claim registry (#3923)
The router scenarios' growth was all registry and write-site code (the hand-written pages tree-shake `claimElement`, `claimElementTree` and `spread`). Drop the cached union `Set` and its namespaced-form expansion: the write sites scan the per-consumer attribute lists directly and look a qualified name up by its local part (`xlink:href` → `href`, no allocation for a plain name). Inline the default list; optional-chain the options. page: base + router / live + router: +252 → +125 B minified vs next. Every-page scenarios stay byte-identical. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 8951960 commit 922fe1c

1 file changed

Lines changed: 17 additions & 29 deletions

File tree

‎packages/web/src/client.ts‎

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,10 @@ export function setProperty(node, name, value) {
657657
// (compiled output captures it at creation), since the effect's callback
658658
// runs ownerless when the mount lands from a held flush.
659659
let claimHandlers = null;
660-
// Per registered handler, the attribute names whose writes re-claim; and
661-
// their union, what the write sites consult. Rebuilt on (un)registration.
662-
let claimAttributeSets = null;
663-
let claimedAttributes = null;
664-
const DEFAULT_CLAIM_ATTRIBUTES = ["href", "action"];
660+
// Per registered handler, the attribute names whose writes re-claim — the
661+
// write sites consult their union (a handful of names across one or two
662+
// consumers; scanned, not indexed).
663+
const claimAttributeSets = [];
665664

666665
// The live handler list is mirrored onto a registered symbol so the frame
667666
// runtime — deliberately importless in both directions, like the FRAME
@@ -708,33 +707,16 @@ export function registerElementClaim(
708707
*/
709708
export function registerElementClaim(handler, options) {
710709
(claimHandlers || (claimHandlers = globalThis[CLAIM_SEAM] = [])).push(handler);
711-
(claimAttributeSets || (claimAttributeSets = [])).push(
712-
(options && options.attributes) || DEFAULT_CLAIM_ATTRIBUTES
713-
);
714-
rebuildClaimedAttributes();
710+
claimAttributeSets.push(options?.attributes || ["href", "action"]);
715711
return () => {
716712
const index = claimHandlers.indexOf(handler);
717713
if (index > -1) {
718714
claimHandlers.splice(index, 1);
719715
claimAttributeSets.splice(index, 1);
720-
rebuildClaimedAttributes();
721716
}
722717
};
723718
}
724719

725-
// The union of the declared sets, keyed the way the write sites see names:
726-
// the plain name (`setAttribute`, `setProperty`, the spread's property path
727-
// with `prop:` stripped) and its qualified namespaced forms (`setAttributeNS`
728-
// receives `xlink:href`), expanded here so the write sites do no parsing.
729-
function rebuildClaimedAttributes() {
730-
claimedAttributes = new Set();
731-
for (let i = 0; i < claimAttributeSets.length; i++)
732-
for (const name of claimAttributeSets[i]) {
733-
claimedAttributes.add(name);
734-
for (const prefix in Namespaces) claimedAttributes.add(prefix + ":" + name);
735-
}
736-
}
737-
738720
/** Fire every handler in `handlers` on `node`. */
739721
function runClaimHandlers(handlers, node) {
740722
for (let i = 0; i < handlers.length; i++) handlers[i](node);
@@ -744,13 +726,19 @@ function runClaimHandlers(handlers, node) {
744726
// element has had its mount claim (`_$claimed`), a write to a declared
745727
// attribute re-claims it. `name` is the name as the write site has it —
746728
// plain, `prop:` stripped on the property path, qualified on the namespaced
747-
// path (the set carries both forms). The registry check comes first on
748-
// purpose: `claimHandlers` is only ever
749-
// assigned by `registerElementClaim`, so an app without a consumer (the
750-
// registration tree-shaken) has the bundler fold this — and `claimElement`
751-
// — to nothing; the dormant path costs no bytes and no reads.
729+
// path — and the declared names are plain, so a qualified name is looked up
730+
// by its local part (`xlink:href` re-claims as `href`; a plain name slices
731+
// from 0, which hands back the same string). The registry check comes first
732+
// on purpose: `claimHandlers` is only ever assigned by `registerElementClaim`,
733+
// so an app without a consumer (the registration tree-shaken) has the bundler
734+
// fold this — and `claimElement` — to nothing; the dormant path costs no
735+
// bytes and no reads.
752736
function reclaimAttribute(node, name) {
753-
if (claimHandlers !== null && node._$claimed && claimedAttributes.has(name))
737+
if (
738+
claimHandlers !== null &&
739+
node._$claimed &&
740+
claimAttributeSets.some(set => set.includes(name.slice(name.indexOf(":") + 1)))
741+
)
754742
runClaimHandlers(claimHandlers, node);
755743
}
756744

0 commit comments

Comments
 (0)