From 8e8594063d6e755608283eb55e0394f88f465603 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Tue, 22 Dec 2020 18:28:54 +0100 Subject: [PATCH] Optimize allocations in MetabolismComponent and TileAtmosphere --- Content.Server/Atmos/TileAtmosphere.cs | 4 ++-- .../Components/Metabolism/MetabolismComponent.cs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Content.Server/Atmos/TileAtmosphere.cs b/Content.Server/Atmos/TileAtmosphere.cs index 0e210865d0..a0c669e3cd 100644 --- a/Content.Server/Atmos/TileAtmosphere.cs +++ b/Content.Server/Atmos/TileAtmosphere.cs @@ -574,7 +574,7 @@ namespace Content.Server.Atmos [MethodImpl(MethodImplOptions.AggressiveInlining)] private void FinalizeEq() { - var transferDirections = new float[Atmospherics.Directions]; + Span transferDirections = stackalloc float[Atmospherics.Directions]; var hasTransferDirs = false; for (var i = 0; i < Atmospherics.Directions; i++) { @@ -609,7 +609,7 @@ namespace Content.Server.Atmos } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void FinalizeEqNeighbors(in float[] transferDirs) + private void FinalizeEqNeighbors(ReadOnlySpan transferDirs) { for (var i = 0; i < Atmospherics.Directions; i++) { diff --git a/Content.Server/GameObjects/Components/Metabolism/MetabolismComponent.cs b/Content.Server/GameObjects/Components/Metabolism/MetabolismComponent.cs index bb459bf544..775dfa8cb6 100644 --- a/Content.Server/GameObjects/Components/Metabolism/MetabolismComponent.cs +++ b/Content.Server/GameObjects/Components/Metabolism/MetabolismComponent.cs @@ -145,20 +145,20 @@ namespace Content.Server.GameObjects.Components.Metabolism private float SuffocatingPercentage() { - var percentages = new float[Atmospherics.TotalNumberOfGases]; + var total = 0f; foreach (var (gas, deficit) in DeficitGases) { - if (!NeedsGases.TryGetValue(gas, out var needed)) + var lack = 1f; + if (NeedsGases.TryGetValue(gas, out var needed)) { - percentages[(int) gas] = 1; - continue; + lack = deficit / needed; } - percentages[(int) gas] = deficit / needed; + total += lack / Atmospherics.TotalNumberOfGases; } - return percentages.Average(); + return total; } private float GasProducedMultiplier(Gas gas, float usedAverage)