Skip to content

Fix preview hang on large documents with many code/math regions - #423

Open
KnockoutKiss wants to merge 3 commits into
Shpigford:mainfrom
KnockoutKiss:fix/large-doc-preview-hang
Open

KnockoutKiss wants to merge 3 commits into
Shpigford:mainfrom
KnockoutKiss:fix/large-doc-preview-hang

Conversation

@KnockoutKiss

@KnockoutKiss KnockoutKiss commented Oct 31, 2026 •

Copy link
Copy Markdown

Problem

Opening a large document in preview mode hangs the main thread before the window appears. The app shows in the Dock but no window opens and it stops responding.

Reproduced with a 17 MB, ~9,300-line UTF-8 Arabic Markdown file (defaultViewMode = preview, Clearly 3.3.0, macOS 27.0.1). The macOS hang report shows the main thread inside NSDocument showWindows → SwiftUI layout → PreviewView → repeated replacingOccurrences calls over the whole document.

Cause

Every inline post-processor (processMath, processWikilinks, processHighlightMarks, processSuperSub, processEmoji) wraps its work in protectCodeRegions / restoreProtectedSegments:

  • protectCodeRegions called replaceSubrange on the full HTML string once per match, copying the document each time.
  • restoreProtectedSegments called replacingOccurrences(of:with:) once per segment, rescanning the entire document for each token (non-literal Unicode comparison).

Cost grows as segments × document length, repeated five times per render. The file above produces ~540 protected code/math regions, so that is thousands of full passes over ~10M characters.

Fix

Both functions now build their output in a single forward pass:

  • protectCodeRegions appends text between matches plus a token, instead of mutating in place.
  • restoreProtectedSegments scans for __CLEARLY_PROTECTED_CODE_(\d+)__ tokens once with a cached regex and splices the segments in.

Token format and output are unchanged. I checked equivalence by porting the old and new algorithms to a script and running both against the real document's rendered HTML (539 regions, plus a post-processor pass in between): output was byte-for-byte identical.

Tests

Added MarkdownProtectedRegionTests:

  • 2,000 code spans + highlights round-trip with no leaked tokens (covers multi-digit token indexes like 1/10/100/1000)
  • ==mark== inside code stays untouched
  • ~5 MB Arabic document with 1,000 code spans renders within a generous time bound

…ions

protectCodeRegions() mutated the HTML in place once per match, and restoreProtectedSegments() called replacingOccurrences once per segment, rescanning the whole document each time. Both run inside every inline post-processor, so rendering cost grew as (regions x document length).

Both functions now build their output in a single forward pass. Output is unchanged.
@KnockoutKiss

Copy link
Copy Markdown
Author

Added a related commit: PreviewView.makeNSView called loadHTML unconditionally, so a document opening in Edit mode (defaultViewMode = edit) still rendered the hidden preview and hit the same hang before the window appeared. A second hang report confirmed the identical stack with Edit as the default.

makeNSView now renders only when the window opens in Preview. In Edit mode, lastContentKey stays nil, so updateNSView renders on the first switch to Preview, matching the existing "skip expensive content rendering when preview is hidden" logic.

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