Skip to content

Commit

Permalink
End of workday, zzz.
Browse files Browse the repository at this point in the history
  • Loading branch information
thygrrr committed Nov 12, 2024
1 parent 301d71d commit 8b2a6e4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
59 changes: 32 additions & 27 deletions fennecs.benchmarks/Conceptual/ForStructuralVsStateful.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,44 @@ namespace Benchmark.Conceptual;
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByParams)]
public class ForStructuralVsStateful
{
[Params(10_000)]
[Params(100_000)]
public int Entities { get; set; }

[Params(0.9f, 0.5f, 0.1f)]
public float Homogenity { get; set; }
[Params(100, 1_000, 10_000)]

[Params(10_000)]
public int Actions { get; set; }

private World _world = null!;
private Stream<ushort> _stream = null!;
private Random _random = null!;

[GlobalSetup]
public void GlobaSetup()
public void GlobalSetup()
{
_world = new(Entities * 30);

_stream = _world.Query<ushort>().Stream();

_world.Spawn().Add(1).Despawn();

_world.Spawn().Add<ushort>(1).Despawn();
_world.Spawn().Add<ushort>(1);
_stream.Query.Remove<ushort>();
}

[IterationSetup]
public void IterationSetup()
{
Console.WriteLine("IterationSetup");
_world.All.Despawn();
if (_world.Count != 0) throw new("World is not empty!");

_random = new(69);

_world.Entity().Spawn(Entities);
foreach (var entity in _world.All.ToArray())
{
entity.Add((ushort) _random.Next((ushort) (Actions * Homogenity), Actions));
entity.Add((ushort)_random.Next((ushort)(Actions * Homogenity), Actions));
}
}

Expand Down Expand Up @@ -99,7 +101,7 @@ public int CountDown_NewFor_Structural_ChangeSD()
{
while (_stream.Count > 0)
{
_stream.For(static delegate (RW<ushort> value)
_stream.For(static delegate(RW<ushort> value)
{
value.write--;
if (value.read <= 0) value.Remove();
Expand Down Expand Up @@ -133,24 +135,21 @@ public int CountDown_with_Delayed_Change()
{
if (value <= 0) return;
value--;
done = false;
done = false;
});
}
_stream.Query.Remove<ushort>();
return _stream.Count;
}

private record Done(bool done)
{
public bool done { get; set; } = done;
};
private record struct Flag(bool done);

[Benchmark]
public int CountDown_with_Raw_Loop()
{
var done = new Done(false);
var flag = new Flag(false);

while (!done.done)
while (!flag.done)
{
_stream.Raw(values =>
{
Expand All @@ -162,7 +161,7 @@ public int CountDown_with_Raw_Loop()
localDone = false;
span[i]--;
}
done.done = localDone;
flag.done = localDone;
}
);
}
Expand All @@ -173,15 +172,15 @@ public int CountDown_with_Raw_Loop()
[Benchmark]
public int CountDown_with_Raw_SIMD()
{
var done = new Done(false);
var flag = new Flag(false);

while (!done.done)
while (!flag.done)
{
_stream.Raw(values =>
{
var count = values.Length;
var localDone = true;

using var mem1 = values.Pin();

unsafe
Expand All @@ -190,17 +189,23 @@ public int CountDown_with_Raw_SIMD()

var vectorSize = Vector256<ushort>.Count;
var vectorEnd = count - count % vectorSize;

for (var i = 0; i <= vectorEnd; i += vectorSize)
{
var v1 = Avx.LoadVector256(p1 + i);
var sum = Avx2.SubtractSaturate(v1, Vector256<ushort>.One);
Avx.Store(p1 + i, sum);
localDone &= sum == Vector256<ushort>.Zero;
}

for (var i = vectorEnd; i < count; i++) // remaining elements
{
localDone &= p1[i] == 0;
p1[i] -= 1;
}
}
done.done = localDone;
}
);
flag.done = localDone;
});
}
_stream.Query.Remove<ushort>();
return _stream.Count;
Expand Down
7 changes: 7 additions & 0 deletions fennecs/Delegates.1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ namespace fennecs;
public delegate void EntityComponentActionR<C0>(EntityRef entity, R<C0> comp0) where C0 : notnull;

#endregion


#region Raw: Memory Actions
public delegate void MemoryActionR<C0>(ReadOnlyMemory<C0> comp0);
public delegate void MemoryActionW<C0>(Memory<C0> comp0);

#endregion
1 change: 1 addition & 0 deletions fennecs/Stream.2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public void For(ComponentActionRR<C0, C1> action)

#endregion


#region Entity Stream.For

/// <include file='XMLdoc.xml' path='members/member[@name="T:For"]'/>
Expand Down
1 change: 1 addition & 0 deletions fennecs/fennecs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<AssemblyName>fennecs</AssemblyName>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>true</IsPackable>
<Nullable>enable</Nullable>
<PackageId>fennecs</PackageId>
<Version>0.6.0-beta</Version>
Expand Down

0 comments on commit 8b2a6e4

Please sign in to comment.