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:
@@ -4,11 +4,14 @@ using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Physics2D.Primitives;
|
||||
|
||||
public record AABB(Vector2D LowerBoundary, Vector2D UpperBoundary)
|
||||
public readonly struct AABB(Vector2D LowerBoundary, Vector2D UpperBoundary)
|
||||
{
|
||||
public Vector2D Center => (LowerBoundary + UpperBoundary) * .5f;
|
||||
public Vector2D Size => LowerBoundary.FromTo(UpperBoundary).Abs();
|
||||
public Vector2D SizeHalf => Size * .5f;
|
||||
public readonly Vector2D LowerBoundary { get; init; } = LowerBoundary;
|
||||
public readonly Vector2D UpperBoundary { get; init; } = UpperBoundary;
|
||||
|
||||
public readonly Vector2D Center => (LowerBoundary + UpperBoundary) * .5f;
|
||||
public readonly Vector2D Size => LowerBoundary.FromTo(UpperBoundary).Abs();
|
||||
public readonly Vector2D SizeHalf => Size * .5f;
|
||||
|
||||
public static AABB FromVectors(IEnumerable<Vector2D> vectors)
|
||||
{
|
||||
|
@@ -3,10 +3,13 @@ using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Physics2D.Primitives;
|
||||
|
||||
public record Circle(Vector2D Center, float Radius)
|
||||
public readonly struct Circle(Vector2D Center, float Radius)
|
||||
{
|
||||
public float RadiusSquared => Radius * Radius;
|
||||
public float Diameter => 2f * Radius;
|
||||
public readonly Vector2D Center { get; init; } = Center;
|
||||
public readonly float Radius { get; init; } = Radius;
|
||||
|
||||
public readonly float RadiusSquared => Radius * Radius;
|
||||
public readonly float Diameter => 2f * Radius;
|
||||
|
||||
public static Circle SetCenter(Circle circle, Vector2D center) => new(center, circle.Radius);
|
||||
public static Circle SetRadius(Circle circle, float radius) => new(circle.Center, radius);
|
||||
|
@@ -6,12 +6,15 @@ using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Physics2D.Primitives;
|
||||
|
||||
public record Line(Vector2D From, Vector2D To)
|
||||
public readonly struct Line(Vector2D From, Vector2D To)
|
||||
{
|
||||
public Line Reversed => new(To, From);
|
||||
public Vector2D Direction => From.FromTo(To).Normalize();
|
||||
public float Length => From.FromTo(To).Length();
|
||||
public float LengthSquared => From.FromTo(To).LengthSquared();
|
||||
public readonly Vector2D From { get; init; } = From;
|
||||
public readonly Vector2D To { get; init; } = To;
|
||||
|
||||
public readonly Line Reversed => new(To, From);
|
||||
public readonly Vector2D Direction => From.FromTo(To).Normalize();
|
||||
public readonly float Length => From.FromTo(To).Length();
|
||||
public readonly float LengthSquared => From.FromTo(To).LengthSquared();
|
||||
|
||||
public static LineEquation GetLineEquation(Line line)
|
||||
{
|
||||
|
@@ -2,8 +2,11 @@ using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Physics2D.Primitives;
|
||||
|
||||
public record LineEquation(float Slope, float OffsetY)
|
||||
public readonly struct LineEquation(float Slope, float OffsetY)
|
||||
{
|
||||
public readonly float Slope { get; init; } = Slope;
|
||||
public readonly float OffsetY { get; init; } = OffsetY;
|
||||
|
||||
public static float Resolve(LineEquation lineEquation, float x) => lineEquation.Slope * x + lineEquation.OffsetY; // y = mx + b
|
||||
|
||||
public static bool ApproximatelyEquals(LineEquation left, LineEquation right)
|
||||
|
@@ -1,7 +1,10 @@
|
||||
namespace Syntriax.Engine.Physics2D.Primitives;
|
||||
|
||||
public record Projection(float Min, float Max)
|
||||
public readonly struct Projection(float Min, float Max)
|
||||
{
|
||||
public readonly float Min { get; init; } = Min;
|
||||
public readonly float Max { get; init; } = Max;
|
||||
|
||||
public static bool Overlaps(Projection left, Projection right) => Overlaps(left, right, out var _);
|
||||
public static bool Overlaps(Projection left, Projection right, out float depth)
|
||||
{
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -4,9 +4,13 @@ using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Physics2D.Primitives;
|
||||
|
||||
public record Triangle(Vector2D A, Vector2D B, Vector2D C)
|
||||
public readonly struct Triangle(Vector2D A, Vector2D B, Vector2D C)
|
||||
{
|
||||
public float Area
|
||||
public readonly Vector2D A { get; init; } = A;
|
||||
public readonly Vector2D B { get; init; } = B;
|
||||
public readonly Vector2D C { get; init; } = C;
|
||||
|
||||
public readonly float Area
|
||||
=> .5f * MathF.Abs(
|
||||
A.X * (B.Y - C.Y) +
|
||||
B.X * (C.Y - A.Y) +
|
||||
|
Reference in New Issue
Block a user