Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use compiler-generated delegate caches in ToDelimitedString #966

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
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
101 changes: 14 additions & 87 deletions MoreLinq/ToDelimitedString.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ public static string ToDelimitedString(this IEnumerable<bool> source, string del
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.Boolean);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, bool, StringBuilder> Boolean = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -88,12 +83,7 @@ public static string ToDelimitedString(this IEnumerable<byte> source, string del
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.Byte);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, byte, StringBuilder> Byte = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -119,12 +109,7 @@ public static string ToDelimitedString(this IEnumerable<char> source, string del
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.Char);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, char, StringBuilder> Char = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -150,12 +135,7 @@ public static string ToDelimitedString(this IEnumerable<decimal> source, string
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.Decimal);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, decimal, StringBuilder> Decimal = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -181,12 +161,7 @@ public static string ToDelimitedString(this IEnumerable<double> source, string d
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.Double);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, double, StringBuilder> Double = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -212,12 +187,7 @@ public static string ToDelimitedString(this IEnumerable<float> source, string de
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.Single);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, float, StringBuilder> Single = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -243,12 +213,7 @@ public static string ToDelimitedString(this IEnumerable<int> source, string deli
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.Int32);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, int, StringBuilder> Int32 = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -274,12 +239,7 @@ public static string ToDelimitedString(this IEnumerable<long> source, string del
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.Int64);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, long, StringBuilder> Int64 = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -305,12 +265,7 @@ public static string ToDelimitedString(this IEnumerable<sbyte> source, string de
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.SByte);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, sbyte, StringBuilder> SByte = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -336,12 +291,7 @@ public static string ToDelimitedString(this IEnumerable<short> source, string de
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.Int16);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, short, StringBuilder> Int16 = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -367,12 +317,7 @@ public static string ToDelimitedString(this IEnumerable<string> source, string d
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.String);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, string, StringBuilder> String = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -398,12 +343,7 @@ public static string ToDelimitedString(this IEnumerable<uint> source, string del
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.UInt32);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, uint, StringBuilder> UInt32 = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -429,12 +369,7 @@ public static string ToDelimitedString(this IEnumerable<ulong> source, string de
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.UInt64);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, ulong, StringBuilder> UInt64 = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

/// <summary>
Expand All @@ -460,15 +395,7 @@ public static string ToDelimitedString(this IEnumerable<ushort> source, string d
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.UInt16);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, ushort, StringBuilder> UInt16 = (sb, e) => sb.Append(e);
}


static partial class StringBuilderAppenders {}
}
}
15 changes: 4 additions & 11 deletions MoreLinq/ToDelimitedString.g.tt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace MoreLinq
using System.Text;

partial class MoreEnumerable
{
<# var cscp = new CSharpCodeProvider();
{<#
var cscp = new CSharpCodeProvider();
var types =
from method in typeof(StringBuilder).GetMethods(BindingFlags.Public | BindingFlags.Instance)
where "Append" == method.Name
Expand Down Expand Up @@ -73,6 +73,7 @@ namespace MoreLinq
break;
}
#>

/// <summary>
/// Creates a delimited string from a sequence of values and
/// a given delimiter.
Expand All @@ -96,16 +97,8 @@ namespace MoreLinq
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (delimiter == null) throw new ArgumentNullException(nameof(delimiter));
return ToDelimitedStringImpl(source, delimiter, StringBuilderAppenders.<#= type.Type.Name #>);
}

static partial class StringBuilderAppenders
{
public static readonly Func<StringBuilder, <#= type.Name #>, StringBuilder> <#= type.Type.Name #> = (sb, e) => sb.Append(e);
return ToDelimitedStringImpl(source, delimiter, static (sb, e) => sb.Append(e));
}

<# } #>

static partial class StringBuilderAppenders {}
}
}