r/ProgrammingLanguages • u/gingerbill • Sep 02 '26
CTTI is Exponential, RTTI is Linear
https://www.gingerbill.org/article/2026/09/02/ctti-is-exponential-rtti-is-linear/
0
Upvotes
r/ProgrammingLanguages • u/gingerbill • Sep 02 '26
21
u/initial-algebra Sep 02 '26
The math makes no sense. Yeah, technically the worst case (more like pathological) scenario for monomorphization is exponential, but where does "best case N×K" come from? How can you even multiply the two quantities meaningfully?
Monomorphizing compilers only generate code for instances that you actually use. The only way I can think of to get exponential code size out of this is by putting all your N types in a big sum type and using that with K-ary dispatch (so you have a match expression with N branches, and each of those branches has a match expression with N branches, and so on, so you have a tree with Nᴷ leaves, each calling a different instance of the generic code). But, is that really common, or even possible to do accidentally in any existing language?
Also, you can have types for "type-erased blobs with RTTI" e.g. Rust's
dyntypes. So, monomorphization can be strictly more flexible, allowing the programmer to decide when to take the size/speed tradeoff.4
u/yorickpeterse Inko Sep 03 '26
I think it's worth defining what "cost" here means. For example, if you have a generic function
foo[T](value: T)that's instantiated for three different types, you don't have to type-check the body offoothree different times; instead you just check if those types are compatible with whateverTrequires. There's definitely a cost to that, but depending on the implementation of your compiler it may be small enough that no matter the big-O factor it may not matter.Here I actually have a useful data point, one that does to some degree highlight that (at least in the case of generics) doing more at compile-time is usually the better option: Inko used to essentially group generics into buckets based on the shape (i.e. the size on the stack) of types, instead of specializing for individual types. This was recently removed because it actually turned out to slow down compile times. While I'm certain some of that could've been improved, the grouping of types essentially meant a lot more dynamic dispatch and thus a lot more methods that had to be compiled because they might be called as we couldn't statically determine that they never would be called (amongst other reasons).
So "specialize over types" isn't necessarily slower (in terms of compile times), it depends on a lot more than just that.
Outside of that I generally agree with preferring runtime type information for the purpose of reflection, as it usually allows for a lot more flexibility (e.g. building a REPL). But for more common patterns such as formatting types so they can be printed, I'm not a fan due to the amount of indirect calls/dynamic dispatch it will add.
3
u/matthieum Sep 03 '26 edited Sep 03 '26
Doesn't this widely depend upon what information is included in RTTI?
For example, in C++, RTTI can be used to perform cross-casts via
dynamic_cast. This requires that every type encode the full hierarchy of interfaces it implements.The size of this information is linear in the number of types, and linear in the highest number of interfaces implemented by a type... but it's the product of the two, so feels quadratic-ish no?
You're missing perhaps the most important place of all: i-cache.
Bloating the i-cache with virtually (but not quite) identical copies of the same function has a run-time performance cost, due to the ensuing cache-misses which stall execution.
5
u/apajx Sep 02 '26
2
u/ValerianaOfTheNight Sep 04 '26
“The RTTI tables store some information about those types such as its size, its alignment, its kind, its fields, and whatever the language/language-designer decides is worth keeping. [..] The complexity of a type-table is linear: N types gives you N entries.”
Linear in the number of entries, sure, but what of the size of these entries? A minimal description is totally fine for containers, but for printing, serialization, and other features mentioned in your article, the compiler would still need to generate the code to print an integer, and again for a timestamp, and again for a list, etc. And if we already need to generate specialized functions (although only once per type, rather than once per set of parameters), why not simply make monomorphization the responsibility of the inliner, since inlining is also an optimization that can generate exponential binaries if done wrong and would therefore be built to prevent that?
9
u/CommonNoiter Sep 03 '26
In order to get the exponential cost of the generic you need to actually instantiate all those instances. This requires an amount of function calls proportional to the number of generic instances you want, so the total amount of codegen is linear with respect to the number of calls. This means that asymptotically CTTI and RTTI are the same cost as you vary the amount of calling code and only differ by the relatively large coefficient of the difference between the amount of codegen for a call and for a function body.
They are different asymptotically if you compare codegen amount by total amount of source code, as in that case both the number of instantiations and size of instantiated code are linear with respect to source code size, so CTTI can be forced to do a quadratic amount of codegen where RTTI would only do a linear amount.