mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
perf: Grid2D initial capacity to avoid warmup allocations
This commit is contained in:
parent
a6f450710c
commit
3e4b5b3660
@ -18,22 +18,27 @@ public struct Grid2D<T>
|
|||||||
// => makes the code a lot easier too
|
// => makes the code a lot easier too
|
||||||
// => this is FINE because in the worst case, every grid position in the
|
// => this is FINE because in the worst case, every grid position in the
|
||||||
// game world is filled with a player anyway!
|
// game world is filled with a player anyway!
|
||||||
readonly Dictionary<Vector2Int, HashSet<T>> grid =
|
readonly Dictionary<Vector2Int, HashSet<T>> grid;
|
||||||
new Dictionary<Vector2Int, HashSet<T>>();
|
|
||||||
|
|
||||||
// cache a 9 neighbor grid of vector2 offsets so we can use them more easily
|
// 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,
|
grid = new Dictionary<Vector2Int, HashSet<T>>(initialCapacity);
|
||||||
Vector2Int.up + Vector2Int.left,
|
|
||||||
Vector2Int.up + Vector2Int.right,
|
neighbourOffsets = new[] {
|
||||||
Vector2Int.left,
|
Vector2Int.up,
|
||||||
Vector2Int.zero,
|
Vector2Int.up + Vector2Int.left,
|
||||||
Vector2Int.right,
|
Vector2Int.up + Vector2Int.right,
|
||||||
Vector2Int.down,
|
Vector2Int.left,
|
||||||
Vector2Int.down + Vector2Int.left,
|
Vector2Int.zero,
|
||||||
Vector2Int.down + Vector2Int.right
|
Vector2Int.right,
|
||||||
};
|
Vector2Int.down,
|
||||||
|
Vector2Int.down + Vector2Int.left,
|
||||||
|
Vector2Int.down + Vector2Int.right
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// helper function so we can add an entry without worrying
|
// helper function so we can add an entry without worrying
|
||||||
public void Add(Vector2Int position, T value)
|
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
|
// initialize set in grid if it's not in there yet
|
||||||
if (!grid.TryGetValue(position, out HashSet<T> hashSet))
|
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;
|
grid[position] = hashSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +40,8 @@ public enum CheckMethod
|
|||||||
public bool showSlider;
|
public bool showSlider;
|
||||||
|
|
||||||
// the grid
|
// 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
|
// project 3d world position to grid position
|
||||||
Vector2Int ProjectToGrid(Vector3 position) =>
|
Vector2Int ProjectToGrid(Vector3 position) =>
|
||||||
|
@ -11,7 +11,7 @@ public class Grid2DTests
|
|||||||
[SetUp]
|
[SetUp]
|
||||||
public void SetUp()
|
public void SetUp()
|
||||||
{
|
{
|
||||||
grid = new Grid2D<int>();
|
grid = new Grid2D<int>(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
Loading…
Reference in New Issue
Block a user