From 27f04d8a4b9b020eda8148716da858765f3e132b Mon Sep 17 00:00:00 2001 From: v0 Date: Sat, 7 Nov 2026 14:11:57 +0000 Subject: [PATCH 1/2] Optimize parser memory, streaming updates, and tiled text rendering --- .../LTXLabel/TextLayout/LTXTextLayout.swift | 50 +++++-- .../MarkdownParser/MarkdownParser.swift | 79 ++++++----- .../MarkdownTextView+Private.swift | 129 +++++++++++------- .../MarkdownTextView+Update.swift | 3 +- .../MarkdownTextView/MarkdownTextView.swift | 18 ++- .../IncrementalParsingTests.swift | 38 ++++++ .../MarkdownViewTests/PerformanceTests.swift | 80 ++++++----- .../StreamingFastPathTests.swift | 55 ++++++++ .../MarkdownViewTests/TiledLayoutTests.swift | 78 +++++++++++ 9 files changed, 391 insertions(+), 139 deletions(-) create mode 100644 Tests/MarkdownViewTests/TiledLayoutTests.swift diff --git a/Sources/Litext/LTXLabel/TextLayout/LTXTextLayout.swift b/Sources/Litext/LTXLabel/TextLayout/LTXTextLayout.swift index e4c679c..5fdba33 100644 --- a/Sources/Litext/LTXLabel/TextLayout/LTXTextLayout.swift +++ b/Sources/Litext/LTXLabel/TextLayout/LTXTextLayout.swift @@ -45,6 +45,8 @@ public nonisolated class LTXTextLayout: NSObject { private var framesetter: CTFramesetter private var lines: [CTLine]? private var lineOrigins: [CGPoint]? + private var lineBands: [LineBand] = [] + private var hasOrderedLineBands = true private var _highlightRegions: [Int: LTXHighlightRegion] private var cachedSuggestedSize: CGSize? private var cachedSuggestedSizeConstraint: CGSize? @@ -409,6 +411,8 @@ public nonisolated class LTXTextLayout: NSObject { private func generateLayout() { lines = nil lineOrigins = nil + lineBands.removeAll(keepingCapacity: true) + hasOrderedLineBands = true let containerBounds = CGRect( origin: .zero, @@ -433,6 +437,22 @@ public nonisolated class LTXTextLayout: NSObject { var origins = [CGPoint](repeating: .zero, count: frameLines.count) CTFrameGetLineOrigins(ctFrame, CFRange(location: 0, length: 0), &origins) lineOrigins = origins + lineBands.reserveCapacity(frameLines.count) + for (index, line) in frameLines.enumerated() { + var ascent: CGFloat = 0 + var descent: CGFloat = 0 + var leading: CGFloat = 0 + CTLineGetTypographicBounds(line, &ascent, &descent, &leading) + let band = LineBand( + minY: origins[index].y - descent, + maxY: origins[index].y + ascent + leading + ) + if let previous = lineBands.last, + band.minY > previous.minY || band.maxY > previous.maxY { + hasOrderedLineBands = false + } + lineBands.append(band) + } } } } @@ -515,18 +535,26 @@ public nonisolated class LTXTextLayout: NSObject { ) { guard let lines, let lineOrigins else { return } - for i in 0 ..< lines.count { + var start = 0 + if let band, hasOrderedLineBands { + var end = lineBands.count + while start < end { + let middle = start + (end - start) / 2 + if lineBands[middle].minY > band.maxY { + start = middle + 1 + } else { + end = middle + } + } + } + for i in start ..< lines.count { if let band { - let origin = lineOrigins[i] - var ascent: CGFloat = 0 - var descent: CGFloat = 0 - var leading: CGFloat = 0 - _ = CTLineGetTypographicBounds(lines[i], &ascent, &descent, &leading) - // Same vertical extents as `lineRects()`. Origins descend as - // the index grows (CoreText is bottom-up), so once a line falls - // below the band no later line can re-enter it. - if origin.y - descent > band.maxY { continue } - if origin.y + ascent + leading < band.minY { break } + let bounds = lineBands[i] + if bounds.minY > band.maxY { continue } + if bounds.maxY < band.minY { + if hasOrderedLineBands { break } + continue + } } block(lines[i], i, lineOrigins[i]) } diff --git a/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift b/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift index d746c55..9db1d90 100644 --- a/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift +++ b/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift @@ -48,6 +48,7 @@ public class MarkdownParser { return cmark_parser_finish(parser) } } + defer { if let nodes { cmark_node_free(nodes) } } var blocks = dumpBlocks(root: nodes) blocks = finalizeMathBlocks(blocks, mathContext: math) return .init(document: blocks, mathContext: math.contents) @@ -80,6 +81,9 @@ public class MarkdownParser { return ranges } + defer { cmark_node_free(root) } + let sourceLines = SourceLineIndex(markdown) + assert(root.pointee.type == CMARK_NODE_DOCUMENT.rawValue) for block in root.children { let node = block.pointee @@ -89,8 +93,8 @@ public class MarkdownParser { let endLine = Int(node.end_line) let endColumn = Int(node.end_column) - guard let startIndex = getIndex(forLine: startLine, column: startColumn, in: markdown), - let endIndex = getIndex(forLine: endLine, column: endColumn, in: markdown) + guard let startIndex = sourceLines.index(line: startLine, column: startColumn), + let endIndex = sourceLines.index(line: endLine, column: endColumn) else { assertionFailure() continue @@ -160,38 +164,45 @@ public class MarkdownParser { } } -private func getIndex(forLine targetLine: Int, column targetColumn: Int, in text: String) -> String.Index? { - var currentLine = 1 - var lineStartIndex = text.startIndex - - while currentLine < targetLine { - guard let newlineIndex = text[lineStartIndex...].firstIndex(of: "\n") else { - return nil +private struct SourceLineIndex { + let text: String + let lines: [Range] + + init(_ text: String) { + self.text = text + let bytes = text.utf8 + var lines: [Range] = [] + var start = bytes.startIndex + var cursor = start + while cursor < bytes.endIndex { + let byte = bytes[cursor] + if byte == 10 || byte == 13 { + lines.append(start.. maxOffset { - return lineEndIndex - } - - if targetOffset < 0 { - return lineStartIndex + func index(line: Int, column: Int) -> String.Index? { + guard line > 0, line <= lines.count else { return nil } + let range = lines[line - 1] + let bytes = text.utf8 + let offset = min(max(column - 1, 0), bytes.distance(from: range.lowerBound, to: range.upperBound)) + var position = bytes.index(range.lowerBound, offsetBy: offset) + // cmark columns are UTF-8 bytes; inclusive ends can land inside a scalar. + while String.Index(position, within: text) == nil, position > range.lowerBound { + position = bytes.index(before: position) + } + return String.Index(position, within: text) } - - return text.index(lineStartIndex, offsetBy: targetOffset) } private extension MarkdownParser { @@ -360,12 +371,12 @@ private extension MarkdownParser { at boundary: String.Index ) -> [RootBlockRange] { ranges.map { range in - let startOffset = tailMarkdown.distance(from: tailMarkdown.startIndex, to: range.startIndex) - let endOffset = tailMarkdown.distance(from: tailMarkdown.startIndex, to: range.endIndex) + let startOffset = tailMarkdown.utf8.distance(from: tailMarkdown.startIndex, to: range.startIndex) + let endOffset = tailMarkdown.utf8.distance(from: tailMarkdown.startIndex, to: range.endIndex) return .init( type: range.type, - startIndex: fullMarkdown.index(boundary, offsetBy: startOffset), - endIndex: fullMarkdown.index(boundary, offsetBy: endOffset), + startIndex: fullMarkdown.utf8.index(boundary, offsetBy: startOffset), + endIndex: fullMarkdown.utf8.index(boundary, offsetBy: endOffset), outputBlockCount: range.outputBlockCount ) } diff --git a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift index 07c1a1c..916f153 100644 --- a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift +++ b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift @@ -12,14 +12,17 @@ import MarkdownParser extension MarkdownTextView { // Flows through the background preprocessing queue, so both the context and the - // pipeline's update enum are nonisolated (they only carry Sendable payloads). + // pipeline's update enum are nonisolated immutable snapshots. nonisolated struct IncrementalParsingContext { let previousRawMarkdown: String let previousContent: PreprocessedContent let previousRanges: [MarkdownParser.RootBlockRange]? } - nonisolated enum RawMarkdownUpdate { + // Immutable snapshots cross the serial worker boundary. Background processing + // reads AST data and builds new content; UI rendering stays on the main actor. + // Platform colors/images in these snapshots lack checked Sendable conformance. + nonisolated enum RawMarkdownUpdate: @unchecked Sendable { case ready(content: PreprocessedContent, rawMarkdown: String) case parse(markdown: String, theme: MarkdownTheme, incrementalContext: IncrementalParsingContext?) case parsedFull( @@ -91,6 +94,8 @@ extension MarkdownTextView { ) func resetCombine() { + rawPipelineGeneration &+= 1 + pendingRawMarkdown = nil cancellables.forEach { $0.cancel() } cancellables.removeAll() contentPipelineMode = .none @@ -125,57 +130,75 @@ extension MarkdownTextView { } pipeline - .map { [weak self] rawMarkdown -> RawMarkdownUpdate in - let markdown = RawDiffMarkdownNormalizer.normalizeForParsing(rawMarkdown) - guard let self else { - return .parse(markdown: markdown, theme: .default, incrementalContext: nil) - } - if let content = self.makePlainTextAppendFastPath(for: markdown) { - return .ready(content: content, rawMarkdown: markdown) - } - return .parse( - markdown: markdown, - theme: self.theme, - incrementalContext: self.makeIncrementalParsingContext() - ) + .sink { [weak self] markdown in + guard let self else { return } + self.pendingRawMarkdown = markdown + self.processPendingRawMarkdown() } - .receive(on: Self.preprocessingQueue) - // Runs on `preprocessingQueue` (off the main actor), so it must be a - // `nonisolated` function — a main-actor-isolated closure here would trap - // the runtime isolation check when Combine delivers it on that queue. - .map(Self.processOnPreprocessingQueue) - .receive(on: DispatchQueue.main) - .sink { [weak self] update in + .store(in: &cancellables) + } + + private func processPendingRawMarkdown() { + guard !isRawPreprocessing, contentPipelineMode == .raw, + let rawMarkdown = pendingRawMarkdown else { return } + pendingRawMarkdown = nil + let markdown = RawDiffMarkdownNormalizer.normalizeForParsing(rawMarkdown) + if let content = makePlainTextAppendFastPath(for: markdown) { + applyRawMarkdownUpdate(.ready(content: content, rawMarkdown: markdown)) + return + } + let generation = rawPipelineGeneration + let request = RawMarkdownUpdate.parse( + markdown: markdown, + theme: theme, + incrementalContext: makeIncrementalParsingContext() + ) + isRawPreprocessing = true + // Only one job per view is queued. The main actor retains just the latest + // pending input, and reset invalidates results without racing the worker. + Self.preprocessingQueue.async { [weak self] in + let update = Self.processOnPreprocessingQueue(request) + DispatchQueue.main.async { [weak self] in guard let self else { return } - switch update { - case let .ready(content, rawMarkdown): - self.lastRawMarkdown = rawMarkdown - self.lastRootBlockRanges = nil - self.use(content) - case let .parsedFull(result, content, theme, rawMarkdown): - // Complete math rendering on main thread where UI context is available - let finalContent = content.completeMathRendering(parserResult: result, theme: theme) - self.lastRawMarkdown = rawMarkdown - self.lastRootBlockRanges = nil - self.use(finalContent) - case let .parsedIncremental(result, tailContent, context, theme, rawMarkdown): - let completedTailContent = tailContent.completeMathRendering( - parserResult: result.tailResult, - theme: theme - ) - let mergedContent = PreprocessedContent.incrementalMerged( - prefix: context.previousContent, - stablePrefixBlockCount: result.stablePrefixBlockCount, - tail: completedTailContent - ) - self.lastRawMarkdown = rawMarkdown - self.lastRootBlockRanges = result.blockRanges - self.use(mergedContent) - case .parse: - assertionFailure("Unexpected raw markdown parse request on main thread") + self.isRawPreprocessing = false + if self.rawPipelineGeneration == generation, + self.contentPipelineMode == .raw, + self.pendingRawMarkdown == nil { + self.applyRawMarkdownUpdate(update) } + self.processPendingRawMarkdown() } - .store(in: &cancellables) + } + } + + private func applyRawMarkdownUpdate(_ update: RawMarkdownUpdate) { + switch update { + case let .ready(content, rawMarkdown): + lastRawMarkdown = rawMarkdown + lastRootBlockRanges = nil + use(content, layoutImmediately: false) + case let .parsedFull(result, content, theme, rawMarkdown): + // Complete math rendering on main thread where UI context is available + let finalContent = content.completeMathRendering(parserResult: result, theme: theme) + lastRawMarkdown = rawMarkdown + lastRootBlockRanges = nil + use(finalContent, layoutImmediately: false) + case let .parsedIncremental(result, tailContent, context, theme, rawMarkdown): + let completedTailContent = tailContent.completeMathRendering( + parserResult: result.tailResult, + theme: theme + ) + let mergedContent = PreprocessedContent.incrementalMerged( + prefix: context.previousContent, + stablePrefixBlockCount: result.stablePrefixBlockCount, + tail: completedTailContent + ) + lastRawMarkdown = rawMarkdown + lastRootBlockRanges = result.blockRanges + use(mergedContent, layoutImmediately: false) + case .parse: + assertionFailure("Unexpected raw markdown parse request on main thread") + } } private func makeIncrementalParsingContext() -> IncrementalParsingContext? { @@ -276,7 +299,7 @@ extension MarkdownTextView { use(document) } - func use(_ content: PreprocessedContent) { + func use(_ content: PreprocessedContent, layoutImmediately: Bool = true) { assert(Thread.isMainThread) document = content // due to a bug in model gemini-flash @@ -287,9 +310,11 @@ extension MarkdownTextView { invalidateIntrinsicContentSize() #if canImport(UIKit) - layoutIfNeeded() + setNeedsLayout() + if layoutImmediately { layoutIfNeeded() } #elseif canImport(AppKit) - layoutSubtreeIfNeeded() + needsLayout = true + if layoutImmediately { layoutSubtreeIfNeeded() } #endif } } diff --git a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Update.swift b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Update.swift index 9c761a2..6ff68d4 100644 --- a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Update.swift +++ b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Update.swift @@ -110,7 +110,8 @@ extension MarkdownTextView { } } - for goneView in oldViews where !artifacts.subviews.contains(goneView) { + let currentViewIDs = Set(artifacts.subviews.map { ObjectIdentifier($0) }) + for goneView in oldViews where !currentViewIDs.contains(ObjectIdentifier(goneView)) { goneView.removeFromSuperview() } } diff --git a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView.swift b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView.swift index e848103..234af08 100644 --- a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView.swift +++ b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView.swift @@ -49,6 +49,9 @@ enum ContentPipelineMode { let viewProvider: ReusableViewProvider var contentPipelineMode: ContentPipelineMode = .none + var rawPipelineGeneration: UInt64 = 0 + var pendingRawMarkdown: String? + var isRawPreprocessing = false var lastRawMarkdown: String? var lastRootBlockRanges: [MarkdownParser.RootBlockRange]? var lastRenderedBlocks: [MarkdownBlockNode] = [] @@ -201,9 +204,9 @@ enum ContentPipelineMode { contentSubject.send(content) } - /// Sets raw markdown string. Parsing, syntax highlighting, and math rendering - /// are performed on a background queue. Cancels any in-flight preprocessing - /// when new content arrives. + /// Sets raw markdown string. Parsing and highlighting run on a background queue; + /// math rendering stays on the main actor. Pending inputs are coalesced and + /// superseded results are discarded before rendering. public func setMarkdown(string: String) { if contentPipelineMode != .raw { setupRawCombine() @@ -260,6 +263,9 @@ enum ContentPipelineMode { let viewProvider: ReusableViewProvider var contentPipelineMode: ContentPipelineMode = .none + var rawPipelineGeneration: UInt64 = 0 + var pendingRawMarkdown: String? + var isRawPreprocessing = false var lastRawMarkdown: String? var lastRootBlockRanges: [MarkdownParser.RootBlockRange]? var lastRenderedBlocks: [MarkdownBlockNode] = [] @@ -420,9 +426,9 @@ enum ContentPipelineMode { contentSubject.send(content) } - /// Sets raw markdown string. Parsing, syntax highlighting, and math rendering - /// are performed on a background queue. Cancels any in-flight preprocessing - /// when new content arrives. + /// Sets raw markdown string. Parsing and highlighting run on a background queue; + /// math rendering stays on the main actor. Pending inputs are coalesced and + /// superseded results are discarded before rendering. public func setMarkdown(string: String) { if contentPipelineMode != .raw { setupRawCombine() diff --git a/Tests/MarkdownViewTests/IncrementalParsingTests.swift b/Tests/MarkdownViewTests/IncrementalParsingTests.swift index ae22734..615786b 100644 --- a/Tests/MarkdownViewTests/IncrementalParsingTests.swift +++ b/Tests/MarkdownViewTests/IncrementalParsingTests.swift @@ -5,6 +5,44 @@ final class IncrementalParsingTests: XCTestCase { private let parser = MarkdownParser() + func testBlockRangesHandleUnicodeAndLineEndings() { + for newline in ["\n", "\r\n", "\r"] { + let blocks = ["你好", "cafe\u{301}", "終わり"] + let markdown = blocks.joined(separator: newline + newline) + let ranges = parser.parseBlockRange(markdown) + XCTAssertEqual(ranges.count, blocks.count) + for (range, expected) in zip(ranges, blocks) { + XCTAssertEqual(String(markdown[range.startIndex...range.endIndex]), expected) + } + } + } + + func testUnicodeIncrementalRangesMatchFullParsing() throws { + let previous = ["你好", "cafe\u{301}", "第三段", "最後"].joined(separator: "\r\n\r\n") + let new = previous + " extended" + let original = parser.parse(previous) + let result = try XCTUnwrap(parser.parseIncremental( + previousMarkdown: previous, + newMarkdown: new, + previousBlocks: original.document, + previousRanges: parser.parseBlockRange(previous) + )) + XCTAssertEqual(mergedBlocks(from: original, incremental: result), parser.parse(new).document) + XCTAssertEqual( + result.blockRanges.map { String(new[$0.startIndex...$0.endIndex]) }, + parser.parseBlockRange(new).map { String(new[$0.startIndex...$0.endIndex]) } + ) + } + + func testParsedValuesOutliveNativeTrees() { + let markdown = "# Title\n\nA **bold** paragraph.\n\n| A | B |\n|---|---|\n| C | D |" + let expected = parser.parse(markdown).document + for _ in 0..<100 { + XCTAssertEqual(parser.parse(markdown).document, expected) + XCTAssertEqual(parser.parseBlockRange(markdown).count, 3) + } + } + func testParseIncrementalMatchesFullParseForTrailingPlainTextAppend() { let paragraphs = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot"] let previous = paragraphs.joined(separator: "\n\n") diff --git a/Tests/MarkdownViewTests/PerformanceTests.swift b/Tests/MarkdownViewTests/PerformanceTests.swift index 9c9a34d..4fe3901 100644 --- a/Tests/MarkdownViewTests/PerformanceTests.swift +++ b/Tests/MarkdownViewTests/PerformanceTests.swift @@ -17,7 +17,7 @@ final class PerformanceTests: XCTestCase { case 1: lines.append("Paragraph \(i) with **bold** and *italic* and `inline code` and [a link](https://example.com).\n") case 2: - lines.append("```python\nfunc f\(i)() -> Int { return \(i) }\n```\n") + lines.append("```javascript\nfunction f\(i)() { return \(i); }\n```\n") case 3: lines.append("- Item A\(i)\n- Item B\(i)\n- Item C\(i)\n") case 4: @@ -31,28 +31,11 @@ final class PerformanceTests: XCTestCase { return lines.joined(separator: "\n") } - /// Generates a large Python code block. + /// Generates JavaScript covered by the package's default grammar traits. private func generateLargeCode(lineCount: Int) -> String { - var lines: [String] = [] - lines.append("import os") - lines.append("") - for i in 0 ..< lineCount { - switch i % 5 { - case 0: - lines.append("def compute_\(i)(x: int) -> int:") - case 1: - lines.append(" result = x * \(i) + 42") - case 2: - lines.append(" if result <= 0:") - case 3: - lines.append(" return -1") - case 4: - lines.append(" return result") - default: - break - } - } - return lines.joined(separator: "\n") + (0 ..< lineCount).map { + "function compute_\($0)(x) { const result = x * \($0) + 42; return result > 0 ? result : -1; }" + }.joined(separator: "\n") } /// Generates markdown with deeply nested blockquotes containing code blocks. @@ -64,7 +47,7 @@ final class PerformanceTests: XCTestCase { } // Add code blocks at various levels for i in 0 ..< codeBlocksPerLevel { - md += "\n```python\nlet nested\(i) = \(i)\n```\n" + md += "\n```javascript\nlet nested\(i) = \(i)\n```\n" } return md } @@ -165,40 +148,63 @@ final class PerformanceTests: XCTestCase { } } + func testBlockRangeIndexPerformance_1000Blocks() { + let markdown = generateLargeMarkdown(blockCount: 1000) + let parser = MarkdownParser() + measure { + XCTAssertFalse(parser.parseBlockRange(markdown).isEmpty) + } + } + + func testRepeatedParsingMemoryPerformance() { + let markdown = generateLargeMarkdown(blockCount: 100) + let parser = MarkdownParser() + measure(metrics: [XCTMemoryMetric()]) { + for _ in 0..<100 { + autoreleasepool { + _ = parser.parse(markdown) + _ = parser.parseBlockRange(markdown) + } + } + } + } + // MARK: - Code Highlighting Performance func testHighlightPerformance_SmallCode() { let code = generateLargeCode(lineCount: 50) let highlighter = CodeHighlighter.current + XCTAssertFalse(highlighter.highlight(key: nil, content: code, language: "javascript").isEmpty) measure { // Use unique key each iteration to bypass cache let key = Int.random(in: Int.min ... Int.max) - _ = highlighter.highlight(key: key, content: code, language: "python") + _ = highlighter.highlight(key: key, content: code, language: "javascript") } } func testHighlightPerformance_LargeCode() { let code = generateLargeCode(lineCount: 500) let highlighter = CodeHighlighter.current + XCTAssertFalse(highlighter.highlight(key: nil, content: code, language: "javascript").isEmpty) measure { let key = Int.random(in: Int.min ... Int.max) - _ = highlighter.highlight(key: key, content: code, language: "python") + _ = highlighter.highlight(key: key, content: code, language: "javascript") } } func testHighlightCachePerformance() { let code = generateLargeCode(lineCount: 200) let highlighter = CodeHighlighter.current - let key = highlighter.key(for: code, language: "python") + let key = highlighter.key(for: code, language: "javascript") - // Prime the cache - _ = highlighter.highlight(key: key, content: code, language: "python") + // Prime the cache and verify this is not an unsupported-language fallback. + XCTAssertFalse(highlighter.highlight(key: key, content: code, language: "javascript").isEmpty) measure { // All iterations should be cache hits - _ = highlighter.highlight(key: key, content: code, language: "python") + _ = highlighter.highlight(key: key, content: code, language: "javascript") } } @@ -207,7 +213,7 @@ final class PerformanceTests: XCTestCase { func testPreprocessedContentPerformance_ManyCodeBlocks() { var md = "" for i in 0 ..< 50 { - md += "Paragraph \(i).\n\n```python\nlet v\(i) = \(i)\n```\n\n" + md += "Paragraph \(i).\n\n```javascript\nlet v\(i) = \(i)\n```\n\n" } let parser = MarkdownParser() let result = parser.parse(md) @@ -254,7 +260,7 @@ final class PerformanceTests: XCTestCase { measure { for code in codes { - _ = highlighter.key(for: code, language: "python") + _ = highlighter.key(for: code, language: "javascript") } } } @@ -328,10 +334,12 @@ final class PerformanceTests: XCTestCase { let previous = paragraphs.joined(separator: "\n\n") let stableTail = previous + " extended" - measure { - let view = makeRawBenchmarkView() + let view = makeRawBenchmarkView() + measureMetrics([.wallClockTime], automaticallyStartMeasuring: false) { sendRawMarkdownAndWait(previous, to: view) + startMeasuring() sendRawMarkdownAndWait(stableTail, to: view) + stopMeasuring() } } @@ -340,10 +348,12 @@ final class PerformanceTests: XCTestCase { let previous = paragraphs.joined(separator: "\n\n") let openFenceTail = previous + "\n\n```swift\nlet streamed = 1" - measure { - let view = makeRawBenchmarkView() + let view = makeRawBenchmarkView() + measureMetrics([.wallClockTime], automaticallyStartMeasuring: false) { sendRawMarkdownAndWait(previous, to: view) + startMeasuring() sendRawMarkdownAndWait(openFenceTail, to: view) + stopMeasuring() } } diff --git a/Tests/MarkdownViewTests/StreamingFastPathTests.swift b/Tests/MarkdownViewTests/StreamingFastPathTests.swift index c749c1d..f038f25 100644 --- a/Tests/MarkdownViewTests/StreamingFastPathTests.swift +++ b/Tests/MarkdownViewTests/StreamingFastPathTests.swift @@ -7,6 +7,61 @@ final class StreamingFastPathTests: XCTestCase { private let parser = MarkdownParser() + func testBurstRetainsOnlyLatestPendingInput() { + let view = MarkdownTextView() + view.throttleInterval = nil + view.setMarkdown(string: "First **input**") + for index in 0..<100 { + view.setMarkdown(string: "Latest **input \(index)**") + } + XCTAssertTrue(view.isRawPreprocessing) + XCTAssertEqual(view.pendingRawMarkdown, "Latest **input 99**") + waitForPreprocessing(view) + XCTAssertEqual(view.lastRawMarkdown, "Latest **input 99**") + XCTAssertEqual(view.document.blocks, parser.parse("Latest **input 99**").document) + } + + func testResetDiscardsInFlightResult() { + let view = MarkdownTextView() + view.throttleInterval = nil + view.setMarkdown(string: "Stale **content**") + view.reset() + waitForPreprocessing(view) + XCTAssertNil(view.lastRawMarkdown) + XCTAssertTrue(view.document.blocks.isEmpty) + } + + func testManualContentSurvivesInFlightResult() { + let view = MarkdownTextView() + view.throttleInterval = nil + view.setMarkdown(string: "Stale **content**") + let manual = MarkdownTextView.PreprocessedContent(markdown: "Replacement", theme: .default) + view.setMarkdownManually(manual) + waitForPreprocessing(view) + XCTAssertEqual(view.document.blocks, manual.blocks) + XCTAssertNil(view.lastRawMarkdown) + } + + func testRawPipelineCanRestartWhileOldWorkFinishes() { + let view = MarkdownTextView() + view.throttleInterval = nil + view.setMarkdown(string: "Stale **content**") + view.reset() + view.setMarkdown(string: "New **content**") + waitForPreprocessing(view) + XCTAssertEqual(view.lastRawMarkdown, "New **content**") + XCTAssertEqual(view.document.blocks, parser.parse("New **content**").document) + } + + private func waitForPreprocessing(_ view: MarkdownTextView) { + let deadline = Date().addingTimeInterval(5) + while (view.isRawPreprocessing || view.pendingRawMarkdown != nil), Date() < deadline { + _ = RunLoop.main.run(mode: .default, before: Date().addingTimeInterval(0.001)) + } + XCTAssertFalse(view.isRawPreprocessing) + XCTAssertNil(view.pendingRawMarkdown) + } + func testPlainTextAppendFastPathUpdatesTrailingParagraph() { let result = parser.parse("Hello") let content = MarkdownTextView.PreprocessedContent( diff --git a/Tests/MarkdownViewTests/TiledLayoutTests.swift b/Tests/MarkdownViewTests/TiledLayoutTests.swift new file mode 100644 index 0000000..4b0d931 --- /dev/null +++ b/Tests/MarkdownViewTests/TiledLayoutTests.swift @@ -0,0 +1,78 @@ +import CoreGraphics +import CoreText +import Foundation +import XCTest +@testable import Litext + +final class TiledLayoutTests: XCTestCase { + private let size = CGSize(width: 320, height: 6000) + + private func makeLayout() -> LTXTextLayout { + let text = NSMutableAttributedString(string: "") + for index in 0..<180 { + let font = CTFontCreateWithName("Helvetica" as CFString, index % 7 == 0 ? 22 : 14, nil) + text.append(NSAttributedString( + string: "Line \(index): mixed size text\n", + attributes: [NSAttributedString.Key(kCTFontAttributeName as String): font] + )) + } + let layout = LTXTextLayout(attributedString: text) + layout.containerSize = size + return layout + } + + private func makeContext() throws -> CGContext { + try XCTUnwrap(CGContext( + data: nil, + width: Int(size.width), + height: Int(size.height), + bitsPerComponent: 8, + bytesPerRow: Int(size.width) * 4, + space: CGColorSpaceCreateDeviceRGB(), + bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue + )) + } + + private func pixels(_ context: CGContext) throws -> Data { + Data(bytes: try XCTUnwrap(context.data), count: context.bytesPerRow * context.height) + } + + func testVisibleBandsMatchClippedFullDrawing() throws { + let layout = makeLayout() + for y in [CGFloat(0), 500, 1800, 3200, 5900] { + let band = CGRect(x: 0, y: y, width: size.width, height: 180) + let full = try makeContext() + full.clip(to: band) + layout.draw(in: full) + let tiled = try makeContext() + tiled.clip(to: band) + layout.draw(in: tiled, visibleRect: band) + XCTAssertEqual(try pixels(full), try pixels(tiled), "Drawing differs at y=\(y)") + } + } + + func testChangingContainerRebuildsVisibleLineIndex() throws { + let layout = makeLayout() + layout.containerSize = CGSize(width: 160, height: size.height) + let band = CGRect(x: 0, y: 3200, width: 160, height: 180) + let full = try makeContext() + full.clip(to: band) + layout.draw(in: full) + let tiled = try makeContext() + tiled.clip(to: band) + layout.draw(in: tiled, visibleRect: band) + XCTAssertEqual(try pixels(full), try pixels(tiled)) + } + + func testLowerDocumentTileDrawingPerformance() throws { + let layout = makeLayout() + let context = try makeContext() + let band = CGRect(x: 0, y: 3200, width: size.width, height: 180) + context.clip(to: band) + measure { + for _ in 0..<100 { + layout.draw(in: context, visibleRect: band) + } + } + } +} From b8053cd0f61076627e812d00a8d43c2664fbaabb Mon Sep 17 00:00:00 2001 From: v0 Date: Sat, 7 Nov 2026 18:42:44 +0000 Subject: [PATCH 2/2] Prevent rendering starvation during continuous markdown streaming --- .../MarkdownTextView+Private.swift | 5 +-- .../MarkdownTextView/MarkdownTextView.swift | 8 ++--- .../StreamingFastPathTests.swift | 35 +++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift index 916f153..349d16f 100644 --- a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift +++ b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift @@ -162,8 +162,9 @@ extension MarkdownTextView { guard let self else { return } self.isRawPreprocessing = false if self.rawPipelineGeneration == generation, - self.contentPipelineMode == .raw, - self.pendingRawMarkdown == nil { + self.contentPipelineMode == .raw { + // Publish progress even while newer input is pending; otherwise + // continuous streams can starve rendering until input stops. self.applyRawMarkdownUpdate(update) } self.processPendingRawMarkdown() diff --git a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView.swift b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView.swift index 234af08..2837a5c 100644 --- a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView.swift +++ b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView.swift @@ -205,8 +205,8 @@ enum ContentPipelineMode { } /// Sets raw markdown string. Parsing and highlighting run on a background queue; - /// math rendering stays on the main actor. Pending inputs are coalesced and - /// superseded results are discarded before rendering. + /// math rendering stays on the main actor. Pending inputs are coalesced while + /// completed parses render progressively. Reset or mode changes invalidate results. public func setMarkdown(string: String) { if contentPipelineMode != .raw { setupRawCombine() @@ -427,8 +427,8 @@ enum ContentPipelineMode { } /// Sets raw markdown string. Parsing and highlighting run on a background queue; - /// math rendering stays on the main actor. Pending inputs are coalesced and - /// superseded results are discarded before rendering. + /// math rendering stays on the main actor. Pending inputs are coalesced while + /// completed parses render progressively. Reset or mode changes invalidate results. public func setMarkdown(string: String) { if contentPipelineMode != .raw { setupRawCombine() diff --git a/Tests/MarkdownViewTests/StreamingFastPathTests.swift b/Tests/MarkdownViewTests/StreamingFastPathTests.swift index f038f25..e470608 100644 --- a/Tests/MarkdownViewTests/StreamingFastPathTests.swift +++ b/Tests/MarkdownViewTests/StreamingFastPathTests.swift @@ -21,6 +21,41 @@ final class StreamingFastPathTests: XCTestCase { XCTAssertEqual(view.document.blocks, parser.parse("Latest **input 99**").document) } + func testCompletedParseRendersWhileNewerInputIsQueued() { + let view = MarkdownTextView() + view.throttleInterval = nil + let first = "First **input**" + let latest = "Latest **input 99**" + view.setMarkdown(string: first) + + // The serial queue finishes the first parse but cannot start the next one + // until released. This makes intermediate progress deterministic, independent + // of parser speed, rather than accidentally observing only the final result. + let releaseNextParse = DispatchSemaphore(value: 0) + MarkdownTextView.preprocessingQueue.async { + releaseNextParse.wait() + } + defer { releaseNextParse.signal() } + for index in 0..<100 { + view.setMarkdown(string: "Latest **input \(index)**") + } + XCTAssertEqual(view.pendingRawMarkdown, latest) + + let deadline = Date().addingTimeInterval(5) + while view.pendingRawMarkdown != nil, Date() < deadline { + _ = RunLoop.main.run(mode: .default, before: Date().addingTimeInterval(0.001)) + } + XCTAssertNil(view.pendingRawMarkdown, "The completed parse must schedule the latest input") + XCTAssertTrue(view.isRawPreprocessing) + XCTAssertEqual(view.lastRawMarkdown, first, "Progress must render before the stream becomes idle") + XCTAssertEqual(view.document.blocks, parser.parse(first).document) + + releaseNextParse.signal() + waitForPreprocessing(view) + XCTAssertEqual(view.lastRawMarkdown, latest) + XCTAssertEqual(view.document.blocks, parser.parse(latest).document) + } + func testResetDiscardsInFlightResult() { let view = MarkdownTextView() view.throttleInterval = nil