Repository navigation
perf(ssr): skip the head scan for fragment renders - #948
Merged
Merged
Conversation
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]>
trueadm
marked this pull request as ready for review
September 3, 2026 21:14
14 of 21 tasks
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
spliceHead, not to theServerHtmlcarrier.spliceHeadnow 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>.@foritem helpers in the carrier; that change is not repeated here.Why
benchmarks/ssr-throughputCONFIGS=deopt-pageand attributed the cost to the carrier.spliceHeadrising 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.head === ''early exit behind a fullbody.indexOf('</head>')so a document root gains an implicit<head>; only documents need that path, andisDocumentRootdecides it from the first bytes.Changes
packages/octane/src/runtime.server.ts:spliceHeadearly 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.octanepatch.Validation
./node_modules/.bin/vitest run packages/octane/tests/server-renderable-contract.test.tsplusconformance/ssr-serialization,ssr-scaffold,streaming-ssr,hydration/head-hydrate,production-error-bundle,ssr-element-utils,signals-native-collection.abi(dev and prod projects), on the commit rebased onto perf(ssr): skip redundant loop HTML carriers #945, again after merging main at e515b6c (with a fullpnpm typecheck), and the contract, scaffold, and streaming suites again after merging main at eff0271 (perf: recover transition hook hot-path cost from the audit fixes #949);pnpm syncand scoped Prettier checks were clean after each merge.Deliberate breaks: restoring the pre-fix: address React behavioral audit defects #943
if (head === '') return bodyfails the two new head tests; a shared mutable carrier fails the three multi-result wrapper tests; trusting every string fails eight escaping tests.pnpm typecheck,pnpm format:check,pnpm sync(no generated changes).pnpm teston this head (withNODE_OPTIONS=--max-old-space-size=8192; the default heap OOMed the orchestrator once under memory pressure): 28,749 passed, 2 failed on 5-second timeouts while two other test runs shared the machine (compiler/volar.test.ts"type-checks absent and optional host spread refs", the same test perf(ssr): skip redundant loop HTML carriers #945 saw time out, andsyntax-highlighter/tests/negative-controls.test.ts); both pass when re-run in isolation (27 tests).benchmarks/ssr-throughput/run.mjs,CONFIGS=deopt-page(three alternating rounds) andCONFIGS=escape(two), 1 s budget, 500 memory renders, bundles built from the archived pre-fix: address React behavioral audit defects #943 base, fix: address React behavioral audit defects #943, and fix: address React behavioral audit defects #943 with the carrier removed, Node 24.18.0 (the audit used 26.4.0), body lengths 134,189 / 148,003 bytes, every correctness gate passing (median of three runs, score ms):A second quiet session put fix: address React behavioral audit defects #943 and the carrier-free build within noise of each other (1.66–1.70 ms); the CPU profile is the discriminating evidence (below).
Final-build timing could only be taken while two other test runs shared the machine, so it uses a paired harness: the pre-fix: address React behavioral audit defects #943, fix: address React behavioral audit defects #943, merged-main, and this-branch bundles render in one process in alternating 300 ms slices, 12 rounds, so load drift hits every variant alike. Median ratios to the pre-fix: address React behavioral audit defects #943 base (per-round min–max in brackets):
The deopt rows are inside the spread; escape-heavy, where the scanned body is 7× larger, is the one row with a visible direction. No throughput number here is claimed at the audit's precision; the scan removal is established by the profile and by construction (an early return before the
indexOf), and the wall-clock effect scales with response size.CPU profile self time of
spliceHeadon the compiled deopt-page fixture (node --cpu-prof, same bundles): pre-fix: address React behavioral audit defects #943 0.0%, fix: address React behavioral audit defects #943 8.8%, merged main 13.7%, this branch 0.1%. The branch shows 9.6% inisDocumentRootinstead: on a V8 rope any character read flattens the string, andBuffer.byteLength(the harness materialization, and any real response writer) flattens it too, so the prefix check relocates a flatten the baseline pays elsewhere rather than adding one;(anon)drops correspondingly. Every other function is flat across builds.Isolated tail work on real render ropes (render with the pre-fix: address React behavioral audit defects #943 bundle, then time only each version's
spliceHeadoperations plusBuffer.byteLength; medians of 30–60 rounds, load-robust because each sample is a few hundred µs):benchmarks/bundle-size/run-minimal.mjsagainst merged main: 55 of 56 scenarios byte-identical;server-render37,682 → 37,709 raw, 13,353 → 13,368 gzip, 12,078 → 12,104 brotli (the added condition).Risk / follow-ups
<head>is returned unchanged either way.unboxedHtmlflag 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
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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 anisDocumentRootprefix check, so typical fragment responses no longer runindexOf('</head>')over the full string on every render. Document roots still get an implicit empty<head>when needed; serialized output is unchanged.server-renderable-contracttests 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 tospliceHeadand the follow-up timings.Reviewed by Cursor Bugbot for commit cd4cb22. Bugbot is set up for automated code reviews on this repo. Configure here.