-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Description
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:
- removal of interface conversion overhead;
- removal of interface method call overhead;
- reducing memory allocations, since escape analysis may leave variables on stack that is passed to interface functions;
- inlining short interface method calls and further optimizations based on the inlining.
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:
- all the interface methods for the arg implementation may be inlined (i.e. may lead to performance optimizations);
- the function body is short (i.e. reducing generated code bloat).
This could inline and optimize sort.Interface
implementations inside sort.Sort
calls.