feat: improve Markdown preview memory use - #2
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR reduces memory retention and improves determinism in the Markdown preview pipeline, while also adding small UX improvements (copyable fenced code blocks) and bounding undo history behavior for large files across the editor.
Changes:
- Add render-generation invalidation and explicit reset semantics to
MarkdownPreviewModel, plus tests to prevent stale async work from mutating newer state. - Downsample oversized preview images (local + remote) to cap decoded pixel memory usage.
- Add copy controls and language labels to fenced code blocks; cap undo history when documents enter large-file modes.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| Tests/TildeMarkdownTests/MarkdownPreviewModelTests.swift | Adds coverage for reset + generation-guarded rendering behavior. |
| Sources/TildeMarkdown/SecureAttachmentLoader.swift | Adds image downsampling/re-encode path to bound decoded image memory for previews. |
| Sources/TildeMarkdown/MarkdownPreviewView.swift | Introduces a copyable code block style with language label support. |
| Sources/TildeMarkdown/MarkdownPreviewModel.swift | Implements generation-based invalidation and reset() to release retained preview state. |
| Sources/TildeEditor/EditorView.swift | Adjusts presentation-state tracking and updates undo levels based on large-file disposition. |
| Sources/TildeEditor/EditorPresentationState.swift | Includes LargeFileDisposition in the presentation state to trigger UI updates when it changes. |
| Sources/TildeEditor/EditorCoordinator.swift | Updates undo levels after edits to react to large-file disposition transitions. |
| Sources/TildeApplication/DocumentRootView.swift | Resets/releases preview snapshot/model when leaving preview or when the document disappears. |
Suppressed comments (1)
Sources/TildeMarkdown/SecureAttachmentLoader.swift:297
- Remote images are capped while downloading (policy.maximumAttachmentBytes), but oversized images can be re-encoded to PNG when downsampled. The resulting rendered.data can exceed policy.maximumAttachmentBytes, bypassing the intended per-attachment byte cap.
let rendered = try PreviewImageDecoder.renderedData(
from: data,
source: source,
pixelWidth: width,
pixelHeight: height
)
return LocalImageAttachment(
data: rendered.data,
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+176
to
+180
| func waitUntilStarted(revision: UInt64) async { | ||
| while !startedRevisions.contains(revision) { | ||
| await Task.yield() | ||
| } | ||
| } |
Comment on lines
+155
to
+160
| let rendered = try PreviewImageDecoder.renderedData( | ||
| from: data, | ||
| source: source, | ||
| pixelWidth: width, | ||
| pixelHeight: height | ||
| ) |
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.
What changed
Why
Markdown previews and large editing sessions could retain more memory than necessary. In-flight preview work could also finish after a reset and mutate newer UI state. These changes bound retained preview/image/undo data and make render-state transitions deterministic.
User impact
Markdown previews use less memory, large images remain practical to display, code blocks can be copied directly, and large-file editing keeps a bounded undo history.
Validation
git diff --checkscripts/verify-host.sh Debug