GC shape stenciling in Go generics
2 days ago
- #GC shape stenciling
- #Go generics
- #compiler implementation
- GC shape stenciling is Go's approach to generics, monomorphizing based on types' GC shapes rather than full monomorphization or type erasure.
- GC shape refers to how a type appears to the allocator and garbage collector: size, alignment, and pointer-containing parts; pointer types share one shape.
- Implementation involves generating separate function bodies per GC shape and dictionaries for each instantiation to provide type-specific information at runtime.
- Shared bodies incur runtime costs for type-dependent operations like method calls and type assertions, which rely on dictionaries for exact type data.
- Generics initially slowed compiler performance, but improvements in Go 1.20 brought build speeds back in line with pre-generics versions.