How Devirtualization Works in Go

Go devirtualization optimizes interface calls by replacing indirect dispatch with direct function calls using static analysis or runtime profiling data.

Devirtualization in Go is a compiler optimization that replaces indirect interface or function calls with direct calls to a known concrete implementation to enable further optimizations like inlining. The compiler performs this in two ways: "Static" devirtualization analyzes code at compile time to identify the concrete type, while "Profile-guided" devirtualization uses runtime profiling data to insert a conditional check for the most frequently used callee. This process occurs in the middle-end of the compiler within the cmd/compile/internal/devirtualize package, specifically via the StaticCall and ProfileGuided functions, which transform OCALLINTER nodes into OCALLMETH or conditional direct calls.