System.Windows.Media.Matrix: Span-based Transform Overloads
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
`System.Windows.Media.Matrix` supports transforming multiple `Point` or `Vector` instances in a single call by invoking one of the following overloads:
- ```csharp
public void Transform (System.Windows.Point[] points);
```
- ```csharp
public void Transform (System.Windows.Vector[] vectors);
```
We have a performance-sensitive use case where we have a `double[]` array instance with values laid out just perfectly for a `System.Span`, but there's no API for that.
Looking at [the implementations](https://github.com/dotnet/wpf/blob/e5a22ef4d9d3b82d0c8d24ccccfd9d3ab3e9e12b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Media/Matrix.cs#L356-L369) of [both methods](https://github.com/dotnet/wpf/blob/e5a22ef4d9d3b82d0c8d24ccccfd9d3ab3e9e12b/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Media/Matrix.cs#L385-L398), I don't see any technical blockers for creating overloads that accept spans.
As-is, I find myself choosing between one of the following two implementations, which I don't really like doing without opening an issue like this:
## Option 1: use span, one-by-one
```csharp
foreach (ref Point pt in MemoryMarshal.Cast(someDoubleArray))
{
pt = someMatrix.Transform(pt);
}
```
## Option 2: use arrays, bulk
```csharp
Span pts = MemoryMarshal.Cast(someDoubleArray);
Point[] ptsArray = pts.ToArray();
someMatrix.Transform(ptsArray);
ptsArray.AsSpan().CopyTo(pts);
```
*Full disclosure: our application is actually still on .NET Framework 4.8... we're eventually planning to port to .NET 5+, so I'm opening this request in advance in the hopes that it might get attention between now and then.*
Contributor guide
Assessment
This issue has not been assessed yet.