-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: net, os, syscall, internal/poll: Add ControlWith, ReadWith, WriteWith to RawConn interface #51170
Comments
The current usage doesn't seem like a big problem in practice. Can you point to code examples that would clearly benefit from this approach? If we do this, then I don't think we should use |
The main benefit is that the function literal does not escape to heap. An example of code is in net/sendfile_linux.go:
Here, in the function literal, 4 outer variables causes the function to escape:
Yes, in practice, this is not a big problem, but if we need to eliminate the unnecessary memory allocation caused by the escape of the function literal, at least this provides a way, so is meaningful. With previous APIs, we can't eliminate the memory allocation, as long as we refer something outside of the function literal, it always escape. For the type of arg, |
I wonder why escape analysis isn't good enough here? |
I have no idea. The calling path is
Pprof shows these 3 lines cause memory allocation, all of which are in function
These allocations cause noticeable performance reduction on benchmarking. |
If the problem is escape analysis, it sounds like we should fix that for the code snippets above. |
This proposal has been added to the active column of the proposals project |
Thanks! If a change to compiler is feasible in this case, it should be a better choice to solve this issue. |
Filed #51334 for the compiler change. This seems like a likely decline. |
Based on the discussion above, this proposal seems like a likely decline. |
OK, I'll follow the compiler change, please close this issue. |
No change in consensus, so declined. |
The RawConn interface enables us controlling, reading, and writing net.Conn and os.File with direct syscalls. Currently it is defined as:
This definition has some inconvenience:
To overcome these inconvenience, and keep backward compatible, I propose adding these 3 methods to RawConn interface:
Here, we add another argument, named arg, to f, so we can pass more info needed by f. We also add an error to the returned value of f to capture its errors, which is returned directly by these 3 methods as error, so no function literals are needed. Meanwhile, the real argument is passed to these 3 methods in addition to f, which is in turn passed to f when calling f in the implementation of these 3 methods.
By doing this, we eliminate all of the inconvenience, and pprof shows the function literals don't escape to heap while it can still get what it wants, improving the performance.
This proposal affects package net, os, syscall, and internal/poll, but the effect is small and limited. The main implementation is in internal/poll. Taking WriteWith (in package internal/poll, it will be called RawWriteWith) as an example:
The semantics of these methods are consistent with existing Control, Read, and Write. The design is also consistent.
Any thoughts?
The text was updated successfully, but these errors were encountered: