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

Proposal: SocketAsyncEventArgs.SetBuffer(Memory<T>) #20686

Closed
stephentoub opened this issue Mar 19, 2017 · 9 comments
Closed

Proposal: SocketAsyncEventArgs.SetBuffer(Memory<T>) #20686

stephentoub opened this issue Mar 19, 2017 · 9 comments
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Net.Sockets
Milestone

Comments

@stephentoub
Copy link
Member

See discussion here:
dotnet/corefx#17237 (comment)

@geoffkizer
Copy link
Contributor

A couple thoughts on this:

I wonder if Span (or Memory) is the way to do this. That is, if there's a way a Span can tell us that its memory is pre-pinned, then we could just add overloads that take Span (which it seems like we will probably want anyway) and then have them check if the given Span is pre-pinned. I chatted with @KrzysztofCwalina about this not too long ago; he was going to think about this...

That said, if Span is not the right way to do this, then we could just add an unsafe overload that takes (byte *, length). This allows both pre-pinned and native memory.

@davidfowl
Copy link
Member

Memory<byte> would be the way to go here once it's fleshed out. We just added Memory<T>.Pin which returns a MemoryHandle<T> https://github.com/dotnet/corefxlab/blob/91a112c619f008db7acc2e8f0a7d382889691c9b/src/System.Buffers.Primitives/System/Buffers/MemoryHandle.cs. This will pin if it needs to otherwise will return the pointer and keep the ref count up so it doesn't get disposed.

args.SetBuffer(Memory<byte> memory)

That said, if Span is not the right way to do this, then we could just add an unsafe overload that takes (byte *, length). This allows both pre-pinned and native memory.

This would be an API we could add today that would be nice as well but Memory<byte> solves the problem in a non-unsafe way. I'm open to both though.

@stephentoub
Copy link
Member Author

Since we'd want to do the Memory one anyway to work with native memory, and since I think there are other overheads to work on removing first, I suggest we just hold off until that and then add that one.

@stephentoub stephentoub changed the title Proposal: SocketAsyncEventArgs.SetBuffer(...., bool prepinned) Proposal: SocketAsyncEventArgs.SetBuffer(Memory<T>) Mar 19, 2017
@karelz
Copy link
Member

karelz commented Mar 19, 2017

Should we fold this request into #19444? It tracks all BCL places that should start using Span & friends, once the API surface is final enough.

@stephentoub
Copy link
Member Author

@davidfowl
Copy link
Member

@karelz I'm not a fan of uber issues like that. It's ok for overall tracking but each work item should have its own issue.

@karelz
Copy link
Member

karelz commented Mar 20, 2017

@davidfowl I agree that the separate issues are valuable to discuss specific design points. However, Span<T> was far from final back then (not sure where we are now) and having 10s of issues spread around was creating more confusion and distraction rather than value.
Once Span<T> is ready for its prime time in stable API surface (i.e. no huge changes like e.g. #20574), we can kick off / reopen bunch of separate issues to track specific design discussions. I assume you / @KrzysztofCwalina / @terrajobst will tell us when is the right time.

@KrzysztofCwalina
Copy link
Member

One way to make it work for pre-pinned memory would be the following:

  1. Make Memory.Pin call OwnedMemory.Pin, instead of BufferHandle.Create(_owner, _index)
  2. Make OwnedMemory.Pin virtual. Pre-pinned memory would return cached handle without actually doing any pinning.

@davidfowl
Copy link
Member

@KrzysztofCwalina that's a much less leaky way to implement it than it is today. Not sure you can cache the handle unless Free was a noop in this case (today it lowers the reference count)

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.0.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Net.Sockets
Projects
None yet
Development

No branches or pull requests

6 participants