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

API Proposal: SafeBuffer Span<T> methods #27831

Closed
vcsjones opened this issue Nov 6, 2018 · 22 comments · Fixed by #40842
Closed

API Proposal: SafeBuffer Span<T> methods #27831

vcsjones opened this issue Nov 6, 2018 · 22 comments · Fixed by #40842
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime.InteropServices
Milestone

Comments

@vcsjones
Copy link
Member

vcsjones commented Nov 6, 2018

Proposal:

namespace System.Runtime.InteropServices
{
    public abstract unsafe class SafeBuffer
    {
        public void ReadSpan<T>(ulong byteOffset, Span<T> buffer) where T : struct => throw null;
        public void WriteSpan<T>(ulong byteOffset, ReadOnlySpan<T> data) where T : struct => throw null;
    }
}

The purpose of this is to be a Span-based equivalent of ReadArray. The motivation for this is for a later API proposal to add the same methods to UnmanagedMemoryAccessor which will work for memory mapped views.

This would make it possible to work efficiently and safely with stackalloc'ed data.

@karelz
Copy link
Member

karelz commented Nov 12, 2018

@stephentoub
Copy link
Member

Seems reasonable to me. These are just direct span-based equivalents of the existing array methods:

public void ReadArray<T>(ulong byteOffset, T[] array, int index, int count) where T : struct;
public void WriteArray<T>(ulong byteOffset, T[] array, int index, int count) where T : struct;

@jeffschwMSFT
Copy link
Member

cc @AaronRobinsonMSFT

@AaronRobinsonMSFT
Copy link
Member

Not necessarily against these, but if I recall the SafeBuffer class is marked as obsolete. I don't see it in code, but it is on docs.microsoft.com - Seems kind of silly to be extending an obsolete API?

Other than perhaps placement, this seems like a good idea.

@stephentoub
Copy link
Member

obsolete

Interesting. What does it mean for SafeBuffer to be obsolete but SafeMemoryMappedViewHandle derived from it to not be?

@AaronRobinsonMSFT
Copy link
Member

@stephentoub That is a great question. This has been marked this way for a long time if I recall - well before I came on the scene. Can you think of anyone who might know why this class was officially marked as obsolete?

@AaronRobinsonMSFT
Copy link
Member

Looks like this is happening because of something from netstandard 1.2 😮 Perhaps a mistake?

https://github.com/dotnet/dotnet-api-docs/blob/233109a79714842b6ce02e919ad1cb078267f434/xml/System.Runtime.InteropServices/SafeBuffer.xml#L43-L45

@jkotas
Copy link
Member

jkotas commented Nov 22, 2018

People were pretty liberal in early days of .NET Core with marking things obsolete. Most/all of it was rolled back. I am not able to find the history for this particular one, but here are a few related PRs:

dotnet/corefx#17748
dotnet/corefx#10541

@vcsjones
Copy link
Member Author

I think it's possible to implement the proposal directly on SafeMemoryMappedViewHandle which is what I was going for anyway.

We have access to the handle and length via ByteLength.

Does it make more sense to do that or try and undeprecate SafeBuffer?

@jkotas
Copy link
Member

jkotas commented Nov 24, 2018

SafeBuffer is not deprecated. The documentation needs to be fixed: dotnet/dotnet-api-docs#1184

@AaronRobinsonMSFT
Copy link
Member

@vcsjones Now that the SafeBuffer discussion is out of the way 😄 I think this is a good suggestion. I am all for it.

@vcsjones
Copy link
Member Author

vcsjones commented Dec 8, 2018

Happy to do a PR for this when it gets through API review.

@ahsonkhan
Copy link
Member

We didn't discuss the parameter names in the review. Should they both be called span to match the array overloads (or both be called buffer)? Why is one called buffer and the other data?

public void ReadSpan<T>(ulong byteOffset, Span<T> buffer) where T : struct => throw null;
public void WriteSpan<T>(ulong byteOffset, ReadOnlySpan<T> data) where T : struct => throw null;

@vcsjones
Copy link
Member Author

Why is one called buffer and the other data?

Hm. Probably just a poor decision on my part when writing this up.

Thould they both be called span to match the array overloads (or both be called buffer)?

I think I avoided span for both because one is a ReadOnlySpan and calling it span seemed out of place. If that is an ill-placed thought then span works for me, as does buffer.

@GrabYourPitchforks
Copy link
Member

Looks like this is unassigned. @vcsjones did you want to take this or should we mark it up for grabs?

@vcsjones
Copy link
Member Author

@GrabYourPitchforks I'm happy to pick it up; wanted to know how we should resolve @ahsonkhan's feedback post API Review process.

@GrabYourPitchforks
Copy link
Member

It's fine to call the parameter name span IMO. We do this for other APIs which accept ReadOnlySpan<T> as a parameter. Check https://apisof.net/catalog/System.MemoryExtensions.IndexOf%3CT%3E(ReadOnlySpan%3CT%3E,T) for one such example.

@ahsonkhan
Copy link
Member

It's fine to call the parameter name span IMO. We do this for other APIs which accept ReadOnlySpan<T> as a parameter. Check https://apisof.net/catalog/System.MemoryExtensions.IndexOf%3CT%3E(ReadOnlySpan%3CT%3E,T) for one such example.

Granted, that's an extension method. I think either span or buffer is fine as long as we are consistent.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@jeffschwMSFT jeffschwMSFT removed the untriaged New issue has not been triaged by the area owner label Feb 24, 2020
@AaronRobinsonMSFT
Copy link
Member

@vcsjones Where did we land on this? Did I miss the PR?

@stephentoub
Copy link
Member

This is marked as api-approved. Presumably what's next is just for someone to implement it 😄

@vcsjones
Copy link
Member Author

vcsjones commented Apr 8, 2020

Hm, I guess that can be me. I see I offered a little while ago but failed to follow through. Apologies.

@stephentoub
Copy link
Member

No need to apologize :) Thanks for the offer.

@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 15, 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.Runtime.InteropServices
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants