Skip to content

perf(handler): eliminate repeated render scans and lookups - #496

Open
Mohamed Mansour (mohamedmansour) wants to merge 2 commits into
mainfrom
mohamedmansour-optimize-crate-rps
Open

perf(handler): eliminate repeated render scans and lookups#496
Mohamed Mansour (mohamedmansour) wants to merge 2 commits into
mainfrom
mohamedmansour-optimize-crate-rps

Conversation

@mohamedmansour

@mohamedmansour Mohamed Mansour (mohamedmansour) commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Why

Large component-list renders repeated two protocol-invariant or already-completed operations on the request hot path: identical inline CSS was rescanned for </style for every instance, and state-backed dynamic component attributes resolved the same path once for HTML output and again for child-scope capture.

What changed

  • Classify component and bundled inline style resources once when the immutable runtime Protocol loads.
  • Directly write known-safe CSS while preserving the existing escape writer for unsafe CSS.
  • Reuse a successful immutable-state attribute lookup for both HTML output and child-scope capture, while retaining the existing resolver fallback for owned, synthetic, missing, and streaming values.
  • Share protocol-owned CSS metadata across buffered and streaming render contexts.
  • Cover detector/writer parity, component and bundled-chunk cache classification, resolver provenance parity, and escaped borrowed attribute output.

Architecture

Both optimizations remain inside webui-handler and preserve existing ownership boundaries. Runtime-only metadata stays on immutable Protocol, not the protobuf wire model. Attribute lookup reuse preserves the established precedence across loop bindings, borrowed locals, owned locals, and global state. No public API, protocol, rendering semantics, or dependencies change.

Performance

Measured with:

cargo bench -p microsoft-webui --bench contact_book_bench -- 'contact_book_contacts_render/contacts/1000'
Metric Before After Change
Criterion mean render time 2.7039 ms 1.4830 ms -44.5%
Render throughput 857.66 MiB/s 1.5271 GiB/s +80.1%
Render/1000 P50 2.62 ms 1.48 ms -43.5%
FAST Render/1000 P50 3.66 ms 2.49 ms -32.0%
Output bytes 2,431,665 2,431,665 unchanged

The attribute lookup reuse alone improved the post-CSS-cache contact benchmark by 16.0% and the focused nested-component benchmark by 7.5%.

Validation

  • cargo xtask check
  • Independent review of cache lifecycle, Style/Module/Link behavior, buffered/streaming parity, resolver precedence, provenance, lifetimes, synthetic values, and escaping

Precompute which inline style resources require closing-tag escaping when the Protocol loads, avoiding repeated CSS scans across component instances and requests while preserving unsafe CSS escaping.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f56ec396-84b4-4878-9eb5-18e420c8b723

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves handler render performance by caching which inline style resources require </style-escaping at Protocol load time, so safe CSS can be written directly without rescanning identical bytes on every render.

Changes:

  • Added a protocol-owned HashSet cache of style resources whose CSS contains a case-insensitive </style sequence.
  • Updated the <style> emission path to use the cached classification: write raw CSS for known-safe resources, and fall back to the existing escape writer for unsafe resources.
  • Introduced a shared detector (style_text_needs_escape) and added tests for detector/writer parity and protocol cache classification.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
crates/webui-handler/src/streaming/session.rs Plumbs the protocol-owned escape cache into streaming render context initialization.
crates/webui-handler/src/streaming/inventory.rs Updates streaming inventory test context initialization to include the new escape cache reference.
crates/webui-handler/src/route_handler.rs Stores and builds the protocol-owned escape classification cache; adds a targeted unit test.
crates/webui-handler/src/lib.rs Uses the cache to select raw vs escaped CSS writing; adds the cache field to the render context.
crates/webui-handler/src/html_encode.rs Adds a fast </style detector shared by both the writer and the protocol classification; adds parity tests.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Reuse the immutable request-state lookup for dynamic component attribute output and child-scope capture instead of resolving the same path twice.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f56ec396-84b4-4878-9eb5-18e420c8b723
@mohamedmansour Mohamed Mansour (mohamedmansour) changed the title perf(handler): cache inline style escape checks perf(handler): eliminate repeated render scans and lookups Aug 27, 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.

2 participants