r/Compilers • • 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.

31 Upvotes

42 comments sorted by

View all comments

Show parent comment

1

u/General_Purple3060 Jul 14 '26

In AET, genericblock$ is a keyword, not a template function call.

When the compiler finds a class containing genericblock$, it adds a generic block function address array to the object layout. The compiler resolves the required generic block implementations during link-time analysis and fills this array.

The generated code calls the generic block through the corresponding function pointer, for example:

(*aet_generic_class_fill_address[0])();

The size of this function address array is determined by the number of genericblock$ occurrences in the class. Each genericblock$ has its own function address entry.

3

u/awoocent Jul 14 '26

Indirectly calling a function pointer through a table associated with a class is dynamic dispatch!!! This is how Java lowers method calls too!

The problem is that indirect branch (calling a function pointer, v.s. directly calling a function by name/label) is slow. Both in that it's often a fairly slow operation in hardware, and in that it basically ruins the most important compiler optimization (inlining). Since Java has a JIT, it will actually go so far as to do speculative optimizations just to get rid of indirect calls, because eliminating dynamic dispatch is that important. So literally just copying this code into Java and factoring out the "generic block" into a method is not unlikely to be faster than the implementation you've proposed.

2

u/Ok_Drag_1459 Jul 15 '26

He’s using AI lol.

1

u/General_Purple3060 Jul 15 '26

Thanks for pointing this out. I think the performance concern is valid and worth investigating.

You are right that an indirect function call can affect inlining and some compiler optimizations.

However, the function table in AET is compiler-controlled, not a normal user-modifiable function pointer table. When the target is statically known, AET can eliminate the indirect call and convert it into a direct function call.

The interesting challenge is how to preserve the code-sharing benefits of genericblock$ while keeping as much optimization freedom as possible.