Skip to content

Cache text layouts between frames - #29

Open
wtholliday wants to merge 2 commits into
mainfrom
text-layout-cache
Open

wtholliday wants to merge 2 commits into
mainfrom
text-layout-cache

Conversation

@wtholliday

Copy link
Copy Markdown
Contributor

What changed

  • Text layout cache: text, text_bounds, glyph_positions and line_metrics used to run a fresh fontdue layout on every call. Layouts are now cached by string, size, maximum width and device pixel ratio (src/text_layout.rs). A layout stays cached as long as some frame uses it: begin() drops the ones the last frame didn't use, so the cache holds about two frames' worth of text. On a key-hash match, the stored string and parameters are compared, so a collision only costs a recompute.
  • Glyph lookup: text() found each glyph's character with text.chars().nth(i), which is quadratic in the string's length. It now uses the glyph's parent field.
  • Faster hashing (second commit): once layouts were cached, SipHash over each string and each (char, size) glyph-cache key became the next cost. Both caches now use an FxHash-style hasher (src/fxhash.rs), and the layout map, whose keys are already hashes, passes them through. These caches are private to vger, so their keys never come from untrusted input.

Why

In rui, an app that redraws the same labels every frame was laying all of them out every frame. In an offscreen benchmark of the gui-bench dashboard at 3,000 order rows, fontdue's Layout::append and Font::metrics_raw were the top entries in the profile, about a third of the time to handle a pointer move.

Results

Median time per pointer move between two rows of the gui-bench dashboard, over 200 moves on macOS. It runs rui's process and update, then an offscreen render. The rui build here includes audulus/rui#85, #86 and #87.

Rows Before With this PR
300 6.48 ms 5.74 ms
1,000 11.92 ms 8.45 ms
3,000 31.09 ms 19.82 ms

fontdue no longer shows up in the profile. Most of what remains is wgpu's queue_write_buffer copying vger's buffers each frame.

Testing

  • cargo test: the 22 render tests pass, plus 3 new unit tests for the cache (reuse, the key's parts, and eviction across frames).
  • All 8 text test images (text, text_small, text_scale, text_box, text_box_scissor and the atlases) are byte-identical to main's. Only path_fill and segment_stroke_stress differ, and they draw random geometry with thread_rng, so they differ on every run.
  • In rui, 16 scripted frames of hovers and clicks match rendering with the published vger 0.3.0 exactly.

For reviewers

  • rui still depends on vger 0.3 (wgpu 0.20). The same two commits apply unchanged on top of 0.3.0 (f057b15) on the branch text-layout-cache-0.3, which is what the numbers above were measured with. Publishing a 0.3.1 from that branch would let rui use this without moving to wgpu 27.
  • The cache adds a String and two small Vecs per distinct piece of text drawn or measured in the last two frames.

🤖 Generated with Claude Code

wtholliday and others added 2 commits November 25, 2026 18:54
text, text_bounds, glyph_positions and line_metrics laid the string
out with fontdue on every call, so UIs that draw the same labels every
frame paid for layout every frame. In a rui app with a few hundred
visible labels, fontdue's layout was about a third of the frame.

Layouts are now kept by string, size, maximum width and device pixel
ratio, and reused while they're used at least once per frame: begin()
drops the ones the last frame didn't use.

text() also took each glyph's char with text.chars().nth(i), which is
quadratic in the string's length; it now uses the glyph's parent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With layouts cached, hashing became the larger cost of drawing text:
SipHash over each string for the layout cache, and over each (char,
size) key in the glyph cache. Both caches now use an FxHash-style
hasher, and the layout map, whose keys are already hashes, passes them
through. These keys never come from untrusted input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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