Repository navigation
Fix preview hang on large documents with many code/math regions - #423
Open
KnockoutKiss wants to merge 3 commits into
Open
KnockoutKiss wants to merge 3 commits into
KnockoutKiss wants to merge 3 commits into
Conversation
…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.
Author
|
Added a related commit:
|
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.
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 insideNSDocument showWindows→ SwiftUI layout →PreviewView→ repeatedreplacingOccurrencescalls over the whole document.Cause
Every inline post-processor (
processMath,processWikilinks,processHighlightMarks,processSuperSub,processEmoji) wraps its work inprotectCodeRegions/restoreProtectedSegments:protectCodeRegionscalledreplaceSubrangeon the full HTML string once per match, copying the document each time.restoreProtectedSegmentscalledreplacingOccurrences(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:
protectCodeRegionsappends text between matches plus a token, instead of mutating in place.restoreProtectedSegmentsscans 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:==mark==inside code stays untouched