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

10

u/PersonalDatabase31 Sep 02 '26

I don't think exponential code generation is a fundamental part of CTTI. One could implement the rust compiler in a way where functions above some number of monomorphized copies would be silently converted to take dyn T instead of T and call sites would take a reference to the generic input along with a function pointer known at compile time. That could prevent iteration over a table in runtime. This would prevent inline optimizations though but I don't think there is a way to combine it along with non exponential compile times.

4

u/gingerbill Sep 02 '26

If everything becomes dyn T, you've just done RTTI, not CTTI.

9

u/PersonalDatabase31 Sep 02 '26

Not everything becomes dyn T though. I assume that most generic functions would still be under the limit and binary explosion would be caused by a minority of the generic functions.

2

u/gingerbill Sep 02 '26

Of course, it is a balance between different concerns, but I have seen these problems explode in other languages. Rust specifically tried to mitigate all of these serialization concerns (of which printing is one of them) directly to prevent it from happening. They did a good job with that in mind.

1

u/Absolute_Enema Sep 03 '26

So that you have to assume that any callsite will use the worse performing alternative without you being able to reliably measure the impact? Sounds like a nightmare.

2

u/PersonalDatabase31 Sep 03 '26

Or it could be behind a compiler flag.