Skip to content

Commit

Permalink
Merge pull request #31 from udaken/Optimize-for-bool-type
Browse files Browse the repository at this point in the history
Optimize for bool type
  • Loading branch information
neuecc authored Jan 8, 2021
2 parents ae478c5 + c250bb6 commit 1c80aa4
Show file tree
Hide file tree
Showing 18 changed files with 206 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ static object CreateFormatter(Type type)
{
return CreateNullableFormatter<System.Guid>();
}
if (type == typeof(System.Boolean?))
{
return CreateNullableFormatter<System.Boolean>();
}
if (type == typeof(System.IntPtr))
{
// ignore format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ public bool TryCopyTo(Span<char> destination, out int charsWritten)
/// <summary>Converts the value of this instance to a System.String.</summary>
public override string ToString()
{
if (index == 0)
return string.Empty;

return new string(buffer, 0, index);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ static object CreateFormatter(Type type)
{
return new TryFormat<System.Guid>((System.Guid x, Span<byte> dest, out int written, StandardFormat format) => Utf8Formatter.TryFormat(x, dest, out written, format));
}
if (type == typeof(System.Boolean))
{
return new TryFormat<System.Boolean>((System.Boolean x, Span<byte> dest, out int written, StandardFormat format) => Utf8Formatter.TryFormat(x, dest, out written, format));
}
if (type == typeof(System.Byte?))
{
return CreateNullableFormatter<System.Byte>();
Expand Down Expand Up @@ -128,6 +132,10 @@ static object CreateFormatter(Type type)
{
return CreateNullableFormatter<System.Guid>();
}
if (type == typeof(System.Boolean?))
{
return CreateNullableFormatter<System.Boolean>();
}
if (type == typeof(System.IntPtr))
{
// ignore format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void AppendLine(System.Byte value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.DateTime value)
Expand Down Expand Up @@ -97,6 +98,7 @@ public void AppendLine(System.DateTime value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.DateTimeOffset value)
Expand Down Expand Up @@ -142,6 +144,7 @@ public void AppendLine(System.DateTimeOffset value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.Decimal value)
Expand Down Expand Up @@ -187,6 +190,7 @@ public void AppendLine(System.Decimal value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.Double value)
Expand Down Expand Up @@ -232,6 +236,7 @@ public void AppendLine(System.Double value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.Int16 value)
Expand Down Expand Up @@ -277,6 +282,7 @@ public void AppendLine(System.Int16 value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.Int32 value)
Expand Down Expand Up @@ -322,6 +328,7 @@ public void AppendLine(System.Int32 value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.Int64 value)
Expand Down Expand Up @@ -367,6 +374,7 @@ public void AppendLine(System.Int64 value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.SByte value)
Expand Down Expand Up @@ -412,6 +420,7 @@ public void AppendLine(System.SByte value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.Single value)
Expand Down Expand Up @@ -457,6 +466,7 @@ public void AppendLine(System.Single value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.TimeSpan value)
Expand Down Expand Up @@ -502,6 +512,7 @@ public void AppendLine(System.TimeSpan value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.UInt16 value)
Expand Down Expand Up @@ -547,6 +558,7 @@ public void AppendLine(System.UInt16 value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.UInt32 value)
Expand Down Expand Up @@ -592,6 +604,7 @@ public void AppendLine(System.UInt32 value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.UInt64 value)
Expand Down Expand Up @@ -637,6 +650,7 @@ public void AppendLine(System.UInt64 value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.Guid value)
Expand Down Expand Up @@ -682,5 +696,52 @@ public void AppendLine(System.Guid value, StandardFormat format)
Append(value, format);
AppendLine();
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.Boolean value)
{
if(!Utf8Formatter.TryFormat(value, buffer.AsSpan(index), out var written))
{
Grow(written);
if(!Utf8Formatter.TryFormat(value, buffer.AsSpan(index), out written))
{
ThrowArgumentException(nameof(value));
}
}
index += written;
}

/// <summary>Appends the string representation of a specified value to this instance with numeric format strings.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(System.Boolean value, StandardFormat format)
{
if(!Utf8Formatter.TryFormat(value, buffer.AsSpan(index), out var written, format))
{
Grow(written);
if(!Utf8Formatter.TryFormat(value, buffer.AsSpan(index), out written, format))
{
ThrowArgumentException(nameof(value));
}
}
index += written;
}

/// <summary>Appends the string representation of a specified value followed by the default line terminator to the end of this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine(System.Boolean value)
{
Append(value);
AppendLine();
}

/// <summary>Appends the string representation of a specified value with numeric format strings followed by the default line terminator to the end of this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine(System.Boolean value, StandardFormat format)
{
Append(value, format);
AppendLine();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ public Task WriteToAsync(Stream stream)
/// <summary>Encode the innner utf8 buffer to a System.String.</summary>
public override string ToString()
{
if (index == 0)
return string.Empty;

return UTF8NoBom.GetString(buffer, 0, index);
}

Expand Down
5 changes: 5 additions & 0 deletions src/ZString.Unity/Assets/Scripts/ZString/ZString.Concat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public static partial class ZString
/// <summary>Concatenates the string representation of some specified objects.</summary>
public static string Concat<T1>(T1 arg1)
{
if (typeof(T1) == typeof(string))
{
return (arg1 != null) ? Unsafe.As<string>(arg1) : string.Empty;
}

var sb = new Utf16ValueStringBuilder(true);
try
{
Expand Down
2 changes: 2 additions & 0 deletions src/ZString/T4Common.t4
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@
typeof(Guid),
// typeof(Version),
};

var utf8spanFormattables = spanFormattables.Append(typeof(bool));
#>
4 changes: 4 additions & 0 deletions src/ZString/Utf16/Utf16ValueStringBuilder.CreateFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ static object CreateFormatter(Type type)
{
return CreateNullableFormatter<System.Guid>();
}
if (type == typeof(System.Boolean?))
{
return CreateNullableFormatter<System.Boolean>();
}
if (type == typeof(System.IntPtr))
{
// ignore format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Cysharp.Text
return new TryFormat<<#= t.FullName #>>((<#= t.FullName #> x, Span<char> dest, out int written, ReadOnlySpan<char> format) => x.TryFormat(dest, out written, format));
}
<# } #>
<# foreach(var t in spanFormattables) { #>
<# foreach(var t in spanFormattables.Append(typeof(bool))) { #>
if (type == typeof(<#= t.FullName #>?))
{
return CreateNullableFormatter<<#= t.FullName #>>();
Expand Down
3 changes: 3 additions & 0 deletions src/ZString/Utf16ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ public bool TryCopyTo(Span<char> destination, out int charsWritten)
/// <summary>Converts the value of this instance to a System.String.</summary>
public override string ToString()
{
if (index == 0)
return string.Empty;

return new string(buffer, 0, index);
}

Expand Down
8 changes: 8 additions & 0 deletions src/ZString/Utf8/Utf8ValueStringBuilder.CreateFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ static object CreateFormatter(Type type)
{
return new TryFormat<System.Guid>((System.Guid x, Span<byte> dest, out int written, StandardFormat format) => Utf8Formatter.TryFormat(x, dest, out written, format));
}
if (type == typeof(System.Boolean))
{
return new TryFormat<System.Boolean>((System.Boolean x, Span<byte> dest, out int written, StandardFormat format) => Utf8Formatter.TryFormat(x, dest, out written, format));
}
if (type == typeof(System.Byte?))
{
return CreateNullableFormatter<System.Byte>();
Expand Down Expand Up @@ -128,6 +132,10 @@ static object CreateFormatter(Type type)
{
return CreateNullableFormatter<System.Guid>();
}
if (type == typeof(System.Boolean?))
{
return CreateNullableFormatter<System.Boolean>();
}
if (type == typeof(System.IntPtr))
{
// ignore format
Expand Down
4 changes: 2 additions & 2 deletions src/ZString/Utf8/Utf8ValueStringBuilder.CreateFormatter.tt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace Cysharp.Text
{
static object CreateFormatter(Type type)
{
<# foreach(var t in spanFormattables) { #>
<# foreach(var t in utf8spanFormattables) { #>
if (type == typeof(<#= t.FullName #>))
{
return new TryFormat<<#= t.FullName #>>((<#= t.FullName #> x, Span<byte> dest, out int written, StandardFormat format) => Utf8Formatter.TryFormat(x, dest, out written, format));
}
<# } #>
<# foreach(var t in spanFormattables) { #>
<# foreach(var t in utf8spanFormattables) { #>
if (type == typeof(<#= t.FullName #>?))
{
return CreateNullableFormatter<<#= t.FullName #>>();
Expand Down
Loading

0 comments on commit 1c80aa4

Please sign in to comment.