perf: Grid2D initial capacity to avoid warmup allocations

This commit is contained in:
vis2k 2022-09-17 16:11:56 +08:00
parent a6f450710c
commit 3e4b5b3660
3 changed files with 25 additions and 16 deletions

View File

@ -18,22 +18,27 @@ public struct Grid2D<T>
// => makes the code a lot easier too
// => this is FINE because in the worst case, every grid position in the
// game world is filled with a player anyway!
readonly Dictionary<Vector2Int, HashSet<T>> grid =
new Dictionary<Vector2Int, HashSet<T>>();
readonly Dictionary<Vector2Int, HashSet<T>> grid;
// cache a 9 neighbor grid of vector2 offsets so we can use them more easily
readonly Vector2Int[] neighbourOffsets =
readonly Vector2Int[] neighbourOffsets;
public Grid2D(int initialCapacity)
{
Vector2Int.up,
Vector2Int.up + Vector2Int.left,
Vector2Int.up + Vector2Int.right,
Vector2Int.left,
Vector2Int.zero,
Vector2Int.right,
Vector2Int.down,
Vector2Int.down + Vector2Int.left,
Vector2Int.down + Vector2Int.right
};
grid = new Dictionary<Vector2Int, HashSet<T>>(initialCapacity);
neighbourOffsets = new[] {
Vector2Int.up,
Vector2Int.up + Vector2Int.left,
Vector2Int.up + Vector2Int.right,
Vector2Int.left,
Vector2Int.zero,
Vector2Int.right,
Vector2Int.down,
Vector2Int.down + Vector2Int.left,
Vector2Int.down + Vector2Int.right
};
}
// helper function so we can add an entry without worrying
public void Add(Vector2Int position, T value)
@ -41,7 +46,10 @@ public void Add(Vector2Int position, T value)
// initialize set in grid if it's not in there yet
if (!grid.TryGetValue(position, out HashSet<T> hashSet))
{
hashSet = new HashSet<T>();
// each grid entry may hold hundreds of entities.
// let's create the HashSet with a large initial capacity
// in order to avoid resizing & allocations.
hashSet = new HashSet<T>(128);
grid[position] = hashSet;
}

View File

@ -40,7 +40,8 @@ public enum CheckMethod
public bool showSlider;
// the grid
Grid2D<NetworkConnectionToClient> grid = new Grid2D<NetworkConnectionToClient>();
// begin with a large capacity to avoid resizing & allocations.
Grid2D<NetworkConnectionToClient> grid = new Grid2D<NetworkConnectionToClient>(1024);
// project 3d world position to grid position
Vector2Int ProjectToGrid(Vector3 position) =>

View File

@ -11,7 +11,7 @@ public class Grid2DTests
[SetUp]
public void SetUp()
{
grid = new Grid2D<int>();
grid = new Grid2D<int>(10);
}
[Test]