-
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
cmd/compile: rewrite interface args by actual type args in functions called only with a single set of argument types #19165
Comments
cc @randall77 , @dr2chase |
This optimization is also known as devirtualization in C++ compilers. |
How would this impact stack traces appearing after a panic that refer to rewritten funcs? |
It's not obvious to me how this works with package-at-a-time compilation. For example, it would seem to require re-compiling package sort when a new type satisfying sort.Interface shows up. This can be done semi-manually with an source code rewriter/specializer--which the sort package in fact has internally. :) |
@josharian what about unexported funcs? |
How often does that happen? This optimization would (I think) take pretty significant effort to implement. And if all the code is in the same package, then it could be specialized manually or with a source code generator, like in package sort. |
One example that comes to mind is funcs that you want to test, so you make them take interfaces to help with the mocking. If you made it take an interface just for that, when compiling in non-test mode the interface type could be swapped with a single specific type. I don't think you could specialize this manually, and I think this pattern is fairly well regarded. I agree that this optimization would probably be far from trivial and most interface uses wouldn't benefit, though (unless inlining takes place?). |
Forgot mentioning that the devirtualization may help escape analysis leaving more variables on stack. For instance, currently all the variables passed to interface functions are escaped because the compiler doesn't know all the function implementations which may be used in the program. With the devirtualization step the compiler may leave part of such variables on stack.
This shouldn't change stack traces at all
I don't know how the current compiler works, but here is a naive sketch how the devirtualization may be implemented:
|
Sometimes programs use only a single set of interface implementations for every function call accepting interface args. In this case the compiler may safely rewrite such functions, so they'll accept actual type args instead of interface args.
This brings the following benefits:
The idea may be extended further. For instance, the compiler may generate specialized functions for each unique set of arg types passed to interface args (aka
"generic" functions
:)) . But naive implementation may lead to uncontrolled code bloat and performance degradation, so it must be carefully designed beforehand. One possible idea - specializing the function only if the following conditions are met:This could inline and optimize
sort.Interface
implementations insidesort.Sort
calls.The text was updated successfully, but these errors were encountered: