Skip to content

Commit

Permalink
refactor EntityQueryParallelExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
sicusa committed Oct 20, 2023
1 parent 942707e commit 8d736fd
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions Sia/Entities/Extensions/EntityQueryParallelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public static class EntityQueryParallelExtensions
{
private readonly struct ForEachParallelAction
{
private readonly EntityRef[] _array;
private readonly ArraySegment<EntityRef> _array;
private readonly EntityHandler _handler;

public ForEachParallelAction(EntityRef[] array, EntityHandler handler)
public ForEachParallelAction(ArraySegment<EntityRef> array, EntityHandler handler)
{
_array = array;
_handler = handler;
Expand All @@ -27,11 +27,11 @@ public void Invoke(System.Tuple<int, int> range)

private readonly struct ForEachParallelAction<TData>
{
private readonly EntityRef[] _array;
private readonly ArraySegment<EntityRef> _array;
private readonly TData _data;
private readonly EntityHandler<TData> _handler;

public ForEachParallelAction(EntityRef[] array, in TData data, EntityHandler<TData> handler)
public ForEachParallelAction(ArraySegment<EntityRef> array, in TData data, EntityHandler<TData> handler)
{
_array = array;
_data = data;
Expand All @@ -49,17 +49,17 @@ public void Invoke(System.Tuple<int, int> range)
private unsafe struct ForEachParallelData
{
public int* Index;
public EntityRef[] Array;
public ArraySegment<EntityRef> Array;

public ForEachParallelData(int* index, EntityRef[] array)
public ForEachParallelData(int* index, ArraySegment<EntityRef> array)
{
Index = index;
Array = array;
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe static void RecordEntities(IEntityQuery query, EntityRef[] array)
private unsafe static void DoRecord(IEntityQuery query, ArraySegment<EntityRef> array)
{
int index = 0;
var indexPtr = (int*)Unsafe.AsPointer(ref index);
Expand All @@ -71,15 +71,26 @@ private unsafe static void RecordEntities(IEntityQuery query, EntityRef[] array)
});
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe SpanOwner<EntityRef> Record(this IEntityQuery query)
{
var count = query.Count;
if (count == 0) { return default; }

var spanOwner = SpanOwner<EntityRef>.Allocate(count);
DoRecord(query, spanOwner.DangerousGetArray());
return spanOwner;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void ForEachParallel(this IEntityQuery query, EntityHandler handler)
{
var count = query.Count;
if (count == 0) { return; }

using var spanOwner = SpanOwner<EntityRef>.Allocate(count);
var array = spanOwner.DangerousGetArray().Array!;
RecordEntities(query, array);
var spanOwner = SpanOwner<EntityRef>.Allocate(count);
var array = spanOwner.DangerousGetArray();
DoRecord(query, array);

var action = new ForEachParallelAction(array, handler);
Partitioner.Create(0, count)
Expand All @@ -93,9 +104,9 @@ public static unsafe void ForEachParallel<TData>(this IEntityQuery query, in TDa
var count = query.Count;
if (count == 0) { return; }

using var spanOwner = SpanOwner<EntityRef>.Allocate(count);
var array = spanOwner.DangerousGetArray().Array!;
RecordEntities(query, array);
var spanOwner = SpanOwner<EntityRef>.Allocate(count);
var array = spanOwner.DangerousGetArray();
DoRecord(query, array);

var action = new ForEachParallelAction<TData>(array, data, handler);
Partitioner.Create(0, count)
Expand Down

0 comments on commit 8d736fd

Please sign in to comment.