feat: avoid re-evaluating the hydrate platform closure on every SSR render
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Tranquilla
- Stack tecnologico
- typescript
- Ambito
- performance, web-dev
Direzione di ricerca
Inizia da src/compiler/output-targets/dist-hydrate-script/hydrate-factory-closure.ts, src/hydrate/runner/render.ts e src/declarations/stencil-public-compiler.ts. Esegui prima le suite di test di hydrate e il benchmark di riproduzione collegato; il lavoro è completo quando la nuova opzione e il riutilizzo della closure funzionano senza rompere il comportamento esistente, incluso il rendering e la serializzazione delle finestre raggruppate.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Prerequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already include this feature request, without success.
Describe the Feature Request
Every call to renderToString() / hydrateDocument() from the dist-hydrate-script output re-executes hydrateAppClosure and constructs a fresh MockWindow. hydrateAppClosure is the function in the generated hydrate module that wraps the whole Stencil platform (runtime, vdom) and every component class in the library. The closure exists so the platform can capture the per-call window/document, but re-evaluating it on each render is a fixed cost that scales with the size of the component library rather than with the content being rendered.
This request is for two changes:
- Cache the evaluated closure per window (transparent, no API change): in
HYDRATE_FACTORY_OUTRO, store the result ofhydrateAppClosure(win)on the window object and reuse it when the same window is passed again. - Add an opt-in
reuseWindow?: booleanhydrate option. When set,hydrateDocument/renderToStringreuse a pooledMockWindowacross calls instead of creating and destroying one per render. The pool is keyed by theserializeShadowRootconfiguration, because'scoped'permanently ORsshadowNeedsScopedCssintocmpMetaflags inside the evaluated closure. Freshhead/bodyelements are swapped in per render, so therootAppliedStylesWeakMap (keyed on the head node) resets correctly, and renders against a shared window are serialized through an internal queue.
Describe the Use Case
We maintain a design system (~300 shadow components, hydrate module >10 MB) consumed by high-traffic Next.js (App Router) sites via @stencil/react-output-target. Its SSR integration (createComponentForServerSideRendering) calls renderToString once per component boundary in the React tree, so a page with 150 Stencil component boundaries pays the fixed per-call overhead 150 times per request.
Measured with the attached reproduction (300 components, @stencil/core 4.43.5, Node 22):
- The fixed overhead is ~0.5 ms median per
renderToStringcall. A page with one component instance costs 6% of a page with 200 instances, so the per-call cost comes from platform re-evaluation and window construction, not from rendering. - A simulated request (150 component boundaries) spends ~54-62 ms in pure hydrate overhead before any application work.
- A CPU profile of 500 small renders under the V8 sampling profiler puts 42-49% of all sampled time inside
hydrateAppClosure(platform re-evaluation, withregisterComponentsalone at 19.6% self time), plus ~17% in garbage collection caused by the allocation churn.MockWindowconstruction is ~6.5%. - With both proposed changes the simulated request drops to 7.9 ms (6.7x), the p95 of single-component renders drops from 1.60 ms to 0.19 ms, and
hydrateAppClosureandMockWindowfall to 0.0% of profile samples.
Single large one-shot renders are unaffected (1.03x), since their time is real rendering work. The win is specific to the many-small-renders SSR pattern that production React/Next.js integrations use.
Describe Preferred Solution
Implementation is complete on a branch (test/hydrate-platform-reuse, happy to open a PR):
src/compiler/output-targets/dist-hydrate-script/hydrate-factory-closure.tscacheshydrateAppClosure(win)on the window object in the factory outro.src/hydrate/runner/render.tsadds thereuseWindowsupport: aMapof pooledMockWindows keyed by serializedserializeShadowRootoptions. Per render, freshhead/bodyelements are swapped in viareplaceChildand the render is queued behind any in-flight render on the same window.destroyWindow/destroyDocumentare forced off for pooled windows.src/declarations/stencil-public-compiler.tsdocuments the newreuseWindow?: booleanoption onHydrateDocumentOptions(defaultfalse, fully backwards compatible).
Existing test suites pass (test.jest, test.end-to-end).
Describe Alternatives
- Userland window pooling via the existing
destroyWindow: falseoption is not enough. The platform closure is still re-evaluated per call (the cache in change 1 is not reachable from userland), and internal state such asrootAppliedStylesand the mode/flag mutations underserializeShadowRoot: 'scoped'make naive window reuse incorrect. - Caching rendered HTML at the application layer only helps repeated identical pages; personalised and dynamic pages still pay the full cost.
Related Code
Reproduction repo: https://github.com/Armand-Lluka/stencil-hydrate-perf-repro. Run npm install && npm run generate && npm run build && npm run bench. The benchmark prints the baseline table above and, when run against a build containing the proposed changes, a comparison section. node profile.mjs produces the CPU profile attribution.
Additional Information
- The closure cache (change 1) alone benefits any consumer that passes the same window twice;
reuseWindow(change 2) is what makes it reachable through the publicrenderToString/hydrateDocumentAPI. - Concurrency: parallel
renderToStringcalls withreuseWindow: trueare serialized per pooled window; callers wanting parallelism can keep the default behaviour.
- Lingua principale
- TypeScript
- Stelle
- 13.2k
- Fork
- 865
- Merge medio
- 2g 16h
- PR unite (30g)
- 29
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di stenciljs/core
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
bug(mock-doc): `Element.toggleAttribute()` is missing, so components that use it fail during SSRApertatriage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Stencil FOUC styles should use @layer to avoid cascade conflicts with React 19Forse già presa @johnjenkins l’ha presa 230 giorni fa. ApertaStencil v5
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
stenciljs/core#7016 · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 22/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di stenciljs/core
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
AykutSarac/jsoncrack.com#616 ·
-
nip27: long hashtag swallows the rest of the content, lone # gives an empty hashtagForse già presa @bardrop l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
nbd-wtf/nostr-tools#578 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
backnotprop/plannotator#3427 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
microsoft/Power-CAT-Copilot-Studio-Kit#942 ·
I maintainer di solito rispondono entro 1 giorno
-
[BUG]: Duplicate preposition in heading and unformatted feedback text on Strengthen Argument pageApertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
AOSSIE-Org/DebateAI#801 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno