Repository navigation
perf(app): faster tab switching for documents with many headings - #801
Merged
Merged
Conversation
…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>
|
@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. |
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>
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.
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:
aiAgentContextcallededitor.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.react-markdownagain on every drawer render, and the drawer re-renders several times per switch.Changes
App.tsx:aiAgentContextcounts 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:readCodeMirrorTableAnchorsreads 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:OutlineTitleis memoized ontitleandtitleMarkdown. 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.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 onv2.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.tscase "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-summaryperformance 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:
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
pnpm --filter @markra/app test: 1671 passedpnpm --filter @markra/editor test: 685 passedtypecheck:testclean for app and editor🤖 Generated with Claude Code