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

Add a fast pass for ZString.Join when the argument is a string #94

Merged
merged 2 commits into from
Sep 21, 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
7 changes: 5 additions & 2 deletions sandbox/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../../opensource.snk</AssemblyOriginatorKeyFile>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="System.Text.Json" Version="4.7.1" />
<ProjectReference Include="..\..\src\ZString\ZString.csproj" />
<ProjectReference Include="..\..\src\ZString\ZString.csproj">
<SetTargetFramework>TargetFramework=netstandard2.1</SetTargetFramework>
</ProjectReference>
</ItemGroup>

</Project>
30 changes: 20 additions & 10 deletions sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using Cysharp.Text;
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
// using System.Text.Formatting;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace ConsoleApp
{
Expand All @@ -20,19 +15,34 @@ class Program
{
static void Main(string[] args)
{
// BenchmarkRunner.Run<JoinBenchmark>();
Run();
}

static void Run()
{
ZString.Join(",", "a", "b");
TimeSpan span = new TimeSpan(12, 34, 56);
Console.WriteLine($"string.Format: {string.Format(@"{0:h\,h\:mm\:ss}", span)}");


Console.WriteLine($"ZString.Format: {ZString.Format(@"{0:h\,h\:mm\:ss}", span)}");
}
}

public class JoinBenchmark
{
public string[] Source = new []{ "111", "222", "333"};
public const string Sp = ",";

[Benchmark]
public string StringJoin()
{
return string.Join(Sp, Source);
}


[Benchmark]
public string ZStringJoin()
{
return ZString.Join(Sp, Source);
}
}

Expand Down
115 changes: 100 additions & 15 deletions src/ZString.Unity/Assets/Scripts/ZString/ZString.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace Cysharp.Text
Expand Down Expand Up @@ -57,28 +57,28 @@ public static Utf8ValueStringBuilder CreateUtf8StringBuilder(bool notNested)
return new Utf8ValueStringBuilder(notNested);
}

/// <summary>Concatenates the elements of an array, using the specified seperator between each element.</summary>
/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join<T>(char separator, params T[] values)
{
ReadOnlySpan<char> s = stackalloc char[1] { separator };
return JoinInternal<T>(s, values.AsSpan());
}

/// <summary>Concatenates the elements of an array, using the specified seperator between each element.</summary>
/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join<T>(char separator, List<T> values)
{
ReadOnlySpan<char> s = stackalloc char[1] { separator };
return JoinInternal(s, (IReadOnlyList<T>)values);
}

/// <summary>Concatenates the elements of an array, using the specified seperator between each element.</summary>
/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join<T>(char separator, ReadOnlySpan<T> values)
{
ReadOnlySpan<char> s = stackalloc char[1] { separator };
return JoinInternal(s, values);
}

/// <summary>Concatenates the elements of an array, using the specified seperator between each element.</summary>
/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join<T>(char separator, IEnumerable<T> values)
{
ReadOnlySpan<char> s = stackalloc char[1] { separator };
Expand Down Expand Up @@ -109,19 +109,19 @@ public static string Join<T>(char separator, IReadOnlyCollection<T> values)
return JoinInternal(s, values.AsEnumerable());
}

/// <summary>Concatenates the elements of an array, using the specified seperator between each element.</summary>
/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join<T>(string separator, params T[] values)
{
return JoinInternal<T>(separator.AsSpan(), values.AsSpan());
}

/// <summary>Concatenates the elements of an array, using the specified seperator between each element.</summary>
/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join<T>(string separator, List<T> values)
{
return JoinInternal(separator.AsSpan(), (IReadOnlyList<T>)values);
}

/// <summary>Concatenates the elements of an array, using the specified seperator between each element.</summary>
/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join<T>(string separator, ReadOnlySpan<T> values)
{
return JoinInternal(separator.AsSpan(), values);
Expand All @@ -147,12 +147,34 @@ public static string Join<T>(string separator, IReadOnlyCollection<T> values)
return JoinInternal(separator.AsSpan(), values.AsEnumerable());
}

/// <summary>Concatenates the elements of an array, using the specified seperator between each element.</summary>
/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join<T>(string separator, IEnumerable<T> values)
{
return JoinInternal(separator.AsSpan(), values);
}


#if NETSTANDARD2_1_OR_GREATER || NET_STANDARD_2_1
/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join(char separator, ReadOnlySpan<string> values)
{
ReadOnlySpan<char> s = stackalloc char[1] { separator };
return JoinInternal(s, values);
}

/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join(char separator, params string[] values)
{
ReadOnlySpan<char> s = stackalloc char[1] { separator };
return JoinInternal(s, (ReadOnlySpan<string>)values.AsSpan());
}

/// <summary>Concatenates the elements of an array, using the specified separator between each element.</summary>
public static string Join(string separator, params string[] values)
{
return JoinInternal(separator.AsSpan(), (ReadOnlySpan<string>)values.AsSpan());
}
#endif

/// <summary>Concatenates the string representation of some specified objects.</summary>
public static string Concat<T>(params T[] values)
{
Expand Down Expand Up @@ -211,16 +233,23 @@ static string JoinInternal<T>(ReadOnlySpan<char> separator, IList<T> values)

static string JoinInternal<T>(ReadOnlySpan<char> separator, IReadOnlyList<T> values)
{
var count = values.Count;
if (count == 0)
if (values.Count == 0)
{
return string.Empty;
}
else if (typeof(T) == typeof(string) && count == 1)
#if NETSTANDARD2_1_OR_GREATER || NET_STANDARD_2_1
if (values is string[] valueArray)
{
return Unsafe.As<string>(values[0]);
return JoinInternal(separator, valueArray.AsSpan());
}

#if NET5_0_OR_GREATER
if (values is List<string> valueList)
{
return JoinInternal(separator, CollectionsMarshal.AsSpan(valueList));
}
#endif
#endif

var sb = new Utf16ValueStringBuilder(true);
try
{
Expand Down Expand Up @@ -269,5 +298,61 @@ static string JoinInternal<T>(ReadOnlySpan<char> separator, IEnumerable<T> value
sb.Dispose();
}
}

#if NETSTANDARD2_1_OR_GREATER || NET_STANDARD_2_1
static string JoinInternal(ReadOnlySpan<char> separator, ReadOnlySpan<string> values)
{
if (values.Length == 0)
{
return string.Empty;
}
if (values.Length == 1)
{
return values[0];
}

var totalSeparatorsLength = (values.Length - 1) * separator.Length;
var totalLength = totalSeparatorsLength;
for (var i = 0; i < values.Length; i++)
{
if (values[i] is { } value)
{
totalLength += value.Length;
}
}

if (totalLength == 0)
{
return string.Empty;
}

var resultString = string.Create(totalLength, 0, (_, _) => { });
var writeBuffer = MemoryMarshal.CreateSpan(ref MemoryMarshal.GetReference(resultString.AsSpan()), resultString.Length);
var copiedLength = 0;
for (var i = 0; i < values.Length; i++)
{
if (values[i] is { } value)
{
value.AsSpan().CopyTo(writeBuffer.Slice(copiedLength));
copiedLength += value.Length;
}

// Fill in the separator
if (i < values.Length - 1)
{
if (separator.Length == 1)
{
writeBuffer[copiedLength++] = separator[0];
}
else
{
separator.CopyTo(writeBuffer.Slice(copiedLength));
copiedLength += separator.Length;
}
}
}
return resultString;
}
#endif
}
}
Loading