-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
System.Numerics.Vectors and Span<T> #25608
Comments
Thanks for the suggestion @dangi12012. Can you please refer to the API review process guidelines here.
For reference. here are the existing CopyTo methods on Vector3: public struct Vector3 : IEquatable<Vector3>, IFormattable
{
public void CopyTo(float[] array);
public void CopyTo(float[] array, int index);
} cc @eerhardt |
If you add the following code to Vector2,3,4 and Vector:
This is faster than what we currently have for not unsupported Vectormath. (Vector is very lacking with horizontal operations and intrinsics) The speed can even be improved to 0 copy, with the new ref return in C# 7: |
I would widen this request to initialize a Vector with Span. Currently you always have to allocate an array to feed the new instance of Vector with new data. In some scenarios where you really need performance (e.g. bilinear interpolation) you need to fill a lot of interpolation partners. Currently, first you have to load your interpolation partners in a newly allocated arrays, which you only need to create your Vector. For this situations it makes much more sense to stackalloc the memory for your interpolation partners that you refill with new data in a loop. If the stackalloc'ed memory can be Spanned there's no additional cost in allocating memory. |
|
Tuple deconstruction should be in its own issue and follow the good example as well @dangi12012. |
@tannergooding: You are right, CopyTo is there, but correct me if I'm wrong there's nothing for the constructor, right? So I cannot do something like
Currently only this is possible:
which could cause pressure on the GC when doing this in a loop in x/x direction over many data points. |
The The |
@tannergooding: Thanks a lot for pointing this out. I did it today, but I looked it up again. According to the docs it only works for .NET Standard 2.1 Preview, whereas I am on .NET Standard 2.0. |
But you can do float* x = stackalloc float[] { 10, 20, 30, 40 };
Vector<float> v = Unsafe.ReadUnaligned<Vector<float>>(x); |
@benaadams 👍 This might help. I'll give it a try. |
@dangi12012, could you please update the original post to follow the "good example" listed under step 1 of our API Review Process? You don't need to provide all sections, but ideally you would provide the rationale and proposed API surface sections as those help the API review process. |
We think spanifying the constructor and CopyTo methods makes sense, but don't think adding the byte-based variants makes sense at the time. Semantics of these members should match what namespace System.Numerics
{
public static partial struct Vector2
{
public Vector2(ReadOnlySpan<float> values);
public readonly void CopyTo(Span<float> destination);
public readonly bool TryCopyTo(Span<float> destination);
}
public static partial struct Vector3
{
public Vector3(ReadOnlySpan<float> values);
public readonly void CopyTo(Span<float> destination);
public readonly bool TryCopyTo(Span<float> destination);
}
public static partial struct Vector4
{
public Vector4(ReadOnlySpan<float> values);
public readonly void CopyTo(Span<float> destination);
public readonly bool TryCopyTo(Span<float> destination);
}
} |
I would like to work on this if the issue could be assigned to me, thanks. |
Done! Let us know if you need help, @C-xC-c |
Please think about implementing this operator:
Since the span can be a stackallocated Array the performance gain should be huge! Also Vertical Operators are missing from Vector like T Min(Vector vector)
The text was updated successfully, but these errors were encountered: