replace List to ConcurrentQueue
add EcsSpan extension
This commit is contained in:
Mikhail 2023-12-31 17:22:56 +08:00
parent b03c0480d1
commit f7e9c9e055
2 changed files with 18 additions and 11 deletions

View File

@ -1,12 +1,16 @@
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public static class EcsGroupExtensions public static class EcsCollectionsExtensions
{ {
public static void IterateParallel(this EcsGroup self, EcsThreadHandler worker, int minSpanSize) public static void IterateParallel(this EcsGroup self, EcsThreadHandler worker, int minSpanSize)
{ {
IterateParallel(self, worker, minSpanSize); ThreadRunner.Run(worker, self.ToSpan(), minSpanSize);
} }
public static void IterateParallel(this EcsReadonlyGroup self, EcsThreadHandler worker, int minSpanSize) public static void IterateParallel(this EcsReadonlyGroup self, EcsThreadHandler worker, int minSpanSize)
{
ThreadRunner.Run(worker, self.ToSpan(), minSpanSize);
}
public static void IterateParallel(this EcsSpan self, EcsThreadHandler worker, int minSpanSize)
{ {
ThreadRunner.Run(worker, self, minSpanSize); ThreadRunner.Run(worker, self, minSpanSize);
} }

View File

@ -1,5 +1,6 @@
using DCFApixels.DragonECS.ClassicThreadsInternal; using DCFApixels.DragonECS.ClassicThreadsInternal;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
@ -13,7 +14,7 @@ namespace DCFApixels.DragonECS
private static EcsThreadHandler _worker; private static EcsThreadHandler _worker;
private static EcsThreadHandler _nullWorker = delegate { }; private static EcsThreadHandler _nullWorker = delegate { };
private static int[] _entities = new int[64]; private static int[] _entities = new int[64];
private static List<Exception> _catchedExceptions; private static ConcurrentQueue<Exception> _catchedExceptions = new ConcurrentQueue<Exception>();
private static bool _isRunning = false; private static bool _isRunning = false;
@ -33,9 +34,7 @@ namespace DCFApixels.DragonECS
} }
catch (Exception e) catch (Exception e)
{ {
if (_catchedExceptions == null) _catchedExceptions.Enqueue(e);
_catchedExceptions = new List<Exception>();
_catchedExceptions.Add(e);
record.doneWork.Set(); record.doneWork.Set();
} }
} }
@ -59,20 +58,24 @@ namespace DCFApixels.DragonECS
_worker = _nullWorker; _worker = _nullWorker;
} }
public static void Run(EcsThreadHandler worker, EcsReadonlyGroup entities, int minSpanSize) public static void Run(EcsThreadHandler worker, EcsSpan entities, int minSpanSize)
{ {
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (_isRunning) Throw.DoubleParallelIteration(); if (_isRunning) Throw.DoubleParallelIteration();
#endif #endif
_isRunning = true; _isRunning = true;
_worker = worker; _worker = worker;
int entitiesCount = entities.Bake(ref _entities); int entitiesCount = entities.Length; //entities.Bake(ref _entities);
int threadsCount = entitiesCount / minSpanSize; int threadsCount = entitiesCount / minSpanSize;
if (entitiesCount % minSpanSize > 0) if (entitiesCount % minSpanSize > 0)
{
threadsCount++; threadsCount++;
}
if (threadsCount > _maxThreadsCount) if (threadsCount > _maxThreadsCount)
{
threadsCount = _maxThreadsCount; threadsCount = _maxThreadsCount;
}
if (threadsCount > 1) if (threadsCount > 1)
{ {
@ -112,10 +115,10 @@ namespace DCFApixels.DragonECS
_isRunning = false; _isRunning = false;
_worker = _nullWorker; _worker = _nullWorker;
if (_catchedExceptions != null) if (_catchedExceptions.Count > 0)
{ {
var exceptions = _catchedExceptions; Exception[] exceptions = _catchedExceptions.ToArray();
_catchedExceptions = null; _catchedExceptions.Clear();
throw new AggregateException(exceptions); throw new AggregateException(exceptions);
} }
} }