Merge remote-tracking branch 'refs/remotes/upstream/pr/fix-small-object-heap-issue' into pr/fix-small-object-heap-issue

This commit is contained in:
wrenge 2024-04-27 09:03:26 +03:00
commit b8832e50e7
9 changed files with 83 additions and 15 deletions

View File

@ -12,6 +12,15 @@ namespace DotRecast.Core
Array.Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy<T>(Span<T> sourceArray, int sourceIndex, Span<T> destinationArray, int destinationIndex, int length)
{
var src = sourceArray.Slice(sourceIndex, length);
var dst = destinationArray.Slice(destinationIndex);
src.CopyTo(dst);
}
// Type Safe Copy
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy<T>(T[] sourceArray, T[] destinationArray, long length)

View File

@ -11,7 +11,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageReference Include="NUnit.Analyzers" Version="4.1.0">
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

View File

@ -1,6 +1,7 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Net.Sockets;
using DotRecast.Core.Buffers;
using DotRecast.Core.Collections;
using NUnit.Framework;
@ -84,18 +85,76 @@ public class RcArrayBenchmarkTests
[Test]
public void TestBenchmarkArrays()
{
var list = new List<(string title, long ticks)>();
list.Add(Bench("new int[len]", RoundForArray));
list.Add(Bench("ArrayPool<int>.Shared.Rent(len)", RoundForPureRentArray));
list.Add(Bench("RcRentedArray.Rent<int>(len)", RoundForRcRentedArray));
list.Add(Bench("new RcStackArray512<int>()", RoundForRcStackArray));
list.Add(Bench("stackalloc int[len]", RoundForStackalloc));
var results = new List<(string title, long ticks)>();
results.Add(Bench("new int[len]", RoundForArray));
results.Add(Bench("ArrayPool<int>.Shared.Rent(len)", RoundForPureRentArray));
results.Add(Bench("RcRentedArray.Rent<int>(len)", RoundForRcRentedArray));
results.Add(Bench("new RcStackArray512<int>()", RoundForRcStackArray));
results.Add(Bench("stackalloc int[len]", RoundForStackalloc));
list.Sort((x, y) => x.ticks.CompareTo(y.ticks));
results.Sort((x, y) => x.ticks.CompareTo(y.ticks));
foreach (var t in list)
foreach (var t in results)
{
Console.WriteLine($"{t.title} {t.ticks / (double)TimeSpan.TicksPerMillisecond} ms");
}
}
[Test]
public void TestSpanVsArray()
{
var r = new RcRand();
var list = new List<(long[] src, long[] dest)>();
for (int i = 0; i < 14; ++i)
{
var s = new long[(int)Math.Pow(2, i + 1)];
var d = new long[(int)Math.Pow(2, i + 1)];
for (int ii = 0; ii < s.Length; ++ii)
{
s[ii] = r.NextInt32();
}
list.Add((s, d));
}
var results = new List<(string title, long ticks)>();
for (int i = 0; i < list.Count; ++i)
{
var seq = i;
Array.Fill(list[seq].dest, 0);
var resultLong = Bench($"long[{list[seq].src.Length}]", _ =>
{
var v = list[seq];
RcArrays.Copy(v.src, 0, v.dest, 0, v.src.Length);
});
Array.Fill(list[seq].dest, 0);
var resultSpan = Bench($"Span<long[], {list[seq].src.Length}>", _ =>
{
var v = list[seq];
Span<long> src = v.src;
Span<long> dest = v.dest;
RcArrays.Copy(src, 0, dest, 0, src.Length);
});
results.Add(resultLong);
results.Add(resultSpan);
}
int newLine = 0;
foreach (var t in results)
{
Console.WriteLine($"{t.title}: {t.ticks / (double)TimeSpan.TicksPerMillisecond} ms");
newLine += 1;
if (0 == (newLine % 2))
{
Console.WriteLine("");
}
}
}
}

View File

@ -11,7 +11,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageReference Include="NUnit.Analyzers" Version="4.1.0">
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

View File

@ -11,7 +11,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageReference Include="NUnit.Analyzers" Version="4.1.0">
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

View File

@ -11,7 +11,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageReference Include="NUnit.Analyzers" Version="4.1.0">
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

View File

@ -11,7 +11,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageReference Include="NUnit.Analyzers" Version="4.1.0">
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

View File

@ -11,7 +11,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageReference Include="NUnit.Analyzers" Version="4.1.0">
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

View File

@ -11,7 +11,7 @@
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageReference Include="NUnit.Analyzers" Version="4.1.0">
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>