Skip to content

fix(renderer): stop :has() rules from restyling the whole page during Mermaid renders - #1

Open
naufaldi wants to merge 1 commit into
mainfrom
fix/mermaid-preview-has-invalidation-34312
Open

naufaldi wants to merge 1 commit into
mainfrom
fix/mermaid-preview-has-invalidation-34312

Conversation

@naufaldi

@naufaldi naufaldi commented Oct 27, 2026 •

Copy link
Copy Markdown
Owner

ELI5

When Orca draws a Mermaid diagram, it measures every label. Each measurement makes the browser re-check styles. Three CSS rules made the browser re-check styles for the whole app on every measurement, instead of just the diagram. With 19 diagrams that is about 1,000 whole-app re-checks, so the window froze for 40+ seconds and scrolling crawled. This PR rewrites those three rules so their re-checks stay small. They look and behave the same.

What Changed

Before: opening a large Markdown preview with many Mermaid diagrams (the synthetic doc from the issue: 1,167 lines, 19 diagrams) froze the renderer while diagrams rendered. Wheel scrolling moved about 500 px in 10 s, at about 1 fps.
After: on a production build, all 19 diagrams render in about 1.5 s instead of about 40 s, scrolling stays responsive while they render, and switching back to the tab takes about 1.4 s instead of 35–55 s.

Mechanism. mermaid.render() calls getBBox(), getComputedTextLength() and getTotalLength() for each label and edge. Each call forces a synchronous style recalc. Normally that recalc only touches the nodes Mermaid just changed. Three rules made Chromium recalc the entire document (median ~5,400 elements per recalc in a timeline trace):

Rule Where Change
body:has(> overlay) .app-layout > :not(.window-controls) { filter: blur(2px) } main.css (stablyai#33799) body:has(> overlay) now only sets --modal-scene-filter; .app-layout > :not(.window-controls) reads filter: var(--modal-scene-filter). This is the same shape stablyai#23721 used for --toaster-z-index.
group-has-[:focus-visible]/row:opacity-100 SshTargetCard.tsx [[class~='group/row']:has(:focus-visible)_&]:opacity-100, the scoped form stablyai#23725 used for group/code
group-has-[:focus-visible]/reasoning:opacity-100 NativeChatReasoningDisclosure.tsx [[class~='group/reasoning']:has(:focus-visible)_&]:opacity-100

I deleted rules from the live stylesheet through CSSOM, reloading the renderer between runs. Any one of the three is enough on its own to cause the freeze. The two Tailwind utilities hurt even when no matching element is mounted. Tailwind compiles them to :is(:where(.group\/x):has(:is(:focus-visible)) *).

Behavior is unchanged:

  • Blur: when no modal is open, --modal-scene-filter is unset, so filter computes to none. That is the same as the old rule not matching. I checked that no .app-layout child sets its own filter.
  • Focus reveals: both still show only on keyboard :focus-visible inside the group, not on mouse focus.

Why

  • Not lazy-rendering diagrams. The issue suggests it, but each diagram would still freeze the window when it scrolls into view. The slowness is in the CSS invalidation, not in Mermaid's work.
  • Not moving Mermaid into an isolated container. Mermaid measures some diagram types (for example sequence diagrams) through body regardless of the container it's given, so that wouldn't fully isolate it. Fixing the selectors also helps everything else that forces a style read under body, such as Monaco and tooltips.
  • Same patterns as earlier fixes. These are the shapes the repo already settled on in fix(renderer): stop the modal toast rule from freezing large diffs stablyai/orca#23721 and fix: bound combined diff editors and scope chat style invalidation stablyai/orca#23725; the three rules re-introduced the old forms.
  • The group variants interact with other :has() rules. An independent probe (Electron 43.8.1, about 6,000 elements, Mermaid rendering) showed the old group/row variant costing about 800 ms of style work with a toast visible, versus 8–10 ms with this branch, even with no SSH row mounted. Perf checks for this kind of selector should run with a toast and a dialog present, not only on an empty scene.
  • main.css:540 (body:has(…) [data-sonner-toaster] [data-sonner-toast]…) is left unchanged on purpose. It has the same descendant-:has() shape, but with only that rule present, style recalc stayed at about 2 s over the whole open (versus about 45 s with only the blur rule present). My inference, not verified in Blink: its rightmost compound has specific attribute features, so Chromium can narrow the invalidation, while :not(.window-controls) and … * cannot.

Linked Issue

Addresses stablyai#34312: the render-time freeze, measured on a production build (see Measurements). Not verified on the reporter's private document. Switching back to the tab still redraws every diagram (MermaidBlock has no SVG cache), now in about 1.4 s.

Visual Proof

Production build

pnpm build:electron-vite bundle run hidden, CDP screenshots. Same scripted journey both times: open repro-100k.md from the file explorer and wheel-scroll 100 px per step from first paint, for a 15 s budget. The overlay shows the real wall time. Before is main c98a2d6's three files built the same way.

Before (main) After (this branch)
Rich Editor (default click) before rich after rich
16.5 s: 10 wheel steps, 900 px, 12 / 19 diagrams, longest freeze 3.0 s 15 s: 397 wheel steps, 39,700 px, 19 / 19 diagrams, longest freeze 159 ms
Open Markdown Preview before preview after preview
15.3 s: 7 wheel steps, 700 px, 9 / 19 diagrams, longest freeze 3.6 s 15 s: 345 wheel steps, 34,500 px, 19 / 19 diagrams, longest freeze 182 ms

Dev build (first investigation)

Scripted wheel scroll, 100 px per step, starting the moment the first diagram appears (hidden window, CDP screenshot, dev build). The overlay is added by the measurement script. The script stops sending wheel events after 10 s, but it awaits each one. While the renderer is frozen a single event can take seconds, so the before run actually lasted about 21 s and the after run about 10.5 s (rAF callbacks ÷ rate shown in the overlay). The overlay's "10 s" label and "frames painted" (really requestAnimationFrame callbacks) are inaccurate; the numbers under each image are corrected.

Before (main 4c66f91) After (this branch)
before after
~21 s elapsed: 500 px scrolled, 7 / 19 diagrams, 19 rAF callbacks (0.9/s) ~10.5 s elapsed: 19,900 px scrolled, 17 / 19 diagrams, 503 rAF callbacks (47.7/s)

Modal blur is unchanged on this branch (screenshots captured by tests/e2e/modal-scene-blur.spec.ts):

Light Dark
blur light blur dark

Measurements

Production build. repro-100k.md is the issue's generator output padded with prose to the reporter's size (105 KB, 1,468 lines, 19 diagrams). macOS, 8 GB Apple Silicon, hidden window at 1440×825, files opened through the UI only (explorer click and the "Open Markdown Preview" context-menu item).

Journey View Before (main) After
Cold open until all 19 diagrams drawn Rich Editor 40.2 s; longest freeze 8.8 s; style recalc 37.6 s 1.3 s; longest freeze 166 ms; style recalc 0.22 s
Markdown Preview 34.4 s; longest freeze 2.9 s; style recalc 32.3 s 1.7 s; longest freeze 101 ms; style recalc 0.21 s
Scroll from first paint (15 s budget) Rich Editor 900 px in 16.5 s 39,700 px in 15 s, frame p95 18.3 ms
Markdown Preview 700 px in 15.3 s 34,500 px in 15 s, frame p95 18.1 ms
Scroll top to bottom after all diagrams drawn Rich Editor p95 18.1 ms, worst 35 ms p95 18.1 ms, worst 19 ms
Markdown Preview p95 17.9 ms, worst 33 ms p95 18.1 ms, worst 35 ms
Switch to Terminal and back, until all diagrams redrawn Rich Editor 35.4 s; longest freeze 4.4 s 1.4 s; longest freeze 188 ms
Markdown Preview 55.3 s; longest freeze 5.4 s 1.5 s; longest freeze 196 ms

Scrolling after everything has rendered was already smooth before; the freeze is entirely while diagrams render, which includes every tab switch back. The isolated 1–2.8 s settled-scroll frames in the dev numbers below did not appear on the production build.

Dev build (first investigation, issue's 65 KB repro.md):

Synthetic doc from the issue's gen_repro.py, macOS, 8 GB Apple Silicon, dev build (pnpm dev --worktree-profile, hidden window at 1440×825), Playwright over CDP. The cost is in native style recalc, not JS (JS was about 1 s either way); the production table above confirms the same pattern.

Metric Before After
All 19 diagrams rendered 47.5 s 4.2 s
Style recalc during open 34.9–52.6 s 0.36 s
getBBox + getComputedTextLength + getTotalLength self time ~30 s ~0.3 s
Long tasks during open 34 tasks / 41 s total / max 5.5 s 18 / 3.2 s / max 391 ms
Wheel scrolling during the first ~20 s 7 steps, 700 px, 29 frames in 24 s 405 steps, 40,500 px, median frame 16.7 ms
Scrolling after all diagrams settled (p50 / p95 frame) 16.7 / 17.6 ms 16.7 / 17.6 ms

Control: the same doc with the Mermaid fences changed to text had 0.8 s of long tasks on open. Without diagrams, document size alone is not the problem.

Not fixed here: MermaidBlock keeps no SVG cache, so switching back to the tab redraws every diagram (about 1.4 s on the production build, previously 35–55 s). A cache, or rendering only diagrams near the viewport, could remove that remaining cost. I kept both out of this focused fix.

Testing

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Perf, production build: the journey table above, scripted over CDP against pnpm build:electron-vite output for both main and this branch, launched hidden with ORCA_BACKGROUND_LAUNCH=1.

  • Perf, dev build: before/after with scripted CDP runs (open phase CPU profile, timeline trace, wheel scrolling during and after rendering), with the CSSOM bisection described above.

  • Blur behavior: ORCA_BACKGROUND_LAUNCH=1 pnpm test:e2e tests/e2e/modal-scene-blur.spec.ts passes 5/5 on the final head. That covers the dialog, the command palette, the Space window dialog, onboarding, and the nested onboarding confirmation, in light and dark.

  • Focus reveals: checked in the live renderer stylesheet with a temporary element using the exact new classes. Opacity is 0 when idle, 0 on focus without :focus-visible, and 1 on :focus-visible, for both group/row and group/reasoning. I did not mount the real SshTargetCard or reasoning row for this.

  • Repo gate: pnpm check:changed --maxWorkers=2 passes all 20 checks.

  • No new test: an earlier source-shape guard for this kind of selector was removed as low-value in [CI] Reduce CI work by removing low-value tests and duplicate fuzz comparisons stablyai/orca#25791, and modal-scene-blur.spec.ts already covers the changed behavior.

  • Platforms: macOS only. Linux, Windows and SSH were not run. The change is renderer CSS and class names only, with no IPC, protocol, path or shortcut changes.

AI Disclosure

Claude Opus 5.5 via Claude Code did the investigation, profiling, fix, verification and this write-up. GPT-6.1 Sol via Codex did an independent review. The author reviewed the change.

Review

Self-reviewed, plus an independent read-only review by GPT-6.1 Sol (Codex). It reproduced the mechanism in a hidden Electron 43.8.1 probe and confirmed the compiled Tailwind selectors. It found no remaining group-has-* / peer-has-* and no blur or focus regression. It flagged the screenshot timing and the "Fixes" claim, both corrected above. The repo's existing modal-blur e2e spec covers the visual change.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Security: none; CSS only.
  • Cross-platform: the same CSS ships on every platform. This is a Chromium style-engine behavior, so it is not platform-specific.
  • Remote SSH / mixed versions: renderer-only; nothing is sent over the wire.
  • Mobile: no mobile-specific code changed. Any client that loads this renderer CSS gets the same fix.
  • Backwards compatibility: the visuals are the same.
  • Performance: this PR is the performance fix.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred). Locally: pnpm check:changed (format, lint, typecheck, affected tests, ratchets) and an e2e build pass; the full pnpm test and pnpm build were not run. pnpm build:electron-vite passes.

🤖 Generated with Claude Code

… Mermaid renders

Opening a Markdown preview with many Mermaid diagrams froze scrolling for
tens of seconds. Mermaid measures each label with getBBox /
getComputedTextLength / getTotalLength, and every call forces a style
recalc. Three rules made each of those recalcs cover the whole document:

- body:has(> overlay) .app-layout > :not(.window-controls) (modal blur)
- group-has-[:focus-visible]/row (SshTargetCard)
- group-has-[:focus-visible]/reasoning (NativeChatReasoningDisclosure)

Each one alone was enough. The blur now flows through a body custom
property set by a subject-only body:has(), the same shape stablyai#23721 used
for the toaster z-index. The two Tailwind utilities use the scoped
[[class~='group/x']:has(:focus-visible)_&] form from stablyai#23725.

On the issue's 19-diagram repro (dev build), all diagrams render in
4.2 s instead of 47.5 s, style recalc during open drops from ~35 s to
0.36 s, and scrolling while diagrams render goes from about 1 fps to a
16.7 ms median frame.

Refs stablyai#34312

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@naufaldi
naufaldi force-pushed the fix/mermaid-preview-has-invalidation-34312 branch from 3637cb1 to 9b861c4 Compare October 28, 2026 01:39
nwparker added a commit to stablyai/orca that referenced this pull request Oct 29, 2026
…ole subtrees (#36098)

Fixes #34312. Takes over naufaldi#1 by @naufaldi, with the main.css part dropped because #34932 already covers it.

- SshTargetCard and the reasoning chevron now use the scoped `[[class~='group/x']:has(:focus-visible)_&]` shape. Tailwind's `group-has-*` compiles to a non-subject `:has()` whose subject is `*`, and that rule made Blink restyle whole subtrees on every mutation. This change is the entire measured win: 19.4 → 1.6 ms, 12.5 → 3.6 ms and 8.0 → 2.4 ms per mutation.
- rich-markdown-editor.css drops a redundant `:has()` on the TOC header, as defence in depth.
- Adds a guard, check-has-restyle-scope.mjs. It compiles the renderer's Tailwind CSS, adds plain stylesheets and the listed vendor stylesheets, and fails on any non-subject `:has()` whose subject has no class, id or attribute. Three pdf.js rules are baselined after being measured as harmless. It runs in lint, check:changed and PR CI in about 1.5 s.

Co-authored-by: Naufaldi <[email protected]>
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.

1 participant