GC shape stenciling in Go generics 0 ▲ Redowan's Reflections 1 hour ago · 6 min read1278 words · Tech · hide · 0 comments While going through the Go generics proposal, I got curious about how the compiler implements it. Compilers usually handle generics in one of two ways: With full monomorphization, the compiler turns generic code into concrete, type-specific code. It generates a separate version for every set of type arguments the program uses. Rust works this way, and so do C++ templates. With type erasure, the compiler keeps one shared version of the generic code and replaces the type parameters with a common type. Java erases them to Object or to their declared bounds. Full monomorphization gives the compiler exact types for every generated function. It can optimize each one like ordinary code, and the generic abstraction adds no runtime overhead. The drawback is that every distinct set of type arguments can add another function body, which increases compile time and binary size. Erasure is at the opposite end of the spectrum. There is only one body to compile, but the concrete types are gone at… No comments yet. Log in to reply on the Fediverse. Comments will appear here.