diff --git a/Robust.Benchmarks/NumericsHelpers/Box2RotatedBenchmark.cs b/Robust.Benchmarks/NumericsHelpers/Box2RotatedBenchmark.cs index 3515af670..134966c69 100644 --- a/Robust.Benchmarks/NumericsHelpers/Box2RotatedBenchmark.cs +++ b/Robust.Benchmarks/NumericsHelpers/Box2RotatedBenchmark.cs @@ -1,4 +1,6 @@ -using System.Numerics; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.Intrinsics; using BenchmarkDotNet.Attributes; using Robust.Shared.Analyzers; using Robust.Shared.Maths; @@ -16,3 +18,43 @@ public Matrix3x2 GetTransform() return Box.Transform; } } + +[Virtual, DisassemblyDiagnoser] +public class Box2RotatedBoundingBoxBenchmark +{ + public Box2Rotated ZeroRotationBox = new(new Box2(-1, -2, 3, 4), Angle.Zero, new Vector2(0.25f, -0.5f)); + public Box2Rotated RotatedBox = new(new Box2(-1, -2, 3, 4), 0.7f, new Vector2(0.25f, -0.5f)); + + private Box2 _boxResult; + + [Benchmark(Baseline = true)] + public void CurrentZeroRotation() + { + _boxResult = ZeroRotationBox.CalcBoundingBox(); + } + + [Benchmark] + public void NoCheckZeroRotation() + { + _boxResult = CalcBoundingBoxNoRotationCheck(ZeroRotationBox); + } + + [Benchmark] + public void CurrentRotated() + { + _boxResult = RotatedBox.CalcBoundingBox(); + } + + [Benchmark] + public void NoCheckRotated() + { + _boxResult = CalcBoundingBoxNoRotationCheck(RotatedBox); + } + + private static Box2 CalcBoundingBoxNoRotationCheck(in Box2Rotated box) + { + box.GetVertices(out var x, out var y); + var aabb = SimdHelpers.GetAABB(x, y); + return Unsafe.As, Box2>(ref aabb); + } +} diff --git a/Robust.Shared.Maths/Box2Rotated.cs b/Robust.Shared.Maths/Box2Rotated.cs index 0b84b6d55..076a65a07 100644 --- a/Robust.Shared.Maths/Box2Rotated.cs +++ b/Robust.Shared.Maths/Box2Rotated.cs @@ -94,8 +94,12 @@ public readonly Box2Rotated Enlarged(float value) /// /// Calculates the smallest AABB that will encompass the rotated box. The AABB is in local space. /// + [Pure] public readonly Box2 CalcBoundingBox() { + if (Rotation == Angle.Zero) + return Box; + GetVertices(out var x, out var y); var aabb = SimdHelpers.GetAABB(x, y); return Unsafe.As, Box2>(ref aabb);