From 7057c7ca77a1fe1a39ce9a496020cfee8f610f79 Mon Sep 17 00:00:00 2001
From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Date: Sun, 10 Apr 2022 04:45:57 +1200
Subject: [PATCH] Reduce array resizing in GasTileOverlay (#7479)
---
.../Atmos/EntitySystems/GasTileOverlaySystem.cs | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/Content.Server/Atmos/EntitySystems/GasTileOverlaySystem.cs b/Content.Server/Atmos/EntitySystems/GasTileOverlaySystem.cs
index 6403937002..a0acf783ec 100644
--- a/Content.Server/Atmos/EntitySystems/GasTileOverlaySystem.cs
+++ b/Content.Server/Atmos/EntitySystems/GasTileOverlaySystem.cs
@@ -187,11 +187,17 @@ namespace Content.Server.Atmos.EntitySystems
///
private List GetChunksInRange(EntityUid entity)
{
- var inRange = new List();
-
// This is the max in any direction that we can get a chunk (e.g. max 2 chunks away of data).
var (maxXDiff, maxYDiff) = ((int) (_updateRange.X / ChunkSize) + 1, (int) (_updateRange.Y / ChunkSize) + 1);
+ // Setting initial list size based on the theoretical max number of chunks from a single grid. For default
+ // parameters, this is currently 6^2 = 36. Unless a player is near more than one grid, this is will
+ // generally slightly over-estimate the actual list size, which will be either 25, 30, or 36 (assuming the
+ // player is not near the edge of a grid).
+ var initialListSize = (1 + (int) MathF.Ceiling(2 * _updateRange.X / ChunkSize)) * (1 + (int) MathF.Ceiling(2 * _updateRange.Y / ChunkSize));
+
+ var inRange = new List(initialListSize);
+
var xform = Transform(entity);
var worldPos = xform.MapPosition;
@@ -219,8 +225,8 @@ namespace Content.Server.Atmos.EntitySystems
// (e.g. if we're on the very edge of a chunk we may need more chunks).
var (xDiff, yDiff) = (chunkIndices.X - entityTile.X, chunkIndices.Y - entityTile.Y);
- if (xDiff > 0 && xDiff > _updateRange.X ||
- yDiff > 0 && yDiff > _updateRange.Y ||
+ if (xDiff > _updateRange.X ||
+ yDiff > _updateRange.Y ||
xDiff < 0 && Math.Abs(xDiff + ChunkSize) > _updateRange.X ||
yDiff < 0 && Math.Abs(yDiff + ChunkSize) > _updateRange.Y) continue;