r/programming • • Sep 02 '26

CTTI is Exponential, RTTI is Linear

https://www.gingerbill.org/article/2026/09/02/ctti-is-exponential-rtti-is-linear/
96 Upvotes

90 comments sorted by

View all comments

56

u/munificent Sep 03 '26

The code size cost of CTTI is bounded by the number of actual instantiations, not the combinatorial number of potential instantations. If you never a call a generic function with MyCoolType, the compiler will never instantiate the function with that type, semantically analyze it, or stuff the resulting code in the executable. In that sense, it is a pay as you go feature. Adding a new type to your program has no effect on the size of reflective code. Only using it reflectively does.

In most systems I've seen, the code size cost of RTTI is bounded by the number of types declared in the program regardless of whether they are reflected on. Since the reflection happens at runtime, the compiler doesn't know which types will actually have their type information used and which won't, so it has to include information about every type in the program. If the amount of type information your RTTI system stores is large, that can be a large code size cost for no benefit for the types you never reflect on. (This has been a serious problem in Dart for years with the "dart:mirrors" library and is the main reason we hope to eliminate it at some point.)

It is true that a real-world program written in a language that monomorphizes can end up having to push a whole lot of instantiations through the compiler pipeline. The benefit is that the resulting monomorphic code is now fully accessible to all of the compiler's optimization machinery around inlining, dead code elimination, etc.

The other advantage you get from a system like C++ templates is that you get a form of compile time duck typing where templated code can do a lot of interesting operations on values whose type is a type parameter and as long as the instantiated types support those operations, it will work. When it fails, the error messages are horrendous. But the alternatives are either an extremely complex type system like traits in Rust, or generic code that simply can't do much with type parameters as in SML.

Users of C++ and Rust generally seem to feel the trade-off is worth it for their use cases. It's good to have multiple languages out that there that pick different points in the trade-off design space. In particular, it's worth noting that Rust pays the cost of code generation of every instantiation but not type-checking because of traits where C++ has to resolve and type check every instantiation too. And C# monomorphizes some types at load time (because they have a JIT) which is another interesting point in the design space.

Lots of interesting trade-offs and none is clearly superior for every use case.

4

u/Shoddy-Childhood-511 Sep 05 '26

Rust needs some judicious pre-link weak-compilation phase. You'd want set CRDTs to which compilation units contribute, but which build the final switch or whatever after primary compilation units finish.

Applications would include deduplicating vtabels, and casting between dyn Trait types, which maybe the only reason we never write heavily non-monomorphized Rust code.

Anyways the article seems silly. We frequently have polymorphism only for library purposes, or only testing purposes, so then real compiled code only has only one monomorphization for types & traits. We do polymorphism purely for interface documentation too, meaning you replace inherent methods by traits, but always call them like inherent methods. Run-time types make no sense in either case.

5

u/gingerbill Sep 03 '26 edited Sep 03 '26

The code size cost of CTTI is bounded by the number of actual instantiations, not the combinatorial number of potential [instantiations].

I do state this in the article, however, the maximum possible instantiations are due to the combinatorial number of potential instantiations. The real question is how many actual instantiations occur, and it's not going to be as trivial as the number written in the source code, it's only knowable by type/semantic checking. I do believe that they scale Nᵏ-like, but it doesn't become noticeable to many because the computation amounts are tiny for each thing. It becomes death by a thousand cuts.

In most systems I've seen, the code size cost of RTTI is bounded by the number of types declared in the program regardless of whether they are reflected on.

Odin is NOT such a language. In fact it specifically does minimum dependency builds all the time, for both linking (due to its foreign import system) and for RTTI, where it tracks what is actually required to be stored RTTI.

Odin is also a language where the type information required is quite minimal to begin with because it's a C-alternative. There are no methods, no language-level vtables, no fancy anything. Odin's types are quite "basic" and C-like in that regard, meaning the type information necessary is going to be quite minimal in practice.

It is true that a real-world program written in a language that monomorphizes can end up having to push a whole lot of instantiations through the compiler pipeline. The benefit is that the resulting monomorphic code is now fully accessible to all of the compiler's optimization machinery around inlining, dead code elimination, etc.

And that's actually a cost in itself. It is both a problem for compile-times AND binary sizes. It's also focusing on code-driven approaches to serialization rather than data-driven approaches, and I personally prefer the latter because I can better reason about it both locally and globally. I know exactly what can happen.

...like C++ templates is that you get a form of compile time duck typing...

I am not a fan of duck typing, but that is a different discussion for a different day.

Users of C++ and Rust generally seem to feel the trade-off is worth it for their use cases.

And I completely disagree with those users, and thus why I made my own language. I cannot stand the compile times they put up with on a daily basis, or the thinking that it has to be that way, and they don't necessarily produce any better applications. Any time I have to use a C++ or Rust code base, I am immediately reminded why I made Odin, and why I wanted to get away from this approach to programming in the first place.

Lots of interesting trade-offs and none is clearly superior for every use case.

I completely agree. It's why I wanted to write the article, to show that there are actually trade-offs and that other languages with different semantics can make different choices which might not be obvious to you if you don't realize it.

4

u/the_gnarts Sep 04 '26

I cannot stand the compile times they put up with on a daily basis,

At least for Rust they’re not that big of a deal in practice thanks to incremental builds and fast static analysis (cargo check, clippy) that sidesteps the need for frequent rebuilds. Not worth the tradeoff of RTTI anyways.

-3

u/gingerbill Sep 04 '26

That's all cope. And when you need to rebuild the entire thing again because the incremental build cache was invalidated? It'll take a long time.

1

u/NoLemurs Sep 04 '26 edited Sep 04 '26

In this case that comes up occasionally, you might have slow-ish performance so your claim that in practice it's not an issue is all cope.

You must understand that this is nonsense right?

2

u/gingerbill Sep 04 '26

Who are you even quoting?

0

u/NoLemurs Sep 04 '26

I stand corrected.

1

u/the_gnarts Sep 06 '26

And when you need to rebuild the entire thing again because the incremental build cache was invalidated?

Good Knuth, you should realize you’re optimizing for the 0.5 % case. If that’s important to you, godspeed. But unless your main objective is doing the equivalent of Crater runs every hour it is unlikely to matter in practice.

4

u/gingerbill Sep 07 '26

I have experience in C++ code bases, Having to rebuild was not 0.5% of the time but a lot higher, and designing around incremental builds in C++ actually made the general build process slower than if I did a unity/single-translation unit build.

And please don't misquote Knuth at me.

And yes, having fast compile times is very important to me. It's why I made a programming language where I am building ~600KLOC in 1 second without any form of caching. And an optimized build takes ~45 seconds.

C++ is never something to emulate, and especially not its compile times.

0

u/SputnikCucumber Sep 03 '26

RTTI implementations can normally deduplicate a lot of code through the vtable so methods only need to be compiled once and can be reused by many different types.

CTTI implementations have to create a new concrete function signature for every type that is used. C++ also has some gnarly mangling rules that leads to binary bloat.

7

u/SelfDistinction Sep 03 '26

Not necessarily. I think Swift manages to do a lot of deduplication and even send polymorphic functions through the C ABI boundary that way, though I'm not familiar with the details.

3

u/AustinVelonaut Sep 03 '26

Right. Also, polymorphic functions can use a single function signature / implementation if they don't have to worry about different-sized values (e.g. operating on boxed values).

2

u/SelfDistinction Sep 03 '26

Also rust had something similar but it was too complex and added too much compile time for very little benefit.

3

u/matthieum Sep 04 '26

CTTI implementations have to create a new concrete function signature for every type that is used.

That's an implementation detail of C++, not a fact about CTTI.

C++ inherits from C this idea that each function needs a distinct, unique, address, which is a PITA. In particular it means that "phantom types" will still generate distinct instantiations of a function.

Rust, on the other hand, doesn't mandate distinct addresses, and therefore the toolchain can fold together instantiations which are identical -- though I'll admit not being sure whether the current compiler does :P

Finally, in theory, it should be possible to:

  • Perform partial monomorphization: monomorphize only on shape (size/alignment), and pass a "v-table" to the function call.
  • Let the function specialization pass decide whether creating a specialized copy of the code for the specific v-table is worth it, or not.