From 8850d4996052dff4f844a1aa2a82be0b2bc5eeca Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Mon, 8 Apr 2019 13:35:18 +0300 Subject: [PATCH] (3f75a3756) Minor optimization: removing hudbars don't generate garbage anymore (the collection usually contains only one item and the loop was not evaluated many times, but it still doesn't hurt to fix it, especially when there's a todo note about it). --- .../Source/Characters/Character.cs | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Characters/Character.cs b/Barotrauma/BarotraumaClient/Source/Characters/Character.cs index 3be451f56..03c78d45d 100644 --- a/Barotrauma/BarotraumaClient/Source/Characters/Character.cs +++ b/Barotrauma/BarotraumaClient/Source/Characters/Character.cs @@ -275,6 +275,7 @@ namespace Barotrauma DisableControls = false; } + private List> progressBarRemovals = new List>(); partial void UpdateControlled(float deltaTime, Camera cam) { if (controlled != this) return; @@ -283,25 +284,22 @@ namespace Barotrauma Lights.LightManager.ViewTarget = this; CharacterHUD.Update(deltaTime, this, cam); - - bool removeProgressBars = false; - - foreach (HUDProgressBar progressBar in hudProgressBars.Values) + + if (hudProgressBars.Any()) { - if (progressBar.FadeTimer <= 0.0f) + foreach (var progressBar in hudProgressBars) { - removeProgressBars = true; - continue; + if (progressBar.Value.FadeTimer <= 0.0f) + { + progressBarRemovals.Add(progressBar); + continue; + } + progressBar.Value.Update(deltaTime); } - progressBar.Update(deltaTime); - } - - if (removeProgressBars) - { - // TODO: this generates garbage, can we fix anything here? - foreach (var pb in hudProgressBars.Where(pb => pb.Value.FadeTimer <= 0.0f).ToList()) + if (progressBarRemovals.Any()) { - hudProgressBars.Remove(pb.Key); + progressBarRemovals.ForEach(pb => hudProgressBars.Remove(pb.Key)); + progressBarRemovals.Clear(); } } }