Skip to content

Commit

Permalink
Optimize integral ToString (without generics) (#32528)
Browse files Browse the repository at this point in the history
- Make the fast path for Number.FormatXX & Number.TryFormatXX inlineable
- Make parameterless ToString in SByte, Int16, Int32, and Int64 directly invoke the default formatting method
  • Loading branch information
ts2do committed Feb 19, 2020
1 parent b49ab01 commit 7c7e8ed
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 283 deletions.
19 changes: 4 additions & 15 deletions src/libraries/System.Private.CoreLib/src/System/Int16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public override int GetHashCode()

public override string ToString()
{
return Number.FormatInt32(m_value, null, null);
return Number.Int32ToDecStr(m_value);
}

public string ToString(IFormatProvider? provider)
{
return Number.FormatInt32(m_value, null, provider);
return ToString(null, provider);
}

public string ToString(string? format)
Expand All @@ -84,23 +84,12 @@ public string ToString(string? format)

public string ToString(string? format, IFormatProvider? provider)
{
if (m_value < 0 && format != null && format.Length > 0 && (format[0] == 'X' || format[0] == 'x'))
{
uint temp = (uint)(m_value & 0x0000FFFF);
return Number.FormatUInt32(temp, format, provider);
}

return Number.FormatInt32(m_value, format, provider);
return Number.FormatInt32(m_value, 0x0000FFFF, format, provider);
}

public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
{
if (m_value < 0 && format.Length > 0 && (format[0] == 'X' || format[0] == 'x'))
{
uint temp = (uint)(m_value & 0x0000FFFF);
return Number.TryFormatUInt32(temp, format, provider, destination, out charsWritten);
}
return Number.TryFormatInt32(m_value, format, provider, destination, out charsWritten);
return Number.TryFormatInt32(m_value, 0x0000FFFF, format, provider, destination, out charsWritten);
}

public static short Parse(string s)
Expand Down
10 changes: 5 additions & 5 deletions src/libraries/System.Private.CoreLib/src/System/Int32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,27 @@ public override int GetHashCode()

public override string ToString()
{
return Number.FormatInt32(m_value, null, null);
return Number.Int32ToDecStr(m_value);
}

public string ToString(string? format)
{
return Number.FormatInt32(m_value, format, null);
return ToString(format, null);
}

public string ToString(IFormatProvider? provider)
{
return Number.FormatInt32(m_value, null, provider);
return ToString(null, provider);
}

public string ToString(string? format, IFormatProvider? provider)
{
return Number.FormatInt32(m_value, format, provider);
return Number.FormatInt32(m_value, ~0, format, provider);
}

public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
{
return Number.TryFormatInt32(m_value, format, provider, destination, out charsWritten);
return Number.TryFormatInt32(m_value, ~0, format, provider, destination, out charsWritten);
}

public static int Parse(string s)
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Int64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public override int GetHashCode()

public override string ToString()
{
return Number.FormatInt64(m_value, null, null);
return Number.Int64ToDecStr(m_value);
}

public string ToString(IFormatProvider? provider)
Expand Down
Loading

0 comments on commit 7c7e8ed

Please sign in to comment.