Reuse a TimeCode instance in TimeSpanToDisplayFullConverter - #13221
Merged
niksedk merged 3 commits intoAug 5, 2026
Conversation
Avoid allocating a new TimeCode on every Convert call by reusing a private instance, and return cached constants for the zero-time fallbacks. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Member
Author
…onvertBack Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes TimeSpanToDisplayFullConverter to reduce steady-state allocations/GC pressure during UI grid rendering by reusing formatting state and switching parsing to span-based tokenization.
Changes:
- Reuse a single
TimeCodeinstance duringConvertto avoid per-callTimeCodeallocations. - Cache zero-time fallback strings for frame/time mode.
- Refactor
ConvertBackparsing to useSpan.SplitAnyand unify frame/time parsing paths.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Merged
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.

Summary
TimeCodeinstance inTimeSpanToDisplayFullConverter.Convertinstead of allocating a new one on every callConvertBackinput with allocation-freeSpan.SplitAnyinstead ofstring.Split, collapsing the duplicated frame-mode/time-mode branches into oneBenchmark: Convert
BenchmarkDotNet (ShortRun, .NET 10.0.10, X64 RyuJIT AVX2, i7-13700), mirroring the converter's old vs new formatting path:
new TimeCode(ts).ToString()ToString()new TimeCode(ts).ToHHMMSSFF()ToHHMMSSFF()The change removes the 24 B
TimeCodeallocation per call (the remaining 48 B is the result string itself); the time delta is minor. Note the converter is only used from Avalonia bindings on the UI thread, so the shared mutable instance is safe there.Benchmark: ConvertBack
Same setup, old
string.Splitparsing vs the new span-basedSplitAny:00:01:23,456(ms mode)00:01:23,456(frame mode)01:02:03.12not a time(invalid)~1.8× faster; the 184 B saved per call is the
string[]plus the four substring allocations fromSplit. The remaining 24 B is theTimeSpanbox forced byIValueConverterreturningobject, paid identically in both versions.Test plan
CurrentVideoOffsetInMs≠ 0) and verify displayed times include the offset in both time and frame mode00:00:00,000(time mode) /00:00:00.00(frame mode)00:00:00,000🤖 Generated with Claude Code