Skip to content

Commit c569ea7

Browse files
committed
some benchmarks and conceptual ideas
1 parent 177addc commit c569ea7

File tree

3 files changed

+155
-4
lines changed

3 files changed

+155
-4
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using System.Runtime.Intrinsics;
2+
using System.Runtime.Intrinsics.X86;
3+
using BenchmarkDotNet.Attributes;
4+
using BenchmarkDotNet.Configs;
5+
using BenchmarkDotNet.Order;
6+
using fennecs;
7+
8+
namespace Benchmark.Conceptual;
9+
10+
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
11+
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByParams)]
12+
public class ForStructuralVsStateful
13+
{
14+
[Params(10_000)]
15+
public int Entities { get; set; }
16+
17+
[Params(0.9f, 0.5f, 0.1f)]
18+
public float Homogenity { get; set; }
19+
20+
[Params(100, 1_000, 10_000)]
21+
public int Actions { get; set; }
22+
23+
private World _world = null!;
24+
private Stream<ushort> _stream = null!;
25+
private Random _random = null!;
26+
27+
[GlobalSetup]
28+
public void GlobaSetup()
29+
{
30+
_world = new(Entities * 30);
31+
32+
_stream = _world.Query<ushort>().Stream();
33+
34+
_world.Spawn().Add(1).Despawn();
35+
}
36+
37+
[IterationSetup]
38+
public void IterationSetup()
39+
{
40+
Console.WriteLine("IterationSetup");
41+
_world.All.Despawn();
42+
if (_world.Count != 0) throw new("World is not empty!");
43+
44+
_random = new(69);
45+
46+
_world.Entity().Spawn(Entities);
47+
foreach (var entity in _world.All.ToArray())
48+
{
49+
entity.Add((ushort) _random.Next((ushort) (Actions * Homogenity), Actions));
50+
}
51+
}
52+
53+
[Benchmark]
54+
public int CountDown_with_Structural_Change()
55+
{
56+
while (_stream.Count > 0)
57+
{
58+
_stream.For((in Entity entity, ref ushort value) =>
59+
{
60+
value--;
61+
if (value <= 0) entity.Remove<ushort>();
62+
});
63+
}
64+
return _stream.Count;
65+
}
66+
67+
[Benchmark]
68+
public int CountDown_with_Delayed_Change()
69+
{
70+
var done = false;
71+
while (!done)
72+
{
73+
done = true;
74+
_stream.For((ref ushort value) =>
75+
{
76+
if (value <= 0) return;
77+
value--;
78+
done = false;
79+
});
80+
}
81+
_stream.Query.Remove<ushort>();
82+
return _stream.Count;
83+
}
84+
85+
private record Done(bool done)
86+
{
87+
public bool done { get; set; } = done;
88+
};
89+
90+
[Benchmark]
91+
public int CountDown_with_Raw_Loop()
92+
{
93+
var done = new Done(false);
94+
95+
while (!done.done)
96+
{
97+
_stream.Raw(values =>
98+
{
99+
var localDone = true;
100+
var span = values.Span;
101+
for (var i = 0; i < span.Length; i++)
102+
{
103+
if (span[i] <= 0) continue;
104+
localDone = false;
105+
span[i]--;
106+
}
107+
done.done = localDone;
108+
}
109+
);
110+
}
111+
_stream.Query.Remove<ushort>();
112+
return _stream.Count;
113+
}
114+
115+
[Benchmark]
116+
public int CountDown_with_Raw_SIMD()
117+
{
118+
var done = new Done(false);
119+
120+
while (!done.done)
121+
{
122+
_stream.Raw(values =>
123+
{
124+
var count = values.Length;
125+
var localDone = true;
126+
127+
using var mem1 = values.Pin();
128+
129+
unsafe
130+
{
131+
var p1 = (ushort*)mem1.Pointer;
132+
133+
var vectorSize = Vector256<ushort>.Count;
134+
var vectorEnd = count - count % vectorSize;
135+
for (var i = 0; i <= vectorEnd; i += vectorSize)
136+
{
137+
var v1 = Avx.LoadVector256(p1 + i);
138+
var sum = Avx2.SubtractSaturate(v1, Vector256<ushort>.One);
139+
Avx.Store(p1 + i, sum);
140+
localDone &= sum == Vector256<ushort>.Zero;
141+
}
142+
}
143+
done.done = localDone;
144+
}
145+
);
146+
}
147+
_stream.Query.Remove<ushort>();
148+
return _stream.Count;
149+
}
150+
}

fennecs.benchmarks/Conceptual/CostCentersBench.cs renamed to fennecs.benchmarks/Conceptual/QueryWideVsTall.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Benchmark.Conceptual;
55

6-
public class CostCentersBench
6+
public class QueryWideVsTall
77
{
88
[Params(10000)]
99
public int Count { get; set; }

fennecs.benchmarks/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Runtime.Intrinsics.Arm;
22
using System.Runtime.Intrinsics.X86;
33
using Benchmark;
4+
using Benchmark.Conceptual;
45
using Benchmark.ECS;
56
using BenchmarkDotNet.Configs;
67
using BenchmarkDotNet.Environments;
@@ -14,8 +15,8 @@
1415
.HideColumns("Job", "Error", "Median", "RatioSD");
1516

1617
var jobs = new List<Job>([
17-
Job.ShortRun.WithId("Default").WithRuntime(CoreRuntime.Core90),
18-
Job.ShortRun.WithId("Native").WithRuntime(NativeAotRuntime.Net90),
18+
Job.MediumRun.WithId("Default").WithRuntime(CoreRuntime.Core90),
19+
//Job.ShortRun.WithId("Native").WithRuntime(NativeAotRuntime.Net90),
1920
]);
2021

2122

@@ -31,4 +32,4 @@
3132
if (!AdvSimd.IsSupported) config.AddFilter(new CategoryExclusion(nameof(AdvSimd)));
3233

3334

34-
BenchmarkRunner.Run<DorakuBenchmarks>(config);
35+
BenchmarkRunner.Run<ForStructuralVsStateful>(config);

0 commit comments

Comments
 (0)