Refactor single-thread worker

Re-parallel Hull Update
Use new gap shaffle algorithm
This commit is contained in:
NotAlwaysTrue
2026-04-30 20:05:23 +08:00
parent 099d664731
commit 02689d0d86
6 changed files with 90 additions and 26 deletions
@@ -814,7 +814,7 @@ namespace Barotrauma
if (outsideCollisionBlocker == null) { return false; }
if (IsRoomToRoom || Submarine == null || open <= 0.0f || linkedTo.Count == 0 || linkedTo[0] is not Hull)
{
SingleThreadWorker.GlobalWorker.AddAction(() =>
SingleThreadWorker.Instance.AddAction(() =>
{
if (outsideCollisionBlocker == null) { return; }
outsideCollisionBlocker.Enabled = false;
@@ -888,22 +888,24 @@ namespace Barotrauma
Oxygen -= OxygenDeteriorationSpeed * deltaTime;
if (FakeFireSources.Count > 0)
SingleThreadWorker.Instance.AddAction(() =>
{
if ((Character.Controlled?.CharacterHealth?.GetAffliction("psychosis")?.Strength ?? 0.0f) <= 0.0f)
if (FakeFireSources.Count > 0)
{
for (int i = FakeFireSources.Count - 1; i >= 0; i--)
if ((Character.Controlled?.CharacterHealth?.GetAffliction("psychosis")?.Strength ?? 0.0f) <= 0.0f)
{
if (FakeFireSources[i].CausedByPsychosis)
for (int i = FakeFireSources.Count - 1; i >= 0; i--)
{
FakeFireSources[i].Remove();
if (FakeFireSources[i].CausedByPsychosis)
{
FakeFireSources[i].Remove();
}
}
}
FireSource.UpdateAll(FakeFireSources, deltaTime);
}
FireSource.UpdateAll(FakeFireSources, deltaTime);
}
FireSource.UpdateAll(FireSources, deltaTime);
FireSource.UpdateAll(FireSources, deltaTime);
});
foreach (Decal decal in decals)
{
@@ -8,6 +8,7 @@ using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Xml.Linq;
using static OneOf.Types.TrueFalseOrNull;
namespace Barotrauma
{
@@ -642,6 +643,7 @@ namespace Barotrauma
/// </summary>
public static void UpdateAll(float deltaTime, Camera cam, ParallelOptions parallelOptions)
{
Random rand = new Random();
#if CLIENT
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
@@ -652,9 +654,15 @@ namespace Barotrauma
var structureList = Structure.WallList.ToList();
List<Gap> gapList = Gap.GapList.ToList();
List<Gap> shuffledGaps = new List<Gap>(gapList?.OrderBy(g => Rand.Int(int.MaxValue)));
// In case if it failed, but why it would fail?
shuffledGaps = shuffledGaps ?? gapList;
// This should never break again... right?
int n = gapList.Count;
while (n > 1)
{
n--;
int k = rand.Next(n + 1);
(gapList[n], gapList[k]) = (gapList[k], gapList[n]);
}
var itemList = Item.ItemList.ToList();
@@ -662,11 +670,11 @@ namespace Barotrauma
Parallel.Invoke(parallelOptions,
() =>
{
// basically nothing here is thread-safe so
foreach (var hull in hullList)
Parallel.ForEach(hullList, parallelOptions, hull =>
{
hull.Update(deltaTime, cam);
}
});
},
// Structure parallel update
() =>
@@ -685,7 +693,7 @@ namespace Barotrauma
// moved waterflow reset here to see if we can reduce at least some time
{
// PLEASE WORK
Parallel.ForEach(shuffledGaps, parallelOptions, gap =>
Parallel.ForEach(gapList, parallelOptions, gap =>
{
gap.ResetWaterFlowThisFrame();
gap.Update(deltaTime, cam);
@@ -698,8 +706,6 @@ namespace Barotrauma
}
);
SingleThreadWorker.GlobalWorker.RunActions();
#if CLIENT
// Hull Cheats need to be executed after Hull update
Hull.UpdateCheats(deltaTime, cam);