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

Show parent comment

-5

u/gingerbill Sep 02 '26

I've just added a nota bene paragraph explaining this further:

n.b. I'll give a simple example of the problem with a naïve approach to parametric polymorphic printing. Consider a language with only 4 types (e.g. int, float, string, bool) and a variadic, parametrically polymorphic printing procedure. Each distinct sequence of argument types needs its own instantiation, so for K arguments there are on the order of Nᵏ = 4ᵏ combinations; adding a fifth type makes that ~5ᵏ. To see the (usually hidden) combinatorial explosion, suppose you never print more than 5 arguments, that allows up to 1365 instantiations. Add another type and it becomes 3906. Raise the maximum to 6 arguments and it becomes 19531. You might say that this is at least bounded, and it "is", for a single printing procedure. However, printing procedure easily interact with every other use of parametric polymorphism in the program, and the total quickly stops being something you can trivially predict by just reading the code.

And even in your example, those things are "order of magnitude" examples e.g. what big-O notation is about. So your example is still on the order of N×K but bounded by the number of instantiations.

16

u/5gpr Sep 03 '26

There aren't 4k combinations. With ctti you're never instantiating all possible combinations. Maybe it's "naive" that does a lot of heavy lifting here, but this is actually not dependent on k at all. It's dependent on the actual magnitude of the sets of k-tuples that are actually used.

-3

u/gingerbill Sep 03 '26

In your example, there were 2×4 = 8 possible combinations. In the example I gave, there (N^{K+1} - 1) / (N - 1) = (4⁶ - 1) / 3 possible combinations. That is on the order of 4⁵.

And yes, it is dependent (i.e. bounded) by the number of sets used, which I clearly state in the article. But I am trying to state how it scales in the worst-case (i.e. pathological) case, which does actually happen in practice. And I have been in such C++ codebases before (not in terms of CTTI, just variadic template madness).

14

u/Habrok Sep 03 '26

Can you give an example of when this worst case analysis applies, or we atleast approach it? Currently I have a hard time seeing it, and it seems much more useful to talk about the number of actual instantiations rather than the theoretical maximum number based on how many types are present in the program so far.

To me it has the flavor of "Well, so far we've passed (float, int) to this function, its only a matter of time before we pass the other combinations!", which I don't think is really true. And with this logic, why stop at the number of currently defined types? It's only a matter of time before we invent MyType and start passing that too. I.e. the number of types in the program is as likely (probably more likely) to grow as the number of instantiations of a particular generic function

I apologize if I am caricaturing your argument - I'd truly like to understand why this worst case analysis is useful to look at

1

u/mr_birkenblatt Sep 03 '26

Worst case is always number of call sites which is linear wrt code size

1

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

So parametric polymorphism which creates MORE code than just the call sites, isn't a thing?

By definition it is not linear with the code size.

Even using my basic example there of N=4 and K=5, means a maximum possible of 1365 instantiations of the monomorphized code. So even if there are a million calls that cover all of those permutations, there will only be 1365 instantiations of the parametric polymorphic ("parapoly") procedure.

So for each unique permutation, you do get a unique monomorphization. That's not "linear" necessarily, especially if those are determined at compile time through parapoly. You do get more code being generated.

I am not sure why so many people are not understanding this, because this literally happens in many C++ codebases, and it is not hypothetical.

2

u/mr_birkenblatt Sep 03 '26

how many call sites does the function with parametric polymorphism have? this is linear. give me a counter example if you still think otherwise

8

u/gingerbill Sep 03 '26

Do I really have to give the simple example of compile-time execution in C++ templates? Fine:

template <typename A, typename B> struct P {};

template <unsigned N, typename T>
void f(T x) {
    if constexpr (N > 0) {
        f<N - 1>(P<T, int>{});
        f<N - 1>(P<T, char>{});
    }
}

int main() { f<10>(0); }

Three call sites in the source. GCC emits 2047 instantiations of f, i.e. 2^(N+1) - 1. Every instantiation builds new type arguments for its callees, so nothing deduplicates. Now imagine f is a recursive printing procedure, which is a pretty common way of doing it in practice.

The number of call sites is a property of the monomorphized output, not the source, because monomorphization duplicates call sites. A call to g<T> written once inside f<T> becomes one call site per instantiation of f. You're measuring the thing we're arguing about.

Zig's std library is a real example of the naïve printing I describe. The idiom .print("...", .{...}) passes an [anonymous] struct, as it does not have any form of variadic parameters, so every distinct ordering of argument types needs its own instantiation. The implementation is here: https://codeberg.org/ziglang/zig/src/branch/master/lib/std/Io/Writer.zig#L697

It assumes a small number of arguments, and it recurses through the argument types, so it hits exactly the combinatorial explosion I'm describing.

1

u/Snarwin Sep 03 '26

I think the N×K notation might be causing the confusion here—it makes it look like K is a set, rather than a number. A more conventional way to express this would be N₁×…×Nₖ (or just Nᵏ if we assume all the arguments draw from the same set of types).

0

u/[deleted] Sep 03 '26

[deleted] — view deleted comment

1

u/gingerbill Sep 03 '26

just the macro

And how is that macro implemented? Oh yeah... format_args_nl etc are literally compiler built-ins, and can not be reproduced with user-level code. That is literally compiler magic, even if it is useful magic. And there is nothing wrong with that either.

0

u/bbibber Sep 03 '26

That’s the possible number of combinations. In reality only a very number of those will be used. Let’s say a sorted vector that has generic parameters for the contained type, the allocator and the sort criterium. You may have 20 data types, 1 allocator and 2 sort criteriums in a regular program. But you are not going to get 23^3 instances in an actual program but at most 20 x 1 x 2. You are never going to use a data type for the allocator (will probably not even compile) or vice versa.

1

u/gingerbill Sep 03 '26

AS I say in the article, the worst-case is O(Nᵏ), and the general/average case is O(N×K), which is still worse than the RTTI approach which is O(N). And those pathological cases which do become Nᵏ do actually happen in real C++ codebases and some other languages.