Skip to content

perf(app): faster tab switching for documents with many headings - #801

Merged
murongg merged 6 commits into
markrahq:v2from
leemour:perf/tab-switch
Nov 25, 2026
Merged

murongg merged 6 commits into
markrahq:v2from
leemour:perf/tab-switch

Conversation

@leemour

@leemour leemour commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Switching to a tab with a long document felt slow on Linux. CHANGELOG.md (488 headings) took about 450 ms to show, other tabs about 50 ms. Most of that time went into heading titles, which went through remark on every switch, even with the sidebar closed:

  • aiAgentContext called editor.getTableAnchors() on every switch, because its dependencies include the document content. That reads all heading anchors and parses each title, only to show a table count in the AI agent panel, which isn't mounted while it is closed.
  • The document summary (outline and word count) was rebuilt from scratch on every switch. Nothing kept the result per tab, and the active content changes with every switch.
  • Every outline row rendered its title through react-markdown again on every drawer render, and the drawer re-renders several times per switch.

Changes

  • App.tsx: aiAgentContext counts tables only while the AI agent panel is visible. The panel is the only place that shows the count, and it is mounted only when visible.
  • controller.ts: readCodeMirrorTableAnchors reads heading anchors on the first table it finds. With the panel open, a document without tables now skips the heading parse.
  • useMarkdownDocument.ts: the hook keeps the last summary of each open tab in a ref, with the content and document revision it was built from. Entries of closed tabs are dropped. An unchanged tab reuses its summary without a reparse. On the deferred path (documents over 64k chars) the render shows the tab's last summary while the revision matches, even if the content changed since, and the idle callback replaces it. That is the rule the deferred path already used while one document is edited. Before, switching back to a large tab showed an empty outline until the idle callback ran.
  • MarkdownFileTreeDrawer.tsx: OutlineTitle is memoized on title and titleMarkdown. Rows mount fresh after a switch to another document, so this does not help the first render. It saves the markdown render on the App re-renders that follow a switch and on active-heading changes.
  • Tests:
    • useMarkdownDocument.test.tsx: for both the immediate and the deferred summary path, the test records every render and checks the outline after each switch and edit: switching back shows that tab's outline with no empty render, an edit right before leaving a tab keeps an outline when you come back, and unchanged documents are not parsed again. Both cases fail on v2.
    • MarkdownFileTreeDrawer.test.tsx: an outline row re-renders its markdown title only when the heading changes. It fails without the memo.

The existing controller.test.ts case "extracts GFM table anchors under their current heading" still covers the table title that comes from the heading above it.

A side effect: the markdown-summary performance measure is no longer recorded when a cached summary is reused. Nothing reads it in the code.

Numbers

Measured in WebKitGTK 2.52 (MiniBrowser, WEBKIT_DISABLE_DMABUF_RENDERER=1), production web build, 6 tabs: README.md, README.zh-CN.md, DESIGN.md, themes/README.md, CHANGELOG.md and the untitled tab. A small script injected into the page clicks each tab 3 times per run, 2 runs per build. After each click it waits two animation frames, then keeps a rAF loop for 1.5 s to find the longest frame and the time the outline list stops changing.

Switch to CHANGELOG.md, one row per change added on top of the previous one:

Build First frames Outline fully shown Longest frame after
v2 427-460 ms 522-554 ms 94-99 ms
+ table count only with the AI panel open 311-367 ms 364-435 ms 52-97 ms
+ summary cache per tab 207-279 ms 216-286 ms 17-18 ms
+ memoized outline titles 163-191 ms 166-220 ms 17-29 ms
this PR, final 159-190 ms 166-200 ms 17-18 ms

Other tabs are short documents and barely change: median 57 ms on v2, 46 ms with this PR.

I can share the script if you want to rerun it.

Testing

🤖 Generated with Claude Code

leemour and others added 2 commits November 24, 2026 19:10
…is closed

Every tab switch re-ran the aiAgentContext memo, which called
editor.getTableAnchors(). That read every heading of the document and
parsed each heading title with remark, even when the AI agent panel was
closed and the table count was never shown. On a long document with
many headings (CHANGELOG.md, ~600 headings) this alone cost tens of ms
per switch.

- Count tables only while the AI agent panel is visible.
- readCodeMirrorTableAnchors reads heading anchors lazily, on the first
  table found, so documents without tables skip the heading parse.

Measured in WebKitGTK 2.52 MiniBrowser (production web build, 6 tabs):
median switch 54 ms -> 42 ms, switch to CHANGELOG.md 430 ms -> 300 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Switching to a long document with many headings rebuilt its outline from
scratch every time: the summary reparsed every heading title with remark,
and every outline row re-rendered its title through react-markdown on each
App render, even while the sidebar was closed.

- Cache the last 16 document summaries by content, so switching back to
  an unchanged document reuses its outline and word count.
- Memoize OutlineTitle on title and titleMarkdown, so outline rows whose
  heading did not change skip the markdown render.

Measured in WebKitGTK 2.52 MiniBrowser (production web build, 6 tabs,
same harness as the previous commit): switch to CHANGELOG.md
~430 ms (v2) -> ~280 ms (previous commit) -> ~165 ms; median switch
~48 ms -> ~38 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 24, 2026

Copy link
Copy Markdown

@leemour is attempting to deploy a commit to the murongg's projects Team on Vercel.

A member of the Team first needs to authorize it.

leemour and others added 4 commits November 24, 2026 21:18
Review follow-up for the outline cache.

The module-level cache keyed by content had FIFO eviction, so a few
edits in one tab pushed the other tabs out, it held old revisions of
large documents, and its state leaked between tests. It also only helped
inside the idle callback of the deferred summary path, so a document over
64k chars still showed an empty outline for a moment after every switch.

- Keep one summary per open tab in a hook ref, reused only while the
  tab content is unchanged, and drop entries of closed tabs.
- On the deferred path, use the cached summary during render and skip
  the idle recompute, so switching back shows the outline at once.
- Test both summary paths: switching back shows that tab's outline
  without a reparse, and editing one tab does not evict the other.
- Test that an outline row re-renders its markdown title only when the
  heading changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… switch

On the deferred summary path, a cache hit after switching back to a tab
skipped the idle recompute but left the deferred summary state keyed to
the previous tab. The first edit then changed the content, the cached
entry no longer matched, and the outline rendered empty until the idle
callback ran.

Store the cached summary under the current document key on a hit, so
the existing keep-previous-while-refreshing logic applies again. The
test now checks the outline right after the edit for both paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review follow-up. The per-tab summary cache matched on exact content, so
switching back to a large tab edited just before leaving it, or one whose
content the editor normalized on the way out, still showed an empty
outline until the idle summary ran. A cache hit on the deferred path also
cost one extra render, because it copied the summary into state.

- Store the document revision with each tab summary. While the revision
  matches, render the tab's last summary even if the content changed,
  the same rule the deferred path already uses while one document is
  edited. The idle callback replaces it.
- Drop the setState on a cache hit. The render reads the cache directly.
- Create the cache Map once instead of on every render.
- The test records every render and fails if any render after a switch
  or edit shows an empty or wrong outline, on both summary paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@murongg
murongg merged commit 7b5f15c into markrahq:v2 Nov 25, 2026
22 of 23 checks passed
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