Generics implementation - GC Shape Stenciling

submited by
Style Pass
2021-10-28 03:00:07

This document describes a method to implement the Go generics proposal by stenciling the code for each different GC shape of the instantiated types, and using a dictionary to handle differing behaviors of types that have the same shape.

This proposal is middle ground between the Generics Implementation - Stenciling and Generics Implementation - Dictionaries proposals.

The GC shape of a type means how that type appears to the allocator / garbage collector. It is determined by its size, its required alignment, and which parts of the type contain a pointer.

When we generate code for a generic function, we will generate a single chunk of assembly for each unique GC shape used by any instantiation. Each chunk of assembly will take as an argument a dictionary, which is a set of information describing the particular concrete types that the parameterized types take on. It includes the concrete types themselves, of course, but also derived information as we will see below.

The most important feature of a dictionary is that it is compile-time computeable. All dictionaries will reside in the read-only data section, and will be passed around by reference. Anything they reference (types, other dictionaries, etc.) must also be in the read-only or code data sections.

Leave a Comment