Repository navigation
Cache text layouts between frames - #29
Open
wtholliday wants to merge 2 commits into
Open
wtholliday wants to merge 2 commits into
wtholliday wants to merge 2 commits into
Conversation
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>
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
text,text_bounds,glyph_positionsandline_metricsused 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.text()found each glyph's character withtext.chars().nth(i), which is quadratic in the string's length. It now uses the glyph'sparentfield.(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::appendandFont::metrics_rawwere 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
processandupdate, then an offscreen render. The rui build here includes audulus/rui#85, #86 and #87.fontdue no longer shows up in the profile. Most of what remains is wgpu's
queue_write_buffercopying 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).text,text_small,text_scale,text_box,text_box_scissorand the atlases) are byte-identical tomain's. Onlypath_fillandsegment_stroke_stressdiffer, and they draw random geometry withthread_rng, so they differ on every run.For reviewers
f057b15) on the branchtext-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.Stringand two smallVecs per distinct piece of text drawn or measured in the last two frames.🤖 Generated with Claude Code