From f7e9c9e05586bca043a7101f219c650aaee85f4b Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 31 Dec 2023 17:22:56 +0800 Subject: [PATCH] update replace List to ConcurrentQueue add EcsSpan extension --- ...ensions.cs => EcsCollectionsExtensions.cs} | 8 +++++-- src/ThreadRunner.cs | 21 +++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) rename src/{EcsGroupExtensions.cs => EcsCollectionsExtensions.cs} (54%) diff --git a/src/EcsGroupExtensions.cs b/src/EcsCollectionsExtensions.cs similarity index 54% rename from src/EcsGroupExtensions.cs rename to src/EcsCollectionsExtensions.cs index d5343fd..cae9511 100644 --- a/src/EcsGroupExtensions.cs +++ b/src/EcsCollectionsExtensions.cs @@ -1,12 +1,16 @@ namespace DCFApixels.DragonECS { - public static class EcsGroupExtensions + public static class EcsCollectionsExtensions { 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) + { + ThreadRunner.Run(worker, self.ToSpan(), minSpanSize); + } + public static void IterateParallel(this EcsSpan self, EcsThreadHandler worker, int minSpanSize) { ThreadRunner.Run(worker, self, minSpanSize); } diff --git a/src/ThreadRunner.cs b/src/ThreadRunner.cs index b4c338f..b5bc798 100644 --- a/src/ThreadRunner.cs +++ b/src/ThreadRunner.cs @@ -1,5 +1,6 @@ using DCFApixels.DragonECS.ClassicThreadsInternal; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; @@ -13,7 +14,7 @@ namespace DCFApixels.DragonECS private static EcsThreadHandler _worker; private static EcsThreadHandler _nullWorker = delegate { }; private static int[] _entities = new int[64]; - private static List _catchedExceptions; + private static ConcurrentQueue _catchedExceptions = new ConcurrentQueue(); private static bool _isRunning = false; @@ -33,9 +34,7 @@ namespace DCFApixels.DragonECS } catch (Exception e) { - if (_catchedExceptions == null) - _catchedExceptions = new List(); - _catchedExceptions.Add(e); + _catchedExceptions.Enqueue(e); record.doneWork.Set(); } } @@ -59,20 +58,24 @@ namespace DCFApixels.DragonECS _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 (_isRunning) Throw.DoubleParallelIteration(); #endif _isRunning = true; _worker = worker; - int entitiesCount = entities.Bake(ref _entities); + int entitiesCount = entities.Length; //entities.Bake(ref _entities); int threadsCount = entitiesCount / minSpanSize; if (entitiesCount % minSpanSize > 0) + { threadsCount++; + } if (threadsCount > _maxThreadsCount) + { threadsCount = _maxThreadsCount; + } if (threadsCount > 1) { @@ -112,10 +115,10 @@ namespace DCFApixels.DragonECS _isRunning = false; _worker = _nullWorker; - if (_catchedExceptions != null) + if (_catchedExceptions.Count > 0) { - var exceptions = _catchedExceptions; - _catchedExceptions = null; + Exception[] exceptions = _catchedExceptions.ToArray(); + _catchedExceptions.Clear(); throw new AggregateException(exceptions); } }