Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Improve DateTime{Offset} "r" and "o" formatting performance (#17092)
Browse files Browse the repository at this point in the history
Two main changes:
1. Rewrote the formatting to use span, only to then discover that we already had almost exactly the same implementation in Utf8Formatter.  As that one had some extra optimizations around JIT behaviors, I ported that over instead.
2. Avoided [ThreadStatic] lookups unless necessary.

ToString/TryFormat for "o"/"O" improve by ~2.5x.

ToString/TryFormat for "r"/"R" improve by ~3x.
  • Loading branch information
stephentoub authored Mar 21, 2018
1 parent b82063e commit 06be68e
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 88 deletions.
18 changes: 9 additions & 9 deletions src/mscorlib/shared/System/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,46 +1255,46 @@ internal DateTime ToLocalTime(bool throwOnOverflow)

public String ToLongDateString()
{
return DateTimeFormat.Format(this, "D", DateTimeFormatInfo.CurrentInfo);
return DateTimeFormat.Format(this, "D", null);
}

public String ToLongTimeString()
{
return DateTimeFormat.Format(this, "T", DateTimeFormatInfo.CurrentInfo);
return DateTimeFormat.Format(this, "T", null);
}

public String ToShortDateString()
{
return DateTimeFormat.Format(this, "d", DateTimeFormatInfo.CurrentInfo);
return DateTimeFormat.Format(this, "d", null);
}

public String ToShortTimeString()
{
return DateTimeFormat.Format(this, "t", DateTimeFormatInfo.CurrentInfo);
return DateTimeFormat.Format(this, "t", null);
}

public override String ToString()
{
return DateTimeFormat.Format(this, null, DateTimeFormatInfo.CurrentInfo);
return DateTimeFormat.Format(this, null, null);
}

public String ToString(String format)
{
return DateTimeFormat.Format(this, format, DateTimeFormatInfo.CurrentInfo);
return DateTimeFormat.Format(this, format, null);
}

public String ToString(IFormatProvider provider)
{
return DateTimeFormat.Format(this, null, DateTimeFormatInfo.GetInstance(provider));
return DateTimeFormat.Format(this, null, provider);
}

public String ToString(String format, IFormatProvider provider)
{
return DateTimeFormat.Format(this, format, DateTimeFormatInfo.GetInstance(provider));
return DateTimeFormat.Format(this, format, provider);
}

public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null) =>
DateTimeFormat.TryFormat(this, destination, out charsWritten, format, DateTimeFormatInfo.GetInstance(provider));
DateTimeFormat.TryFormat(this, destination, out charsWritten, format, provider);

public DateTime ToUniversalTime()
{
Expand Down
10 changes: 5 additions & 5 deletions src/mscorlib/shared/System/DateTimeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,26 +755,26 @@ internal DateTimeOffset ToLocalTime(bool throwOnOverflow)

public override String ToString()
{
return DateTimeFormat.Format(ClockDateTime, null, DateTimeFormatInfo.CurrentInfo, Offset);
return DateTimeFormat.Format(ClockDateTime, null, null, Offset);
}

public String ToString(String format)
{
return DateTimeFormat.Format(ClockDateTime, format, DateTimeFormatInfo.CurrentInfo, Offset);
return DateTimeFormat.Format(ClockDateTime, format, null, Offset);
}

public String ToString(IFormatProvider formatProvider)
{
return DateTimeFormat.Format(ClockDateTime, null, DateTimeFormatInfo.GetInstance(formatProvider), Offset);
return DateTimeFormat.Format(ClockDateTime, null, formatProvider, Offset);
}

public String ToString(String format, IFormatProvider formatProvider)
{
return DateTimeFormat.Format(ClockDateTime, format, DateTimeFormatInfo.GetInstance(formatProvider), Offset);
return DateTimeFormat.Format(ClockDateTime, format, formatProvider, Offset);
}

public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider formatProvider = null) =>
DateTimeFormat.TryFormat(ClockDateTime, destination, out charsWritten, format, DateTimeFormatInfo.GetInstance(formatProvider), Offset);
DateTimeFormat.TryFormat(ClockDateTime, destination, out charsWritten, format, formatProvider, Offset);

public DateTimeOffset ToUniversalTime()
{
Expand Down
Loading

0 comments on commit 06be68e

Please sign in to comment.