Skip to content

cmd/compile: rewrite interface args by actual type args in functions called only with a single set of argument types #19165

@valyala

Description

@valyala

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions