dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.44k stars 4.76k forks source link

.NET Zero-copy API #74191

Open gzak opened 4 years ago

gzak commented 4 years ago

Add support for a zero-copy API, akin to java.nio.channels.FileChannel's transferTo() method.

General

It would be great if .NET had a built-in way of doing zero-copy steam transfers, especially for large blobs (like files). Most operating systems appear to support this, so it's just a matter of exposing some API to do this in .NET. By adding it to .NET standard, both .NET Core and .NET Framework can both benefit from this API.

davidfowl commented 4 years ago

This https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.sendfile?view=netcore-3.1?

gzak commented 4 years ago

Well then...

Last question before I close, is zero-copy usually restricted to file->network transfers only? What about transferring bits between arbitrary streams (file->file, for example)?

davidfowl commented 4 years ago

https://docs.microsoft.com/en-us/dotnet/api/system.io.stream.copytoasync?view=netcore-3.1

gzak commented 4 years ago

But I don't think that's a zero-copy type of API. From what I can see it copies bytes from one stream into the heap, then sends the bytes back out to the other stream. I would imagine a zero-copy version to be "direct" - maybe that's not possible in general, only for file->socket use cases?

davidfowl commented 4 years ago

No it’s not possible in the general case. CopyToAsync is the closest you can get to though. It’s optimized on most stream implementations to do the minimal amount of work.

kesshei commented 3 years ago

Need Zero-copy, Support hight data transmission for .Net core .

davidfowl commented 3 years ago

Usually to move things like this forward, we need an API proposal with some motivating scenarios that somebody can champion and push forward. Otherwise issues like this aren't looked at by the team until a motivating scenario comes up.

John0King commented 3 years ago

I'm asking in https://github.com/dotnet/runtime/issues/46848, but not get the answer for file to file copy. until I find this article. https://www.cnblogs.com/z-sm/p/6547709.html

it seems Java's FileChannel have the ablity to do disk to disk copy too, and they even can have position and length paramerters

and the oracle doc : https://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html

davidfowl commented 3 years ago

.NET has send file exposed, if you want something more specific, you have to be more specific. The reason issues like these are closed are because of a lack of evidence of a miscommunication about the scenario. Reading this dotnet/runtime#46848 I can understand why it got closed:

Here's an issue I filed about taking advantage of splice(2) to accomplish zero copy to unix pipe.

terrajobst commented 2 years ago

.NET Standard is complete, so I'm moving this to dotnet/runtime as a feature request.

dotnet-issue-labeler[bot] commented 2 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.

Issue Details
Add support for a [zero-copy API](https://en.wikipedia.org/wiki/Zero-copy), akin to java.nio.channels.FileChannel's transferTo() method. # General It would be great if .NET had a built-in way of doing zero-copy steam transfers, especially for large blobs (like files). Most operating systems appear to support this, so it's just a matter of exposing some API to do this in .NET. By adding it to .NET standard, both .NET Core and .NET Framework can both benefit from this API.
Author: gzak
Assignees: -
Labels: `area-System.IO`, `untriaged`
Milestone: -