Skip to content

Commit

Permalink
Benchmark cleanup and prepping for more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
thygrrr committed Nov 11, 2024
1 parent fedf14d commit 56a1284
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 71 deletions.
2 changes: 0 additions & 2 deletions fennECS.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ Global
{C75030DB-1B0D-44F7-B01F-018EA46178A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C75030DB-1B0D-44F7-B01F-018EA46178A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C75030DB-1B0D-44F7-B01F-018EA46178A4}.Release|Any CPU.Build.0 = Release|Any CPU
{08768DCD-54CB-4DAE-AD04-E618C1522753}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08768DCD-54CB-4DAE-AD04-E618C1522753}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08768DCD-54CB-4DAE-AD04-E618C1522753}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08768DCD-54CB-4DAE-AD04-E618C1522753}.Release|Any CPU.Build.0 = Release|Any CPU
{67189493-DD27-484F-8761-135501F29E1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Benchmark.ECS;

using BenchmarkDotNet.Filters;
using BenchmarkDotNet.Filters;
using BenchmarkDotNet.Running;

namespace Benchmark;

/// <summary>
/// Excludes a given category from benchmarks
/// </summary>
Expand Down
81 changes: 21 additions & 60 deletions fennecs.benchmarks/Conceptual/RWBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using fennecs;
using fennecs.events;
using fennecs.pools;
using fennecs.storage;

namespace Benchmark.Conceptual;

public interface ITest
{ }

#if !NET9_0_OR_GREATER
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class OverloadResolutionPriorityAttribute : Attribute
Expand All @@ -34,40 +28,6 @@ public OverloadResolutionPriorityAttribute(int priority)
}
#endif

record struct MyInt(in int value) : Fox<int>;

record struct MyVector(in Vector3 value) : Fox<Vector3>
{
private Vector3 _value = value;

public float X
{
get => _value.X;
set => _value.X = value;
}

public float Y
{
get => _value.Y;
set => _value.Y = value;
}

public float Z
{
get => _value.Z;
set => _value.Z = value;
}

public Vector3 value
{
readonly get => _value;
set => _value = value;
}
}

//[ShortRunJob(RuntimeMoniker.Mono)]
[ShortRunJob(RuntimeMoniker.Net90)]
[ShortRunJob(RuntimeMoniker.NativeAot90)]
public class RWBenchmarks
{
[Params(1_000)]
Expand All @@ -79,15 +39,14 @@ public void Setup()
{
_stream = new(count);
}

[Benchmark]
public void OldNOP()
{
_stream.Old((in Entity entity, [In] ref int a, [In] ref int b) =>
{
});
{ });
}

[Benchmark(Baseline = true)]
public void OldWW()
{
Expand All @@ -97,7 +56,7 @@ public void OldWW()
b++;
});
}

[Benchmark]
public void OldWRo()
{
Expand All @@ -106,7 +65,7 @@ public void OldWRo()
a += b;
});
}

[Benchmark]
public void OldEWR()
{
Expand Down Expand Up @@ -151,7 +110,7 @@ public void NewWR()
a.write++;
});
}

[Benchmark]
public void NewRW()
{
Expand All @@ -162,35 +121,37 @@ public void NewRW()
}
}

internal interface Fox<T>where T : notnull
internal interface Fox<T> where T : notnull
{
T value { get; set; }
T value { get; set; }
}

internal readonly record struct BenchStream2<C1, C2>(int Count)
internal readonly record struct BenchStream2<C1, C2>(int Count)
where C1 : notnull
where C2 : notnull
where C2 : notnull
{
public readonly Entity[] entities = new Entity[Count];
public readonly C1[] Data1 = new C1[Count];
public readonly C2[] Data2 = new C2[Count];
[MethodImpl(MethodImplOptions.AggressiveInlining)]


public void New(ComponentActionEWR<C1, C2> action)
{
var match = default(TypeExpression);
for (var i = 0; i < Count; i++) action(new(ref entities[i]), new(ref Data1[i], ref entities[i], ref match), new(ref Data2[i]));
}

public void Old(EntityComponentAction<C1, C2> action)
{
for (var i = 0; i < Count; i++) action(entities[i], ref Data1[i], ref Data2[i]);
}


public void Old2(ComponentActionRead<C1, C2> action)
{
for (var i = 0; i < Count; i++) action(in Data1[i], in Data2[i]);
}

[OverloadResolutionPriority(0)]
public void New(ComponentActionWW<C1, C2> action)
{
Expand Down Expand Up @@ -223,7 +184,7 @@ public void New(ComponentActionWR<C1, C2> action)
action(ref1, ref2);
}
}

[OverloadResolutionPriority(2)]
public void NewRo(ComponentActionWR<C1, C2> action)
{
Expand All @@ -232,10 +193,10 @@ public void NewRo(ComponentActionWR<C1, C2> action)
{
var ref1 = new RW<C1>(ref Data1[i], ref entities[i], ref match);
var ref2 = new R<C2>(ref Data2[i]);
action(ref1, ref2);
action(ref1, ref2);
}
}

[OverloadResolutionPriority(3)]
public void New(ComponentActionRR<C1, C2> action)
{
Expand All @@ -248,6 +209,7 @@ public void New(ComponentActionRR<C1, C2> action)
}
}


public void Old(ComponentActionWRo<C1, C2> action)
{
for (var i = 0; i < Count; i++) action(ref Data1[i], ref Data2[i]);
Expand All @@ -256,10 +218,9 @@ public void Old(ComponentActionWRo<C1, C2> action)

internal delegate void ComponentActionRead<C0, C1>(in C0 comp0, in C1 comp1);
internal delegate void ComponentActionWRo<C0, C1>(ref C0 comp0, ref readonly C1 comp1);

internal delegate void ComponentActionEWR<C0, C1>([In] EntityRef entity, RW<C0> comp0, R<C1> comp1) where C0 : notnull where C1 : notnull;
internal delegate void ComponentActionWR<C0, C1>(RW<C0> comp0, R<C1> comp1) where C0 : notnull where C1 : notnull;
internal delegate void ComponentActionRW<C0, C1>(R<C0> comp0, RW<C1> comp1) where C0 : notnull where C1 : notnull;
internal delegate void ComponentActionWWref<C0, C1>(ref readonly RW<C0> comp0, ref readonly RW<C1> comp1) where C0 : notnull where C1 : notnull;
internal delegate void ComponentActionWW<C0, C1>(RW<C0> comp0, RW<C1> comp1) where C0 : notnull where C1 : notnull;
internal delegate void ComponentActionRR<C0, C1>(R<C0> comp0, R<C1> comp1) where C0 : notnull where C1 : notnull;
internal delegate void ComponentActionRR<C0, C1>(R<C0> comp0, R<C1> comp1) where C0 : notnull where C1 : notnull;
56 changes: 56 additions & 0 deletions fennecs.benchmarks/Conceptual/TensorPrimitivesBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Add a <PackageReference Include="System.Numerics.Tensors" Version="9.0.0" /> to the csproj.
// dotnet run -c Release -f net9.0 --filter "*"

using System.Numerics.Tensors;
using BenchmarkDotNet.Attributes;

namespace Benchmark.Conceptual;

public class TensorPrimitivesBench
{
private float[] _source, _destination;

[GlobalSetup]
public void Setup()
{
var r = new Random(42);
_source = Enumerable.Range(0, 1024).Select(_ => (float)r.NextSingle()).ToArray();
_destination = new float[1024];
}

[Benchmark(Baseline = true)]
public void ManualLoop()
{
ReadOnlySpan<float> source = _source;
Span<float> destination = _destination;
for (int i = 0; i < source.Length; i++)
{
destination[i] = MathF.PI + source[i];
}
}

[Benchmark]
public void UnrolledLoop()
{
Span<float> source = _source.AsSpan();
Span<float> destination = _destination.AsSpan();
for (int i = 0; i < source.Length; i+=8)
{
destination[i] = MathF.PI + source[i];
destination[i+1] = MathF.PI + source[i+1];
destination[i+2] = MathF.PI + source[i+2];
destination[i+3] = MathF.PI + source[i+3];
destination[i+4] = MathF.PI + source[i+4];
destination[i+5] = MathF.PI + source[i+5];
destination[i+6] = MathF.PI + source[i+6];
destination[i+7] = MathF.PI + source[i+7];
}
}

[Benchmark]
public void BuiltIn()
{
//TensorPrimitives.Cosh(_source, _destination);
TensorPrimitives.Multiply(_source, MathF.PI, _destination);
}
}
12 changes: 10 additions & 2 deletions fennecs.benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;
using Benchmark;
using Benchmark.Conceptual;
using Benchmark.ECS;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;


var config = ManualConfig.Create(DefaultConfig.Instance).WithOptions(ConfigOptions.JoinSummary);
var config = ManualConfig
.Create(DefaultConfig.Instance)
.WithOptions(ConfigOptions.JoinSummary)
.AddJob(Job.ShortRun.WithRuntime(CoreRuntime.Core90))
.AddJob(Job.ShortRun.WithRuntime(NativeAotRuntime.Net90))
.HideColumns("Job", "Error", "Median", "RatioSD");

// Most relevant vectorization instruction sets, add other intrinsics as needed.
// These are exclusions you can use to TURN OFF specific benchmarks based on the
Expand All @@ -17,5 +24,6 @@
if (!Sse2.IsSupported) config.AddFilter(new CategoryExclusion(nameof(Sse2)));
if (!AdvSimd.IsSupported) config.AddFilter(new CategoryExclusion(nameof(AdvSimd)));


//BenchmarkRunner.Run<DorakuBenchmarks>(config);
BenchmarkRunner.Run<RWBenchmarks>(config);
4 changes: 2 additions & 2 deletions fennecs.benchmarks/fennecs.benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Benchmark</RootNamespace>
<Configurations>Release;Debug</Configurations>
<Configurations>Release</Configurations>
<Platforms>AnyCPU</Platforms>
<IsPackable>false</IsPackable>
<PackageId>fennecs.benchmarks</PackageId>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
<LangVersion>preview</LangVersion>
<PublishAot>true</PublishAot>
<OptimizationPreference>Speed</OptimizationPreference>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand All @@ -31,5 +32,4 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.1-nightly.20241107.194" />
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions fennecs.tests/Conceptual/BloomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public void GetInterfaces()
{

output.WriteLine($"Hardware: {Vector256.IsHardwareAccelerated}");

Vector256<uint> bloom = default;

var hash = Vector256<long>.Zero;

Expand Down

0 comments on commit 56a1284

Please sign in to comment.