From 8fa27157b76dbe3df71462547d8a098c9e5d7c6d Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 12 Jun 2023 13:35:54 +0800 Subject: [PATCH] even split --- src/ThreadRunner.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/ThreadRunner.cs b/src/ThreadRunner.cs index 126a5ea..56dce0e 100644 --- a/src/ThreadRunner.cs +++ b/src/ThreadRunner.cs @@ -76,14 +76,20 @@ namespace DCFApixels.DragonECS if (threadsCount > 1) { - int spanSize = entitiesCount / (threadsCount - 1); - for (int i = 0; i < threadsCount; i++) + int remainder = entitiesCount % threadsCount; + int quotient = entitiesCount / threadsCount; + for (int i = 0, start = 0; i < threadsCount; i++) { ref var thread = ref _threads[i]; - thread.start = i * spanSize; - thread.size = spanSize; + thread.start = start; + thread.size = quotient; + if (remainder > 0) + { + thread.size++; + remainder--; + } + start += thread.size; } - _threads[threadsCount - 1].size = entities.Count % (threadsCount - 1); } else {