Skip to content

Commit

Permalink
AppendFormatted<T> JIT
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Nov 23, 2023
1 parent 43d5845 commit afa4b18
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/Utf8StringInterpolation/Utf8StringInterpolation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.1'">
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<None Include="../../Icon.png" Pack="true" PackagePath="/" />
</ItemGroup>

<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
<None Update="Utf8String.AppendFormatted.tt">
<None Update="Utf8StringWriter.AppendFormatted.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Utf8String.AppendFormatted.cs</LastGenOutput>
<LastGenOutput>Utf8StringWriter.AppendFormatted.cs</LastGenOutput>
</None>
<Compile Update="Utf8String.AppendFormatted.cs">
<Compile Update="Utf8StringWriter.AppendFormatted.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Utf8String.AppendFormatted.tt</DependentUpon>
<DependentUpon>Utf8StringWriter.AppendFormatted.tt</DependentUpon>
</Compile>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Utf8StringInterpolation;

public ref partial struct Utf8StringWriter<TBufferWriter>
{
# if true
#if true

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(bool? value, int alignment = 0, string? format = null)
Expand Down Expand Up @@ -1398,7 +1398,7 @@ void AppendFormattedAlignment(TimeSpan value, int alignment, string? format)
}
#endif

# if true
#if true

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(char? value, int alignment = 0, string? format = null)
Expand Down Expand Up @@ -1485,4 +1485,83 @@ void AppendFormattedAlignment(char value, int alignment, string? format)
}
#endif


// `if typeof(T) == typeof()` will eliminate in JIT.
public void AppendFormatted<T>(T value, int alignment = 0, string? format = null)
{
if (typeof(T) == typeof(bool))
{
AppendFormatted(Unsafe.As<T, bool>(ref value), alignment, format);
}
else if (typeof(T) == typeof(char))
{
AppendFormatted(Unsafe.As<T, char>(ref value), alignment, format);
}
#if !NET8_0_OR_GREATER
else if (typeof(T) == typeof(byte))
{
AppendFormatted(Unsafe.As<T, byte>(ref value), alignment, format);
}
else if (typeof(T) == typeof(Decimal))
{
AppendFormatted(Unsafe.As<T, Decimal>(ref value), alignment, format);
}
else if (typeof(T) == typeof(Double))
{
AppendFormatted(Unsafe.As<T, Double>(ref value), alignment, format);
}
else if (typeof(T) == typeof(Guid))
{
AppendFormatted(Unsafe.As<T, Guid>(ref value), alignment, format);
}
else if (typeof(T) == typeof(Int16))
{
AppendFormatted(Unsafe.As<T, Int16>(ref value), alignment, format);
}
else if (typeof(T) == typeof(Int32))
{
AppendFormatted(Unsafe.As<T, Int32>(ref value), alignment, format);
}
else if (typeof(T) == typeof(Int64))
{
AppendFormatted(Unsafe.As<T, Int64>(ref value), alignment, format);
}
else if (typeof(T) == typeof(SByte))
{
AppendFormatted(Unsafe.As<T, SByte>(ref value), alignment, format);
}
else if (typeof(T) == typeof(Single))
{
AppendFormatted(Unsafe.As<T, Single>(ref value), alignment, format);
}
else if (typeof(T) == typeof(UInt16))
{
AppendFormatted(Unsafe.As<T, UInt16>(ref value), alignment, format);
}
else if (typeof(T) == typeof(UInt32))
{
AppendFormatted(Unsafe.As<T, UInt32>(ref value), alignment, format);
}
else if (typeof(T) == typeof(UInt64))
{
AppendFormatted(Unsafe.As<T, UInt64>(ref value), alignment, format);
}
else if (typeof(T) == typeof(DateTime))
{
AppendFormatted(Unsafe.As<T, DateTime>(ref value), alignment, format);
}
else if (typeof(T) == typeof(DateTimeOffset))
{
AppendFormatted(Unsafe.As<T, DateTimeOffset>(ref value), alignment, format);
}
else if (typeof(T) == typeof(TimeSpan))
{
AppendFormatted(Unsafe.As<T, TimeSpan>(ref value), alignment, format);
}
#endif
else
{
AppendFormattedCore<T>(value, alignment, format);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Utf8StringInterpolation;
public ref partial struct Utf8StringWriter<TBufferWriter>
{
<# foreach(var x in generateTypes) { #>
<#= (x.type is not "bool" and not "char") ? "#if !NET8_0_OR_GREATER" : "# if true" #>
<#= (x.type is not "bool" and not "char") ? "#if !NET8_0_OR_GREATER" : "#if true" #>

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(<#= x.type #>? value, int alignment = 0, string? format = null)
Expand Down Expand Up @@ -117,4 +117,29 @@ public ref partial struct Utf8StringWriter<TBufferWriter>
#endif

<# } #>

// `if typeof(T) == typeof()` will eliminate in JIT.
public void AppendFormatted<T>(T value, int alignment = 0, string? format = null)
{
if (typeof(T) == typeof(bool))
{
AppendFormatted(Unsafe.As<T, bool>(ref value), alignment, format);
}
else if (typeof(T) == typeof(char))
{
AppendFormatted(Unsafe.As<T, char>(ref value), alignment, format);
}
#if !NET8_0_OR_GREATER
<# foreach(var x in generateTypes.Where(x => x.type is not "bool" and not "char")) { #>
else if (typeof(T) == typeof(<#= x.type #>))
{
AppendFormatted(Unsafe.As<T, <#= x.type #>>(ref value), alignment, format);
}
<# } #>
#endif
else
{
AppendFormattedCore<T>(value, alignment, format);
}
}
}
2 changes: 1 addition & 1 deletion src/Utf8StringInterpolation/Utf8StringWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? for
AppendFormatted(value.Value, alignment, format);
}

public void AppendFormatted<T>(T value, int alignment = 0, string? format = null)
void AppendFormattedCore<T>(T value, int alignment = 0, string? format = null)
{
// no alignment or add right whitespace
if (alignment <= 0)
Expand Down

0 comments on commit afa4b18

Please sign in to comment.