r/Compilers • u/General_Purple3060 • Jul 13 '26
Delayed Specialization: A Third Way to Implement Generics?
While implementing generics in my GCC-based language (AET), I wasn't satisfied with the two mainstream approaches:
- C++ Templates: Generate a full copy of the code for every concrete type (monomorphization) → code bloat and longer compile times.
- Java Generics: Use type erasure → no code duplication, but lose concrete type information.
So I explored a middle path: Delayed Specialization.
How it works in AET:
During the first compilation:
- Generic parameters (E, T, ...) are treated as void*
- Code that needs the real type is wrapped in a genericblock$
For example:
class$ Abc<E>{
void setData(E value);
};
impl$ Abc{
void setData(E value) {
E a = value;
genericblock$(a) {
E x = a;
E y = 5;
x += y;
}
}
};When the compiler later sees a concrete instantiation like Abc<int>, it performs a second compilation pass only on the Generic Blocks, replacing E with int.
Benefits:
- Avoids C++-style template explosion
- Keeps most generic code shared (like Java)
- Still allows real type-specific operations where needed
I call this Delayed Specialization. It sits between full monomorphization and type erasure.
Has anyone seen a similar approach in other languages or compilers? I'd love to hear about papers or existing implementations using delayed/late specialization.
21
u/jonathanhiggs Jul 13 '26
C# calls it late binding, absolutely a better approach than Java. C# also has the advantage of being able to JIT generics only once when used at runtime vs c++ has to do it per TU so just repeats the same work over and over again
10
u/awoocent Jul 13 '26
There's nothing "delayed" about this. In another one of your comments you say "AET delays it only until the concrete type is known during compilation" - this is exactly what C++ and Rust do. It would in fact be much much weirder if a compiler didn't wait until a concrete type was known before instantiating a function!
The main thing I think you've omitted here is that in order to have a single generic implementation of
setData, you'll need to load the dynamic type ofain order to dispatch to the right implementation of thegenericblock$. Which is like, fine? Basically the same as an interface method call. But I do think it's kind of a critical cost you basically haven't mentioned that makesgenericblock$behave much less like "a critical section of specialized code" and much more like "defining a dynamically-dispatched method inline".7
u/SwedishFindecanor Jul 13 '26
That reminded me that Swift does some kind of hybrid. I searched to try to find where I had read about it... and found that this topic is a bigger rabbit hole than I had thought. (I'm posting the link just because it seems to be interesting reading).
3
u/matthieum Jul 13 '26
I'm confused. Doesn't your process also ends up generating a full copy of the code for every concrete type (after the specialization pass)?
Does your language allocates all types on the heap, Java-style?
Otherwise, I would argue that
E a = value;is generic, since it requires knowing the size and alignment ofE(and possibly how to copy/move it).I think that, personally, my greatest concern about monomorphization... is that monomorphization is just like inlining: applying it blindly just leads to bloat.
In C++, it's all the more striking that even "phantom types", which are just used for type safety, actually lead to completely duplicate code, with perhaps some function merging if you're lucky.
I've been wondering if partial monomorphization shouldn't be the name of the game. That is, only monomorphizing on what cannot be abstract away -- alignment & size for example -- but otherwise rely on passing virtual tables by default, and then use monomorphization heuristics just like there's inline heuristics, knowing that many small functions would be inlined first, allowing the compiler to elide virtual tables, etc... and never having to monomorphize fully.
3
u/dnabre Jul 14 '26
I'm not seeing the distinction between your method and monomorphism. You end up with a specialized copy of the generic code for each concrete type it used with, right? How a compiler break up the process pass-wise isn't really relevant. Sorry if I'm missing something obvious here.
1
u/antoyo Jul 13 '26
I'm not sure I understand. Is the idea that doing this in 2 passes allows the compiler to only generate once a generic function even if it is instantiated at multiple places?
1
u/antoyo Jul 13 '26
Since you're based on GCC, I was wondering if you managed to be able to speed-up compilation by somehow asking GCC to run some optimizations that can be ran on generic code (like constant-folding) in order to avoid running the same optimizations multiple times for different instantiations. I don't know if GCC supports this, but since its IR is higher-level than LLVM IR, maybe this is possible.
1
u/Inevitable-Ant1725 Jul 13 '26
This is interesting. Why not save space by only factoring out the parts that have to change?
1
u/c-cul Jul 14 '26
is it like poor generics in go with 2 thunks?
1
u/phischu Jul 14 '26
In our paper on monomorphization we likewise separate the whole process into three phases. First, we collect the immediate flow of types into type variables, then we compute the transitive closures of these, then we specialize the program based on the result. The first and the last must inspect the whole program but can be streaming.
I must admit I did not know that Rust and C++ sometimes specialize a type in the same way more than once. I found it hard to believe but apparently it is true. Holy moly.
1
u/thisisserezha Aug 08 '26
I'm a bit late to the party.
There is a paper "Existentialize Your Generics" which discusses similar approach and builds on and compares with Swift's foundation.