Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate the conversion between SKMatrix44 and Matrix4x4 #2778

Closed
mattleibow opened this issue Mar 2, 2024 · 1 comment · Fixed by #2780
Closed

Investigate the conversion between SKMatrix44 and Matrix4x4 #2778

mattleibow opened this issue Mar 2, 2024 · 1 comment · Fixed by #2780
Labels
tenet/performance Performance related issues

Comments

@mattleibow
Copy link
Contributor

Description

Right now we are creating a new struct each time, but it may be better to use Unsafe.As:

var m = Unsafe.As<Matrix4x4, SKMatrix44>(ref sys);

We need to benchmark and make sure.

@mattleibow mattleibow added the tenet/performance Performance related issues label Mar 2, 2024
@mattleibow
Copy link
Contributor Author

mattleibow commented Mar 2, 2024

I created a benchmark:

[Benchmark(Baseline = true)]
public SKMatrix44 UseNewStruct()
{
	SKMatrix44.UseUnsafeAs = false;

	SKMatrix44 TheSKMatrix44 = SKMatrix44.CreateRotation(10, 20, 30, 0.7f);
	var inverted = TheSKMatrix44.Invert();
	return inverted;
}

[Benchmark]
public SKMatrix44 UseUnsafeAs()
{
	SKMatrix44.UseUnsafeAs = true;

	SKMatrix44 TheSKMatrix44 = SKMatrix44.CreateRotation(10, 20, 30, 0.7f);
	var inverted = TheSKMatrix44.Invert();
	return inverted;
}

And edited the code of SKMatrix44:

public static bool UseUnsafeAs = false;

public static implicit operator Matrix4x4 (SKMatrix44 matrix) =>
	UseUnsafeAs ? Unsafe.As<SKMatrix44, Matrix4x4> (ref matrix) :
	new Matrix4x4 (
		matrix.m00, matrix.m10, matrix.m20, matrix.m30,
		matrix.m01, matrix.m11, matrix.m21, matrix.m31,
		matrix.m02, matrix.m12, matrix.m22, matrix.m32,
		matrix.m03, matrix.m13, matrix.m23, matrix.m33);

public static implicit operator SKMatrix44 (Matrix4x4 matrix) =>
	UseUnsafeAs ? Unsafe.As<Matrix4x4, SKMatrix44> (ref matrix) :
	new SKMatrix44 (
		matrix.M11, matrix.M21, matrix.M31, matrix.M41,
		matrix.M12, matrix.M22, matrix.M32, matrix.M42,
		matrix.M13, matrix.M23, matrix.M33, matrix.M43,
		matrix.M14, matrix.M24, matrix.M34, matrix.M44);

The results:

// * Summary *

BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.22631.3155)
Intel Core i9-9980HK CPU 2.40GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK=8.0.200
  [Host]   : .NET 7.0.16 (7.0.1624.6629), X64 RyuJIT AVX2
  .NET 7.0 : .NET 7.0.16 (7.0.1624.6629), X64 RyuJIT AVX2

Job=.NET 7.0  Runtime=.NET 7.0

|       Method |      Mean |    Error |   StdDev | Ratio | RatioSD | Allocated | Alloc Ratio |
|------------- |----------:|---------:|---------:|------:|--------:|----------:|------------:|
| UseNewStruct | 100.83 ns | 3.085 ns | 8.702 ns |  1.00 |    0.00 |         - |          NA |
|  UseUnsafeAs |  59.51 ns | 1.093 ns | 0.853 ns |  0.59 |    0.05 |         - |          NA |

// * Hints *
Outliers
  TheBenchmark.UseNewStruct: .NET 7.0 -> 8 outliers were removed (132.01 ns..159.35 ns)
  TheBenchmark.UseUnsafeAs: .NET 7.0  -> 3 outliers were removed (68.31 ns..72.48 ns)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tenet/performance Performance related issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant