1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-14 04:15:41 +00:00

perf: use UnsafeUtility for Unity<->System conversions

This commit is contained in:
Oliver Booth 2023-03-29 00:14:49 +01:00
parent 183033cc80
commit a09492d418
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
8 changed files with 20 additions and 11 deletions

View File

@ -1,6 +1,7 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Drawing; using System.Drawing;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine; using UnityEngine;
namespace X10D.Unity.Drawing; namespace X10D.Unity.Drawing;
@ -31,6 +32,6 @@ public static class PointExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int ToUnityVector2Int(this Point value) public static Vector2Int ToUnityVector2Int(this Point value)
{ {
return new Vector2Int(value.X, value.Y); return UnsafeUtility.As<Point, Vector2Int>(ref value);
} }
} }

View File

@ -1,6 +1,7 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Drawing; using System.Drawing;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine; using UnityEngine;
using X10D.Drawing; using X10D.Drawing;
using X10D.Unity.Numerics; using X10D.Unity.Numerics;
@ -38,6 +39,6 @@ public static class PointFExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToUnityVector2(this PointF point) public static Vector2 ToUnityVector2(this PointF point)
{ {
return new Vector2(point.X, point.Y); return UnsafeUtility.As<PointF, Vector2>(ref point);
} }
} }

View File

@ -1,6 +1,7 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Drawing; using System.Drawing;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine; using UnityEngine;
namespace X10D.Unity.Drawing; namespace X10D.Unity.Drawing;
@ -19,6 +20,6 @@ public static class RectExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectangleF ToSystemRectangleF(this Rect rectangle) public static RectangleF ToSystemRectangleF(this Rect rectangle)
{ {
return new RectangleF(rectangle.x, rectangle.y, rectangle.width, rectangle.height); return UnsafeUtility.As<Rect, RectangleF>(ref rectangle);
} }
} }

View File

@ -1,6 +1,7 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Drawing; using System.Drawing;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine; using UnityEngine;
namespace X10D.Unity.Drawing; namespace X10D.Unity.Drawing;
@ -19,7 +20,7 @@ public static class RectIntExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle ToSystemRectangle(this RectInt rectangle) public static Rectangle ToSystemRectangle(this RectInt rectangle)
{ {
return new Rectangle(rectangle.x, rectangle.y, rectangle.width, rectangle.height); return UnsafeUtility.As<RectInt, Rectangle>(ref rectangle);
} }
/// <summary> /// <summary>
@ -31,6 +32,7 @@ public static class RectIntExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectangleF ToSystemRectangleF(this RectInt rectangle) public static RectangleF ToSystemRectangleF(this RectInt rectangle)
{ {
return new RectangleF(rectangle.x, rectangle.y, rectangle.width, rectangle.height); // REMARKS: implicit conversion already exists, this method is largely pointless
return rectangle.ToSystemRectangle();
} }
} }

View File

@ -1,6 +1,7 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Drawing; using System.Drawing;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine; using UnityEngine;
namespace X10D.Unity.Drawing; namespace X10D.Unity.Drawing;
@ -31,6 +32,6 @@ public static class RectangleExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectInt ToUnityRectInt(this Rectangle rectangle) public static RectInt ToUnityRectInt(this Rectangle rectangle)
{ {
return new RectInt(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); return UnsafeUtility.As<Rectangle, RectInt>(ref rectangle);
} }
} }

View File

@ -1,6 +1,7 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Drawing; using System.Drawing;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine; using UnityEngine;
namespace X10D.Unity.Drawing; namespace X10D.Unity.Drawing;
@ -19,7 +20,8 @@ public static class SizeExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToUnityVector2(this Size size) public static Vector2 ToUnityVector2(this Size size)
{ {
return new Vector2(size.Width, size.Height); // REMARKS: implicit conversion already exists, this method is largely pointless
return size.ToUnityVector2Int();
} }
/// <summary> /// <summary>
@ -31,6 +33,6 @@ public static class SizeExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int ToUnityVector2Int(this Size size) public static Vector2Int ToUnityVector2Int(this Size size)
{ {
return new Vector2Int(size.Width, size.Height); return UnsafeUtility.As<Size, Vector2Int>(ref size);
} }
} }

View File

@ -1,6 +1,7 @@
using System.Diagnostics.Contracts; using System.Diagnostics.Contracts;
using System.Drawing; using System.Drawing;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine; using UnityEngine;
namespace X10D.Unity.Drawing; namespace X10D.Unity.Drawing;
@ -19,6 +20,6 @@ public static class SizeFExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToUnityVector2(this SizeF size) public static Vector2 ToUnityVector2(this SizeF size)
{ {
return new Vector2(size.Width, size.Height); return UnsafeUtility.As<SizeF, Vector2>(ref size);
} }
} }

View File

@ -112,7 +112,7 @@ public static class Vector2Extensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PointF ToSystemPointF(this Vector2 vector) public static PointF ToSystemPointF(this Vector2 vector)
{ {
return new PointF(vector.x, vector.y); return UnsafeUtility.As<Vector2, PointF>(ref vector);
} }
/// <summary> /// <summary>
@ -124,7 +124,7 @@ public static class Vector2Extensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static SizeF ToSystemSizeF(this Vector2 vector) public static SizeF ToSystemSizeF(this Vector2 vector)
{ {
return new SizeF(vector.x, vector.y); return UnsafeUtility.As<Vector2, SizeF>(ref vector);
} }
/// <summary> /// <summary>