Skip to content

perf(handler): reduce render lookup and scope allocations - #472

Merged
Mohamed Mansour (mohamedmansour) merged 2 commits into
mainfrom
mohamedmansour-perf-handler-render-fragment-index
Aug 25, 2026
Merged

Mohamed Mansour (mohamedmansour) merged 2 commits into
mainfrom
mohamedmansour-perf-handler-render-fragment-index

Conversation

@mohamedmansour

@mohamedmansour Mohamed Mansour (mohamedmansour) commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Verdict

GO as one combined performance change. This PR now contains both the render-fragment index and borrowed render scopes. It consolidates and supersedes #474.

Summary

  • precompute a compact render-fragment index once in Protocol::new, replacing repeated fragment-target hashes, component prop-name conversion, and unnecessary sibling-route scans with numeric slots and flat metadata arenas
  • borrow state-backed loop items and component attributes through ordinary render scopes instead of cloning JSON subtrees
  • preserve lexical precedence across nested loops, component locals, and global fallback
  • materialize borrowed component attributes only where progressive streaming must retain them across host calls
  • keep all new APIs crate-private; no protobuf, public API, or unsafe changes

Combined performance case

B1 alone moved work from render time to protocol construction. It improved representative fragment-heavy renders by roughly 2–5%, but added a one-time Protocol::new cost of +24 µs for 128 fragments and +128 µs for 512 fragments, plus +3.6–5.1% retained protocol memory. It also had one measured all-miss condition case at +4.15%.

B2 makes the combined stack clearly favorable. Sequential custom-allocator measurements below compare B1 with this combined B1+B2 branch on the contact-book /contacts route:

Contacts FAST Allocations Allocated bytes Median render Throughput
10 no 814 → 220 (-73.0%) 36,143 → 9,829 (-72.8%) 58.855 → 36.432 µs (-38.1%) +61.5%
10 yes 982 → 388 (-60.5%) 86,420 → 60,106 (-30.4%) 84.353 → 62.702 µs (-25.7%) +34.5%
100 no 6,934 → 1,210 (-82.5%) 277,972 → 33,049 (-88.1%) 466.528 → 258.856 µs (-44.5%) +80.2%
100 yes 7,104 → 1,380 (-80.6%) 426,553 → 181,630 (-57.4%) 560.254 → 355.026 µs (-36.6%) +57.8%
1,000 no 68,134 → 11,110 (-83.7%) 2,713,293 → 267,949 (-90.1%) 5.038 → 2.491 ms (-50.6%) +102.3%
1,000 yes 68,308 → 11,284 (-83.5%) 4,827,954 → 2,382,610 (-50.6%) 6.434 → 3.789 ms (-41.1%) +69.8%

Allocation deltas are deterministic: exactly 594, 5,724, and 57,024 allocation calls are removed at the 10/100/1,000 scales in both plugin modes. Timing is corroboration; the allocation counts are the primary evidence.

B2 does not hide B1's construction tradeoff. All 16 construction fixtures retain exactly the same bytes under B1 and combined B1+B2 (contact-book remains 102,619 bytes; the 200-route synthetic remains 574,831 bytes). The combined render savings amortize B1's largest measured construction penalty in one render at 100/1,000 contacts and in roughly six renders at 10 contacts. Completed renders retain zero additional bytes.

For the isolated B1 layer, minimum-time measurements against the original base recorded loop_scaling/500 -5.54%, plugin_fast/without_plugin -4.89%, and loop_scaling/100 -4.58%. The combined measurements above then show the larger B2 gain on top of B1.

Correctness

  • 90 deterministic RENDER/STREAM harness lines are byte-identical between B1 and combined B1+B2
  • excluded only the documented nondeterministic component-assets path=/ plugin=true line, which flips between the same two hashes on the original base
  • retained protocol bytes are unchanged between B1 and combined B1+B2 for all 16 fixtures
  • resolver tests cover borrowed global collections, borrowed component collections, nested loops, owned-local precedence, lexical shadowing, and component isolation
  • progressive streaming keeps owned continuation state and avoids a duplicate resolver lookup

Implementation notes

RenderFragmentIndex reuses interned fragment IDs, stores metadata and canonical component prop names in flat arenas, tracks route presence in a bitset, and resolves fragment lists lazily through eight inline memo slots plus an on-demand spill. Render and continuation slots use the same sorted fragment-ID numbering.

Ordinary rendering uses BorrowedScope, LoopBinding, and VisibleLoopScope to retain references into immutable request state. Four inline borrowed-scope slots cover the common component-attribute case; overflow storage and request-local pools remain bounded. Owned fallback paths preserve synthetic values and continuation lifetimes.

Validation

  • cargo test -p microsoft-webui-handler
  • cargo xtask check — all phases passed, including clippy, deny, workspace tests, native/WASM builds, examples, benchmark validation, and docs (using same-drive TEMP/TMP required by the Windows projection adapter)

Prepare a per-protocol render fragment index once at load so the render
path resolves fragment descents, component prop names and route presence
through numeric slots instead of repeating string hash lookups and
sibling-route scans on every fragment.

Protocol::new builds a flat index that shares the already interned
fragment id Arcs and packs metadata, component prop names and route
presence bits into single arenas, so large protocols are indexed without
duplicating fragment graphs. Render resolves the index once per request
into a small inline memo cache that spills lazily for protocols with more
fragment lists than the cache holds.

Output is byte-identical for buffered and streaming rendering, missing
fragment and component references keep returning the same typed errors,
and no public API or protobuf wire format changes.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@mohamedmansour
Mohamed Mansour (mohamedmansour) marked this pull request as ready for review August 25, 2026 02:27
@mohamedmansour Mohamed Mansour (mohamedmansour) changed the title refactor(handler): precompute a render fragment index perf(handler): reduce render lookup and scope allocations Aug 25, 2026
@mohamedmansour
Mohamed Mansour (mohamedmansour) merged commit 1cdc3b3 into main Aug 25, 2026
35 checks passed
@mohamedmansour
Mohamed Mansour (mohamedmansour) deleted the mohamedmansour-perf-handler-render-fragment-index branch August 25, 2026 02:57
Mohamed Mansour (mohamedmansour) added a commit that referenced this pull request Aug 26, 2026
## Release

Bumps WebUI to `0.0.26`.

Previous release tag: `v0.0.25`

## Changes since `v0.0.25`

Features:

- Python hosts now have an official typed PyO3 renderer package with
buffered, partial, template, token, and host-driven streaming APIs
(feat: add official Python renderer package #453 by @mohamedmansour).
- Progressive SSR can place transport-flushed streaming boundaries
inside reusable components while preserving component ownership and
continuation state (feat: support component-local streaming boundaries
#460 by @mohamedmansour).
- Components can defer compiler-owned hydration until interaction, with
router preload handoff and a combined lazy-render policy (feat: add
compiler-driven interaction hydration #485 by @mohamedmansour).
- Builds can opt into global Light DOM CSS while preserving explicit
authored Shadow roots and deterministic style closures (feat: make Light
DOM a global CSS opt-in #429 by @mohamedmansour).
- State projection supports application-owned TypeScript 7.0.2 while
retaining the TypeScript 6 migration path (feat: support TypeScript 7
projection compilation #452 by @mohamedmansour).
- WebUI Press supports layout-scoped compile-time named regions with
fallback HTML, state, components, and scripts (feat(press): add
compile-time named regions #484 by @mohamedmansour).
- FAST v2 and v3 gain plugin-owned local and npm component discovery
with validated FAST template transformation (feat: add plugin-owned FAST
component discovery #378 by @janechu).

Fixes:

- Missing condition identifiers are treated as falsy before negation and
logical evaluation, aligning SSR with the browser runtime (fix: negate
missing condition paths correctly #449 by @mohamedmansour).
- FAST route state now scales through escaped scalar kebab-case
attributes shared by FAST v2 and v3 (fix: scale FAST route state with
scalar attributes #450 by @janechu).
- Dynamic Link-mode components wait for native stylesheet readiness
across navigation and component assets, preventing unstyled flashes
(fix: prevent dynamic component stylesheet flashes #454 by
@mohamedmansour).
- Authored component definitions defer whenever compiled template
metadata has not arrived, including ordinary router navigation (fix:
defer authored define() whenever template metadata is missing #461 by
@mohamedmansour).
- Client structural updates preserve authored order and sibling
ownership for shared slots and raw HTML ranges (fix: preserve source
order for shared structural slots #465 by @mohamedmansour, fix: preserve
siblings around raw HTML updates #466 by @mohamedmansour).
- Native-element attributes no longer leak into the local state of a
later component (perf(handler): stop native attributes leaking into
component state #469 by @mohamedmansour).
- Templates-and-state-only SSR bootstrap payloads no longer require
component style metadata (fix: allow SSR bootstrap without component
styles #475 by @mohamedmansour).
- The high-level Rust `serve_request` path now preserves complete render
options, including CSP nonces, while sharing the same entry and request
path with partial rendering (fix: forward CSP nonces through
serve_request #488 by @mohamedmansour).
- Projection compilation bounds source reads, excludes binary and
unsupported-loader inputs from semantic analysis, and preserves
deterministic cleanup under large esbuild graphs (fix: bound projection
adapter source reads #489 by @mohamedmansour).
- Router pending UI remains mounted through pre-commit work and settles
at the synchronous DOM commit, with stale, aborted, and re-entrant
navigation cleanup kept generation-safe (fix: settle router pending UI
during navigation commits #491 by @mohamedmansour).

Docs:

- The documentation site adds accessible responsive navigation, improved
reading layouts, stronger search behavior, and Playground recovery
states (feat: polish WebUI documentation experience #459 by
@mohamedmansour).
- Slot-resolution implementation guidance now documents pre-order
lookup, pending placement ownership, and marker handling (chore: clarify
slot resolution comments #471 by @mohamedmansour).
- Repeated `w-ref` behavior is now explicit: refs are scalar and the
last wired occurrence wins, while stable authored IDs or item components
provide identity-based lookup (chore: clarify repeated w-ref behavior
#490 by @mohamedmansour).

Maintenance:

- Release and package policy metadata now constrains the transitive h2
advisory, uses SPDX NuGet licensing, and classifies publishing jobs
correctly (chore: allow constrained h2 advisory #451 by @janechu, fix:
use modern NuGet license metadata #457 by @janechu, chore: mark
publishing jobs as release jobs #458 by @janechu).
- The development and CI Rust toolchain is updated to 1.98 with the
resulting warnings resolved (Update rust toolchain version and fix
clippy warning #462 by @telecos).
- Release builds use Thin LTO to retain cross-crate optimization with
faster linking (perf: switch release builds to Thin LTO #464 by
@mohamedmansour).
- Handler and expression hot paths reduce attribute vtable calls, render
lookup and scope allocations, and single-term condition overhead
(perf(handler): centralize HTML attribute writing in ResponseWriter #467
by @mohamedmansour, perf(expressions): fast-path single-term conditions
#470 by @mohamedmansour, perf(handler): reduce render lookup and scope
allocations #472 by @mohamedmansour).
- Node rendering can reuse immutable prepared-state snapshots and
bounded per-route output capacity hints (perf(node): reuse prepared
state across renders #477 by @mohamedmansour, perf: reuse Node render
output capacity #478 by @mohamedmansour).
- Framework hydration releases bootstrap data sooner and reduces
allocations for bindings, empty hosts, and visible conditionals (perf:
reduce framework hydration allocations #479 by @mohamedmansour, perf:
release SSR bootstrap memory after hydration #480 by @mohamedmansour,
perf: reduce empty template host overhead #482 by @mohamedmansour, perf:
remove visible conditional anchors #483 by @mohamedmansour).
- Benchmark tooling now renders the full contact workload, runs
Criterion baselines per target, and restores the streaming hydration
fixture (fix(bench): render contacts in contact-book benchmark #468 by
@mohamedmansour, fix(xtask): run Criterion benchmarks per target #473 by
@mohamedmansour, fix: repair streaming hydration benchmark fixture #476
by @mohamedmansour).

## Validation

- `cargo xtask check`

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.

2 participants