Skip to content

Keep a formula's image while its LaTeX streams, and draw each formula once - #35

Merged
servercimen merged 9 commits into
feature/soft-line-breaksfrom
feature/math-image-reuse
Nov 24, 2026
Merged

servercimen merged 9 commits into
feature/soft-line-breaksfrom
feature/math-image-reuse

Conversation

@servercimen

Copy link
Copy Markdown
Owner

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 test passes (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.

  • Add 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.
  • Add TranscriptTextView.mathCache and the cache: parameter of TranscriptText.mathRenderer(_:cache:). Both default to TranscriptMathCache.shared. Existing calls compile without changes.
  • Keep the last image of a formula while its LaTeX changes. Each formula draws one version at a time, and only the newest LaTeX waits.
  • Leave empty space of about the size of the formula until its first image arrives. Copy and VoiceOver still give the LaTeX.
  • Keep the last image or the empty space when the renderer returns nil, for example for \frac{a}{. The LaTeX shows as written only when it stays the same for one second.
  • Keep the images of the formulas on screen in the view, so a formula that the cache removed is not drawn again.
  • Call the change handler once on the main queue for images, code colors and formulas that arrive together.
  • Update the README and the doc comments.

Tests

  • swift test on 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.
  • iOS: a local test app builds with this branch for an iPhone (Release), with no warnings in the package sources.

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.

  • One display formula that streams in: main shows LaTeX source in 101 to 103 of about 150 frames, and calls the renderer 29 times. The branch shows no source. It shows empty space for 150 ms, then one box that grows, and calls the renderer 13 times.
  • The same formula in bursts of 6 updates: main changes from box to source 6 times. The branch does this 0 times.
  • A renderer that returns nil while braces are open: main shows source the whole time. The branch shows no source. When the final LaTeX cannot be drawn, the branch keeps the last box, and shows the source 1.3 s after the last update.
  • 50 TranscriptText rows in a LazyVStack, scrolled down and back: main calls the renderer 158 times, and the branch 2 times.
  • 20 inline formulas: main lays out 19 to 20 more times than without a renderer. The branch lays out 1 more time in 16 of 18 runs, and 2 and 5 times in the others. In the run with 5, the images arrived in 5 turns of the main queue.
  • Formulas that are being drawn: main shows source chips, and the branch shows only the background. On both builds, the accessibility label and Copy give the LaTeX.
  • The empty space is an estimate. With the box renderer, it was narrower than the box (26 pt against 54 pt), so the text after it moved one time.

Stand-ins: the box renderer, scrollPosition in place of a finger, and a count of installed layouts in place of a count of layouts.

Not checked

  • Eviction at the byte limit, and removeAll(), on a device.
  • A mathCache that is not the default, shared by several views.
  • A renderer that draws real formulas.

Known limits

  • Views share one math cache by default. Two views with different renderers can show the image that the other renderer drew. An app with more than one renderer gives each one a cache of its own.

🤖 Generated with Claude Code

servercimen and others added 8 commits November 23, 2026 17:10
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>
@servercimen
servercimen added this pull request to stack #44 November 24, 2026 20:57
@servercimen
servercimen removed this pull request from stack #44 November 24, 2026 21:01
@servercimen
servercimen added this pull request to stack #45 November 24, 2026 21:01
@servercimen
servercimen merged commit 3a888d9 into main Nov 24, 2026
0 of 3 checks passed
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