-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: add io.LimitedWriter to make more convenient to implement zero copy #46465
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
Comments
I'm not sure this needs to be a proposal. |
@ianlancetaylor I have update this issue. Because we need add a new type io.LimiterWriter, I think proposal is appropriate. |
I'm sorry, I do not understand this explanation. Why do we need |
We need the |
@jim3ma For Since |
Example: jim3ma@5faae18#diff-f2f92ffe0abe8dd3c833d435c2d859d54380e8e4160af8becab6945395563cfeR2818 Without With We need to limit the writing bytes wrote to
So |
Summary: Why implement in |
Wouldn't it make more sense to add this speedup to the |
Why do you need to limit the number of bytes written to the file? Who needs to impose that limit? If the need is for the |
The timing is uncanny, but I actually have a usecase. I'm transmitting large files over a tcp connection but in a binary format containing the offset, length of data (lets call this the header) and then the data itself. For each section in the tcp stream the header is parsed. The header information is used to seek to the correct location in the target file. After that an |
Thanks. I completely understand why user code needs to use |
Agreed. From a users perspective I also don't see a reason to expose a new API. I think this was added by the author of this issue to make the implementation easier, but it would make more sense to make this an implementation detail of the aforementioned methods. |
Change https://golang.org/cl/324473 mentions this issue: |
@ianlancetaylor Added a very rudimentary implementation of a fast path for TCPConn to *os.File. With support for an Also not sure if this is the correct issue to track or if I should create a new one that supersedes this one. This being my first contribution to the go project I'm a bit lost ;) |
@svenwiltink Thanks for sending the CL. In this case there is already an active CL for this work: https://golang.org/cl/319593. I believe that @jim3ma opened this issue because I questioned the need for io.LimitedWriter on that CL. |
Ah, fair enough. I'll abandon my CL then for now. I'll add my comments to the existing CL instead to not start the same conversation in two places. Thank you for your time anyways :) |
@svenwiltink @ianlancetaylor It's easy to speedup to the |
OK, an internal API is appropriate now. |
@svenwiltink @ianlancetaylor |
We are in the 1.17 release freeze right now. The patch will be reviewed for 1.18. Thanks. Closing this issue as we no longer need this new API. |
Currently, io.LimitedReader make it easy in
x.ReadFrom
. Sometimes, we need to make code changes iny.WriteTo
, because typey
holds the underlaying connections we can not get inx.ReadFrom
. So we need the io.LimitedWriter and adding io.LimitedWriter to match io.LimitedReader seems plausibleUse case: add http.persistConn.WriteTo to make zero copy possible
http.persistConn
holds theconn
, we can not get it in other packages.Implement
http.persistConn.WriteTo
to call conn.WriteTo is a good way (conn.WriteTo is the next use case).Code changes [WIP]:
jim3ma@5faae18#diff-f2f92ffe0abe8dd3c833d435c2d859d54380e8e4160af8becab6945395563cfeR1939
Use case: add net.TCPConn.WriteTo func to enable splice socket data to file
When transfer data from tcp socket to file, splice will help us with low time cost in linux.
We get 10% at least cpu time reduce, zero memory copy from kernel to user space.
Implement PR: #46149
About why add code changes with io.LimitedWriter:
First, I want to use io.LimitedReader to implement zero copy, but can not make changes to os.File due to cycle import.
net
importsos
, we can not detectr.(*net.TCPConn)
. The only way we can do is implement zero copy onnet.TCPConn
, but we need a type like io.LimitedReader to holds the count of writing bytes.Code changes:
jim3ma@c0c9d9a#diff-0adad05e90d38731c53f2b006db41d2cdb4772b55f83bdd98486a9e2f95d1db4R54
The text was updated successfully, but these errors were encountered: