perf: Drastically Improved Memory Usage

TIL, records are not value types and are actually just reference types. So I was pretty much allocating from heap every time I used any of my data types (Like Vector2D). Needless to say, they are all now readonly structs as I originally intended them to be.
This commit is contained in:
2024-01-26 23:40:02 +03:00
parent c32add40ff
commit b14d10db0c
15 changed files with 88 additions and 49 deletions

View File

@@ -6,13 +6,15 @@ using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Physics2D.Primitives;
public record Shape(IList<Vector2D> Vertices) : IEnumerable<Vector2D>
public readonly struct Shape(IList<Vector2D> Vertices) : IEnumerable<Vector2D>
{
public static readonly Shape Triangle = CreateNgon(3, Vector2D.Up);
public static readonly Shape Box = CreateNgon(4, Vector2D.One);
public static readonly Shape Pentagon = CreateNgon(5, Vector2D.Up);
public static readonly Shape Hexagon = CreateNgon(6, Vector2D.Right);
public readonly IList<Vector2D> Vertices { get; init; } = Vertices;
public Vector2D this[System.Index index] => Vertices[index];
@@ -89,9 +91,9 @@ public record Shape(IList<Vector2D> Vertices) : IEnumerable<Vector2D>
float min = float.MaxValue;
float max = float.MinValue;
foreach (var vertex in shape)
for (int i = 0; i < shape.Vertices.Count; i++)
{
float projectedLength = projectionVector.Dot(vertex);
float projectedLength = projectionVector.Dot(shape.Vertices[i]);
min = Math.Min(projectedLength, min);
max = Math.Max(projectedLength, max);
}