Skip to content

perf(ssr): skip the head scan for fragment renders - #948

Merged
trueadm merged 3 commits into
mainfrom
perf/ssr-html-carrier-cost
Sep 3, 2026
Merged

trueadm merged 3 commits into
mainfrom
perf/ssr-html-carrier-cost

Conversation

@trueadm

@trueadm trueadm commented Sep 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Recover the server-rendering throughput lost in fix: address React behavioral audit defects #943 without touching its escaping fix. Measurement attributes the regression to spliceHead, not to the ServerHtml carrier.
  • spliceHead now returns a non-document body unchanged after the existing document prefix check, instead of scanning the entire response for </head> on every render whose hoisted head is empty. A document root still gains its implicit <head>.
  • Complements perf(ssr): skip redundant loop HTML carriers #945, which stops wrapping @for item helpers in the carrier; that change is not repeated here.

Why

  • The audit recorded 1.761 → 1.914 ms (compiled) and 3.656 → 3.901 ms (descriptors) on benchmarks/ssr-throughput CONFIGS=deopt-page and attributed the cost to the carrier.
  • Alternating runs of bundles built from the base, fix: address React behavioral audit defects #943, and fix: address React behavioral audit defects #943 with the carrier removed put all three within run-to-run noise on both deopt-page fixtures and on escape-heavy. A CPU profile of the same bundles shows spliceHead rising from 0% to about 9–11% of self time with every other function flat. Escape-heavy (996 KB body) confirms it: 3.24 → 3.68 ms with the carrier present or absent.
  • fix: address React behavioral audit defects #943 moved the head === '' early exit behind a full body.indexOf('</head>') so a document root gains an implicit <head>; only documents need that path, and isDocumentRoot decides it from the first bytes.
  • An identity-register design that removed every per-component carrier (publish the finished string, claim it by identity at the runtime call site, spill unclaimed direct-call results per pass) was prototyped and passed every suite, but was not measurably faster and adds a value-equality nuance at a security boundary, so it was set aside; the audit document records this.

Changes

  • packages/octane/src/runtime.server.ts: spliceHead early exit for non-document bodies with an empty head; document behavior unchanged.
  • packages/octane/tests/server-renderable-contract.test.ts: pins the implicit document head and fragment pass-through (fails under the pre-fix: address React behavioral audit defects #943 early exit); adds wrappers that keep several directly obtained compiled results (fails under a shared/pooled carrier), wrappers returning their own text after such calls, and escaped string children of <pre>/<textarea> plus a <pre> template hole (all fail when strings are trusted).
  • docs/divergence-audit-2026-09-03.md: attribution follow-up and updated SSR table rows.
  • Changeset: octane patch.

Validation

Risk / follow-ups

  • Documents are still scanned; a document root is decided by the same prefix check the streaming shell already uses, and a fragment that authors its own <head> is returned unchanged either way.
  • Follow-up candidate: extend perf(ssr): skip redundant loop HTML carriers #945's unboxedHtml flag to @if/@switch/@try/Activity arms (control-flow fixture: 901 carrier calls per render today). Unmeasurable on the current fixtures, so not included here.

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

Low Risk
Performance-only early return on an existing SSR path with unchanged output and expanded contract tests; document head handling is preserved.

Overview
Recovers server rendering throughput lost after #943 by fixing spliceHead, not the HTML carrier. When the hoisted head is empty, a non-document body is returned immediately after an isDocumentRoot prefix check, so typical fragment responses no longer run indexOf('</head>') over the full string on every render. Document roots still get an implicit empty <head> when needed; serialized output is unchanged.

server-renderable-contract tests pin that document vs fragment head behavior, multi-result wrapper escaping, wrapper-owned text after compiled calls, and escaped string children in <pre> / <textarea>. The divergence audit and changeset document profiling that attributed the regression to spliceHead and the follow-up timings.

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

PR #943 moved spliceHead's empty-head early exit behind a full
`body.indexOf('</head>')` so a document root gains an implicit <head>.
Every fragment render with nothing to splice therefore scanned its
complete response, which a CPU profile shows as the whole SSR
throughput regression the audit attributed to the ServerHtml carrier;
bundles with and without the carrier time the same. Decide the common
case from the body's first bytes and return it unchanged; documents keep
their implicit head. Pin the document/fragment contract, add negative
escaping cases for wrappers and newline-sensitive hosts, and record the
attribution in the audit document.

Co-Authored-By: Claude Fable 5.1 <[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 Sep 3, 2026
@trueadm
trueadm marked this pull request as ready for review September 3, 2026 21:14
@trueadm
trueadm merged commit 415e7fd into main Sep 3, 2026
30 checks passed
@trueadm
trueadm deleted the perf/ssr-html-carrier-cost branch September 3, 2026 21:47
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.

1 participant