Skip to content

perf(core): hoist a fully static fragment as one constant - #1372

Merged
jonkwheeler merged 3 commits into
tsrx-org:mainfrom
jonkwheeler:w14/1352-hoist-static-subtree
Nov 18, 2026
Merged

jonkwheeler merged 3 commits into
tsrx-org:mainfrom
jonkwheeler:w14/1352-hoist-static-subtree

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

React, Preact and Hono hoist static JSX element by element (#1279). A fragment whose elements were all hoisted was still built again, around their constants, on every render. export function B() { return <><div> 1 </div>{<><div> 2 </div><div> 3 </div></>}</>; } gave three constants. It now gives const B__static1 = <><div>{' '}1{' '}</div>{<><div>{' '}2{' '}</div><div>{' '}3{' '}</div></>}</>; and return B__static1;. mizulu's fragment of ten static <div>s gives one constant instead of ten.

#1279's hoisting is unchanged. Once a component's output is lowered, a fragment becomes one static that takes in the statics it held when two things are true:

  • it holds at least one static;
  • everything else in it is text, a literal, or a fragment of these.

The new static takes the name and place of the first static it took in. A static's reference is known by the declaration it points to, not by its name.

  • Text: the static is the output as lowered, so its text prints exactly as it did in place. React server markup, text nodes and hydration are unchanged.
  • Elements and components: it takes in no element that perf(core): hoist static JSX that an expression container renders #1279 left in place, so every element is built when it was before. No component inside the fragment renders less often, a literal value/checked is still set again on re-render, and nothing new is evaluated at module load.
  • Memoized consumers: a memoized component that receives the fragment as a prop or children can skip a render. A single hoisted element has allowed this since perf(core): hoist static JSX that an expression container renders #1279.
  • Keys: by the time this runs, a keyed @for row is a keyed element, and it keeps its key.

Checked against 34a7ec8 with two rounds of differential matrices (React SSR, client and hydration; Preact; Hono SSR) and all tracked .tsrx files. Solid, Vue and Hono DOM are byte-identical, and editor mappings cover the same ranges. Two differences remain:

  • In an unkeyed @for whose row fragment folds, React dev reports the missing key once, as it does for a hoisted element body.
  • In Preact, a component that reads an external store written during render without notifying picks up the new value one render sooner.

Known, and the same as since #1279:

  • calling a component while its module is still evaluating, before its statics exist, throws;
  • the static-name check doesn't decode escaped identifiers.

Tests

  • Shared compile cases on five targets. They include:
  • A React renderToString suite pinned to the in-place markup for 13 body shapes, including a fragment that is a body's only output.
  • React and Preact runtime tests:
    • a component in a fragment renders again with its parent and reads updated module state;
    • an input's literal value is set again.
  • npx -y [email protected] test: 15,307 passed, 102 skipped. typecheck, format:check and changeset:check pass.
  • Mutation: reverting the source fails 47 tests, and the two earlier versions of this change fail 34 and 20.
  • Review:
    • Opus and Codex (gpt-6.1-sol) reviewed two earlier versions independently. Their findings are fixed here: edge whitespace, components skipping re-renders, absorbed form controls, eager evaluation, and name collisions.
    • A third Codex pass on this version found no defects.

Fixes #1352

🤖 Generated with Claude Code

https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46


Note

Medium Risk
Touches JSX codegen for React/Preact/Hono rendering, but behavior is gated to safe static-only fragments and covered by broad compile/runtime/mapping tests.

Overview
Adds fold_static_fragments in the React/Preact/Hono JSX lowering path so a fragment made only of already-hoisted elements (plus text, static literals, or nested static fragments) collapses into one module-level __static constant instead of re-wrapping separate statics on every render.

Static hoists now return declaration-linked references (static_declaration on identifiers) so folding can merge single-use statics and rewrite the static list without breaking identity or memoization semantics. Solid, Vue, and Hono DOM output is unchanged.

Tests and fixtures are updated across compile, content-mapper, and runtime harnesses (including new whole-fragment markup and render-count cases) to lock in correct markup, keys, component re-render behavior, and editor diagnostics when fragments are hoisted as a unit.

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

jonkwheeler and others added 2 commits November 17, 2026 13:46
…ainers as in children lists

The spaces and tabs at the edges of an element's children print as
`{' '}` (tsrx-org#1277), but JSX inside a `{…}` container kept its text as
written on React, Preact, Hono and Solid, so
`<><div> 123 </div>{<div> 123 </div>}</>` printed two forms (tsrx-org#1351).

- React, Preact, Hono: a static hoisted out of a container (tsrx-org#1279)
  leaves the walk that applies the rule. lower_static_script_bodies,
  renamed lower_static_jsx, already redoes the walk's `<script>`
  lowering for a static; it now also wraps each children list in the
  static. This covers a container inside a bare static element too.
- Solid: create_element_children returns wrapEdgeWhitespace(...), so
  JSX in a container, an attribute value, a callback, a `const`, a
  module-level declaration or nested in a control-flow body gets the
  rule. Solid's compiler collapses a whitespace run in JSX text but
  keeps a `{'  '}` literal, so `{<b>  1  </b>}` now renders "  1  "
  like a children list instead of " 1 ".
- Vue already printed both forms the same; its output is unchanged.

In those places an element's text with edge whitespace now gets three
children instead of one string, as in a children list, so React's
existing <textarea>/<title> problem with that shape reaches them too.

wrapEdgeWhitespace returns its input when it wraps nothing, and gives
the trimmed text the location of what is left of it: with the whole
text's location, a `@` after a space (`<div> @</div>`) mapped one
character early and got no directive completion, in a children list
and now in a container.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
React, Preact and Hono hoisted static JSX element by element (tsrx-org#1279),
so a fragment whose elements were all hoisted was still built again
around their constants on every render.
`<><div> 1 </div>{<><div> 2 </div><div> 3 </div></>}</>` gave three
constants; it now gives one `const B__static1 = <>…</>;` and
`return B__static1;` (tsrx-org#1352).

Hoisting itself is unchanged. Once a component's output is lowered, each
fragment in it that holds at least one static, and otherwise only text,
literals and fragments of these, is replaced by one static that takes in
the statics it held, with the name and place of the first of them. A
static's reference is known by the declaration it points to, not by its
name, and a static is taken in only through its one reference.

Since it reads the output as lowered, the static prints each text as the
fragment in place did, so server markup and text nodes stay the same
wherever the walk wraps or leaves edge whitespace. It takes in no element
tsrx-org#1279 left in place, so every element is built when it was before, and
no component inside the fragment renders less often. A memoized
component that receives the fragment as a prop or as children can skip
a render, as it can for a single hoisted element since tsrx-org#1279. A keyed
`@for` row is a keyed element by then, so it keeps its key and stays in
place.

Solid, Vue and Hono DOM don't hoist, and their output is byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
@jonkwheeler
jonkwheeler marked this pull request as ready for review November 18, 2026 21:20
tsrx-org#1367 is now on main as bd00fda, and tsrx-org#1369 as 4500db9. This merge takes
main in with tsrx-org#1367's commit as the base, so the tree is main plus this
branch's own commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
@jonkwheeler
jonkwheeler merged commit 2553f96 into tsrx-org:main Nov 18, 2026
10 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: react, preact, hono: hoist a fully static fragment or subtree as one constant

1 participant