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

Vector<T> should have a ctor that accepts ReadOnlySpan<T> #26763

Closed
GrabYourPitchforks opened this issue Jul 10, 2018 · 7 comments · Fixed by dotnet/corefx#37473
Closed

Vector<T> should have a ctor that accepts ReadOnlySpan<T> #26763

GrabYourPitchforks opened this issue Jul 10, 2018 · 7 comments · Fixed by dotnet/corefx#37473
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Numerics
Milestone

Comments

@GrabYourPitchforks
Copy link
Member

Currently the vector type has the following span-based ctor overload.

public struct Vector<T> {
    public Vector(Span<T> values);
}

We should add a readonly span-based ctor overload so that vectors can be read from immutable data buffers, such as a ReadOnlySpan<char> backed by a String. This will make it easier to vectorize text processing APIs without falling back to the dangerous Unsafe or MemoryMarshal classes.

public struct Vector<T> {
    /* new ctor overload proposal */
    public Vector(ReadOnlySpan<T> values);

    /* new instance method proposals */
    public void CopyTo(Span<T> destination);
    public bool TryCopyTo(Span<T> destination);
}
@GrabYourPitchforks
Copy link
Member Author

I updated the proposal with CopyTo / TryCopyTo, since if we're reading vectors from buffers it'll also be good to write vectors to buffers. :)

@4creators
Copy link
Contributor

Perhaps we should add above APIs to HWIntrinsics VectorNNN<T> what partially would heal wounds of some devs as discussed in dotnet/corefx#18926

@evilguest
Copy link

Where can I see the Vector(Span) constructor? The one available in System.Numerics.Vectors 4.1.4.0 does have only the T[]-based constructors :(

@Joe4evr
Copy link
Contributor

Joe4evr commented Aug 10, 2018

@dangi12012
Copy link

THIS NEADS TO HAPPEN!
Vector should have a ctor that accepts ReadOnlySpan
Vector should MUST also support CopyTo(Span mySpan)
since if we get it from a buffer we would like to store the results there too ;)

@terrajobst
Copy link
Member

terrajobst commented Sep 25, 2018

Video

@GrabYourPitchforks also suggested these APIs:

public struct Vector<T> {
    /* new ctor overload proposal */
    public Vector(ReadOnlySpan<byte> values);
   
    /* new instance method proposals */
    public void CopyTo(Span<byte> destination);
    public bool TryCopyTo(Span<byte> destination);
}

This would avoid consumers having to re-interpret case the spans. We don't know what happens when the Vector<T> is instantiated with byte though. @GrabYourPitchforks, please take a look and file another item.

@GrabYourPitchforks GrabYourPitchforks self-assigned this Mar 19, 2019
@GrabYourPitchforks
Copy link
Member Author

I also checked out the Vector<byte> behavior with regard to the dueling overloads. In this case, if you call new Vector<byte>(new ReadOnlySpan<byte>(new byte[32])), the compiler prefers the ROS<byte> overload instead of the ROS<T> overload. (The same is true for CopyTo.) This behavior is perfectly fine since both the ROS<byte> and the ROS<T> ctors have the exact same behaviors when T = byte.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Numerics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants