Repository navigation
Conversation
… 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
force-pushed
the
fix/mermaid-preview-has-invalidation-34312
branch
from
October 28, 2026 01:39
3637cb1 to
9b861c4
Compare
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]>
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.
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()callsgetBBox(),getComputedTextLength()andgetTotalLength()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):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)readsfilter: var(--modal-scene-filter). This is the same shape stablyai#23721 used for--toaster-z-index.group-has-[:focus-visible]/row:opacity-100SshTargetCard.tsx[[class~='group/row']:has(:focus-visible)_&]:opacity-100, the scoped form stablyai#23725 used forgroup/codegroup-has-[:focus-visible]/reasoning:opacity-100NativeChatReasoningDisclosure.tsx[[class~='group/reasoning']:has(:focus-visible)_&]:opacity-100I 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:
--modal-scene-filteris unset, sofiltercomputes tonone. That is the same as the old rule not matching. I checked that no.app-layoutchild sets its ownfilter.:focus-visibleinside the group, not on mouse focus.Why
bodyregardless 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 underbody, such as Monaco and tooltips.:has()rules. An independent probe (Electron 43.8.1, about 6,000 elements, Mermaid rendering) showed the oldgroup/rowvariant 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 (
MermaidBlockhas no SVG cache), now in about 1.4 s.Visual Proof
Production build
pnpm build:electron-vitebundle run hidden, CDP screenshots. Same scripted journey both times: openrepro-100k.mdfrom 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 ismainc98a2d6's three files built the same way.main)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
requestAnimationFramecallbacks) are inaccurate; the numbers under each image are corrected.main4c66f91)Modal blur is unchanged on this branch (screenshots captured by
tests/e2e/modal-scene-blur.spec.ts):Measurements
Production build.
repro-100k.mdis 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).main)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.getBBox+getComputedTextLength+getTotalLengthself timeControl: the same doc with the Mermaid fences changed to
texthad 0.8 s of long tasks on open. Without diagrams, document size alone is not the problem.Not fixed here:
MermaidBlockkeeps 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-viteoutput for bothmainand this branch, launched hidden withORCA_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.tspasses 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 bothgroup/rowandgroup/reasoning. I did not mount the realSshTargetCardor reasoning row for this.Repo gate:
pnpm check:changed --maxWorkers=2passes 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.tsalready 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
Notes
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred). Locally:pnpm check:changed(format, lint, typecheck, affected tests, ratchets) and an e2e build pass; the fullpnpm testandpnpm buildwere not run.pnpm build:electron-vitepasses.🤖 Generated with Claude Code