Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ public static string Render(TimeSpan? duration, bool wrapInParentheses = true, b
return string.Empty;
}

#if NET8_0_OR_GREATER
// Fast path for the common case: duration < 1 hour with no milliseconds.
// string.Create with a stackalloc buffer avoids both the StringBuilder allocation and
// the intermediate strings that GetFormattedPart would otherwise produce per component.
// All progress-frame callers use the defaults (wrapInParentheses=true, showMilliseconds=false).
if (!showMilliseconds && duration.Value.Days == 0 && duration.Value.Hours == 0)
{
int seconds = duration.Value.Seconds;
int minutes = duration.Value.Minutes;
return wrapInParentheses
? (minutes == 0
? string.Create(CultureInfo.InvariantCulture, stackalloc char[8], $"({seconds}s)")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is going to be significantly faster compared to string interpolation. This also seem to add complications without any proven gains.

: string.Create(CultureInfo.InvariantCulture, stackalloc char[12], $"({minutes}m {seconds:D2}s)"))
: (minutes == 0
? string.Create(CultureInfo.InvariantCulture, stackalloc char[6], $"{seconds}s")
: string.Create(CultureInfo.InvariantCulture, stackalloc char[10], $"{minutes}m {seconds:D2}s"));
}
#endif

bool hasParentValue = false;

var stringBuilder = new StringBuilder();
Expand Down
Loading