Repository navigation
Keep a formula's image while its LaTeX streams, and draw each formula once - #35
Merged
Merged
Conversation
The image store kept every formula that the math renderer drew, and never removed one. A long conversation with many formulas used more memory over time. Now a TranscriptMathCache keeps the drawn formulas. It removes the least recently used formulas when they use more than a byte limit (16 MB by default). A cache calls the renderer once for each formula and font size. Stores that wait for the same formula get the same result. A formula that the renderer cannot draw is kept as a failure, so that the renderer is not called for it again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An app can close an open display formula on each snapshot, so the LaTeX of one formula changes on each update. Each change started a new render, and the formula showed its LaTeX until the new image arrived. Now the store knows each formula by its slot: the block's id, and how many formulas come before it in the block. This works like the code highlighter, which keeps the last colors of a block while its code grows: - While the new LaTeX is drawn, the slot shows its last image. - A slot draws one formula at a time. Newer LaTeX replaces the LaTeX that waits for its turn. - The last image is shown only for LaTeX that can be an earlier version of the new LaTeX: the same kind of formula, and a shared start of at least half of it. Documents that are parsed with the same id prefix have the same block ids, so a view can get another document's formula in a slot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A formula showed its LaTeX in the code font until the renderer drew it. Then the image replaced the LaTeX, and the text moved. Now a formula that is being drawn, and has no last image, is empty space. Its size comes from its LaTeX: about 0.55 em for each symbol, one line tall for inline math, and for display math one line for each row, two lines when it has a fraction, a sum or an integral. Nothing is drawn in the space. Copy and VoiceOver still give the LaTeX. A formula shows its LaTeX as written only when there is no renderer, or when the renderer returned nil for it. A nil result now lays the formula out again, so that its LaTeX replaces the empty space. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each picture, code color or formula that arrived called the view's change handler. The view then laid out the whole document on the main thread, one time for each arrival. A message with ten formulas laid out ten times when they arrived. Now the store calls the handler on the main queue. Arrivals before the call runs share it, so the view lays out once for all of them. An arrival after the call starts gets a call of its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A page with many message views can show the same formula in many of them. Each view had its own formulas, so each view drew the formula again. Now each TranscriptTextView keeps its formulas in TranscriptMathCache.shared by default, and draws a formula only if no view that shares the cache has drawn it. An app can give a view its own cache with `mathCache`, or in SwiftUI with `.mathRenderer(_:cache:)`. Views that share a cache must have renderers that draw a formula the same way. The README and the doc comments of the renderer now describe the empty space, the last image while LaTeX changes, and the cache. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A slot kept only the key of its last formula. When the cache removed a formula that was on screen, the next layout found no image, showed empty space and drew the formula again. With more formulas on screen than the cache holds, each arrival removed another formula, and the view never settled. Now each slot keeps its last image too. A formula that the cache removed shows that image, and is not drawn again. The store keeps the 1024 slots that it used last, so that a view that shows one document after another does not keep all of their formulas. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A renderer can return nil for LaTeX that is not complete yet. The formula now keeps its last image or its empty space. It shows its LaTeX only when that LaTeX stays the same for one second after the failure. The cache keeps only images. Each view keeps its failures per formula. removeAll() now lays out the views that use the cache again, and a draw from before removeAll() is not kept or joined. A store keeps the slots of the document that it lays out, with no fixed count. Older draws do not replace newer images, and newer LaTeX that is already drawn drops the LaTeX that waits. The slot of a formula is found with a binary search. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LayoutEngine: the engine makes the pending work and gives it to the image store with the shared formula cache. ImageStore: one init takes the formula cache, the pending work, deliver and settle. ImageStore: the setOnChange doc keeps the main queue text and the rule for the work count. ImageStore: the formula code comes from math-image-reuse, and a formula counts as work until its result is handled. ImageStore: each change counts as work until onChange has run on main. ImageStore: a formula that fails counts as work until it shows its LaTeX, and the docs and the README say this. TranscriptTextView: the three call sites use the one imagesChanged handler, which lays out on main at once. TranscriptTextView: the view keeps both mathCache and contentSettled(). ContentSettledTests: the formula test gives a slot, and two new tests check the change and failure counts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Part 5 of a stack of 13 pull requests. The base is #34 (
feature/soft-line-breaks). #31 (part 1) tells how to merge the stack.On this stacked branch,
swift testpasses (246 tests), and the iOS build and build-for-testing pass.Summary
The LaTeX of a formula can change on each update while it streams in, and the renderer needs time to draw each version. Before this change, the formula changed between its LaTeX source and its image. Each view drew its own formulas, so a page with many message views drew one formula many times. Each image that arrived caused one more layout.
TranscriptMathCache. It keeps drawn formulas up to a byte limit (16 MB by default), and removes the least recently used ones. It calls the renderer once for each formula and font size, also when several views wait for it.removeAll()empties it and lays out again the views that use it.TranscriptTextView.mathCacheand thecache:parameter ofTranscriptText.mathRenderer(_:cache:). Both default toTranscriptMathCache.shared. Existing calls compile without changes.\frac{a}{. The LaTeX shows as written only when it stays the same for one second.Tests
swift teston macOS: 200 tests pass. 22 new tests, one of which replaces an old test, cover the eviction, shared draws, the empty space, nil results,removeAll()and one change call for arrivals together.Checked on an iPhone 12 mini
iOS 27.0, in a check screen in a local test app. The test renderer draws boxes, not real formulas, and takes 150 ms for each one. The first runs were on main. Main showed each problem, before and after the branch runs. The numbers come from the last runs of each build.
TranscriptTextrows in aLazyVStack, scrolled down and back: main calls the renderer 158 times, and the branch 2 times.Stand-ins: the box renderer,
scrollPositionin place of a finger, and a count of installed layouts in place of a count of layouts.Not checked
removeAll(), on a device.mathCachethat is not the default, shared by several views.Known limits
🤖 Generated with Claude Code