Memoize CharactersPerSecond and PixelWidth in SubtitleLineViewModel (grid binding perf)#11675
Closed
niksedk wants to merge 1 commit into
Closed
Memoize CharactersPerSecond and PixelWidth in SubtitleLineViewModel (grid binding perf)#11675niksedk wants to merge 1 commit into
niksedk wants to merge 1 commit into
Conversation
Both getters are bound directly into subtitle-grid cells and Avalonia re-evaluates them on every cell repaint: - CharactersPerSecond is hit multiple times per row repaint (CPS cell text, CpsBackgroundBrush, and the DurationBackgroundBrush SE4 fallback), each running SubtitleTextInfoHelper.GetCharactersPerSecond. - PixelWidth shapes text with SkiaSharp (SKShaper.Shape) and is read both by the pixel-width column and by TextBackgroundBrush. Add value-keyed memoization (CPS keyed on Text/StartTime/EndTime; PixelWidth keyed on Text + the width font name/size) so repeated reads with unchanged inputs reuse the result. The keys are the actual inputs, so the cache self-invalidates without relying on PropertyChanged ordering; behavior is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <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.
Optimizes two hot per-row bindings in the subtitle grid. Both getters on
SubtitleLineViewModelare bound straight into grid cells, and Avalonia re-evaluates them on every cell repaint.CharactersPerSecondis hit several times per row repaint — the CPS cell text binding,CpsBackgroundBrush, and theDurationBackgroundBrushSE4 fallback all read it, each runningSubtitleTextInfoHelper.GetCharactersPerSecond.PixelWidthshapes text with SkiaSharp (SKShaper.Shape) on every read and is consumed both by the pixel-width column and byTextBackgroundBrush's "too wide" check.Change
Add value-keyed memoization:
Text/StartTime/EndTimeText+ the configured width font name/sizeThe cache keys are the actual computation inputs, so it self-invalidates when any input changes — no reliance on
PropertyChangedordering, and behavior is identical. Repeated reads with unchanged inputs (the common case during repaint/scroll) reuse the result instead of recomputing.🤖 Generated with Claude Code