Skip to content

Conversation

@hexawyz
Copy link

@hexawyz hexawyz commented Jan 17, 2026

Description

While trying to vectorize the WPF Vector/Point/Matrix code with limited success, I stumbled on some low-hanging fruits that could bring noticeable performance improvements to code using Matrix.
(See here for my tentatives: https://github.com/hexawyz/WpfVectorBenchmark/tree/master/WpfVectorBenchmark/A)

This first change is very simple but does allow the JIT to inline Matrix.Identity, which was previously not the case. This might have been intentional at the time, I wouldn't know, but on the few simple benchmarks i was running, this improved performance by 10% to 23%.
Also, if this ever intentional, this would have been done at a time where support for SIMD in the runtime was inexistant, so making the code more "correct" here should delegate the decision making to the JIT.

Customer Impact

Regression

This won't break any code, but it will change the performance characteristics of the code. While I would expect the change to be mostly positive, there could be scenarios where it induces a performance regression.

Testing

Benchmark

| Method                           | Mean      | Error     | StdDev    | Code Size |
|--------------------------------- |----------:|----------:|----------:|----------:|
| Rotate_Ref                       | 21.586 ns | 0.1903 ns | 0.1780 ns |   1,115 B |
| Rotate_A                         | 15.334 ns | 0.0559 ns | 0.0496 ns |   1,124 B |
| TranslateRotateTranslate_Ref     | 11.076 ns | 0.0367 ns | 0.0325 ns |   1,409 B |
| TranslateRotateTranslate_A       |  9.640 ns | 0.0257 ns | 0.0201 ns |   1,162 B |
private static Vector_Ref Direction_Ref
{
	[MethodImpl(MethodImplOptions.NoInlining)]
	get => new(7, 5);
}

private static Vector_A Direction_A
{
	[MethodImpl(MethodImplOptions.NoInlining)]
	get => new(7, 5);
}

[Benchmark]
public Vector_Ref Rotate_Ref()
{
	var v = Direction_Ref;
	var m = Matrix_Ref.Identity;
	m.Rotate(55);
	return m.Transform(v);
}

[Benchmark]
public Vector_A Rotate_A()
{
	var v = Direction_A;
	var m = Matrix_A.Identity;
	m.Rotate(55);
	return m.Transform(v);
}

[Benchmark]
public Vector_Ref TranslateRotateTranslate_Ref()
{
	var v = Direction_Ref;
	var m = Matrix_Ref.Identity;
	m.Translate(15, 12);
	m.Rotate(55);
	m.Translate(-1, 4);
	return m.Transform(v);
}

[Benchmark]
public Vector_A TranslateRotateTranslate_A()
{
	var v = Direction_A;
	var m = Matrix_A.Identity;
	m.Translate(15, 12);
	m.Rotate(55);
	m.Translate(-1, 4);
	return m.Transform(v);
}

Risk

See the regression part

Microsoft Reviewers: Open in CodeFlow

@hexawyz hexawyz requested a review from a team as a code owner January 17, 2026 18:51
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Jan 17, 2026
@lindexi
Copy link
Member

lindexi commented Jan 19, 2026

@hexawyz I can not find the code changed. Do you miss any commit ?

@hexawyz
Copy link
Author

hexawyz commented Jan 19, 2026

@lindexi Sorry, but I don't understand what you are looking for?

@lindexi
Copy link
Member

lindexi commented Jan 19, 2026

@hexawyz Oh, sorry. I confused your two PR.

Copy link
Member

@lindexi lindexi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants