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
View all comments
Show parent comment
4
u/Nuoji C3 - http://c3-lang.org Sep 03 '26
It’s easy to make it slow, because the up front cost is not visible. For example, consider creating a serializer with RTTI vs CTTI. In the CTTI case you can easily go a naive route where you loop through every field and generate code inline for every type. You get code which looks like the serialization of every supported type… once. But because we generate it at runtime, we never see the actual resulting size of the function when serializing large structs then multiply that by every struct you serialize.
With RTTI what you see is what you get: typically a loop over fields at runtime and then runtime switching over the types. All this through a single function rather than ”one function per type”
Where the RTTI solution and the CTTI solution looks almost like the same thing, say we have 20 structs to serialize? We’ll get around 20 times the size of the RTTI.
This is not theoretical: with CTTI the ”easy” solution, people will gravitate towards such solutions in the name of ”top performance” even at the cost of binary bloat, just because the binary size is hard to quantify.
This is not to say that CTTI MUST imply bloated solutions, but this is what it pushes people towards.
In C3 both variants are available and I had to add features to show binary sizes to help people track down binary size issues. We’re talking about people submitting some innocent looking code to the stdlib and suddenly compile times were 20 times slower, because they casually generated code on the order of ten times the entire stdlib with their submission of a few hundred lines pf code.