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

27

u/SputnikCucumber Sep 02 '26

The author is right, RTTI is a constant time overhead. But some applications need (or just want) to squeeze every last CPU cycle and retrieving data from memory can be the slowest part of a hot loop, ruining all of your benchmarks. CTTI gives us most of the ergonomics of runtime type deduction, without the extra constant time overhead.

5

u/bluegardener Sep 02 '26

Can a JIT get you the best of both worlds often? And sometimes with a big asterisk even beat out the CTTI.

6

u/SputnikCucumber Sep 02 '26

Sometimes. It depends on the kind of data access. For the tightest of hot-loops, you will want to manually layout your data in contiguous chunks of memory in the order that you are processing it. This ensures that the data you are processing is being prefetched into the CPU cache ahead of being processed.

A JIT isn't going to be able to make those kinds of guarantees in the general case.

4

u/Nyefan Sep 03 '26

A jit can't make any guarantees, but in a long running program with statistically consistent inputs and outputs, jits can do some pretty spectacular things. The JVM hotspot compiler does in fact test different instruction ordering and memory layouts during operation, using the measured efficiency at runtime to choose a nearly optimal set of implementations. Jits are never going to beat perfectly optimized hand tuned code for a specific system, but they can do a great job of making most code run pretty well and can often do better on specific systems than code compiled for a broad set of systems due to having perfect information about the available instruction sets and the execution characteristics of the system it's running on.

1

u/SputnikCucumber Sep 03 '26

I'm not sure that a JIT could do much about a function that takes an interface as an argument and whose concrete type isn't known until runtime. Maybe it could speculatively devirtualize it, but that would only be effective if the function was only ever called with a single concrete type.

3

u/Nyefan Sep 03 '26

The java jit selectively monomorphizes hot code at runtime at the call sites, no speculation required.

1

u/SputnikCucumber Sep 04 '26

That's pretty cool. But how does it know that the type being passed in at the call site is the same every time? Or does it just optimistically guess?

3

u/Nyefan Sep 04 '26

It knows because it can observe the behavior. As long as a hot call site uses only one or two concrete types, the method is statically inlined. When a third type is used, it falls back to vtable lookup and triggers a deoptimization pass on the surrounding code segment so it can try again. The order and kinds of optimizations that are used in a code segment are partially randomized, so trying again can give different results and helps avoid getting stuck on local maxima.

2

u/bluegardener Sep 03 '26

If you’re manually laying out your data why does it matter if it’s CTTI vs RTTI? The compiler isn’t providing that kind of cache optimization in any case right?

1

u/SputnikCucumber Sep 03 '26

A std::vector<T> in C++ or a vec<T> are generics whose concrete type is determined at compile time. They also guarantee that the T's are laid out contiguously in memory.

That's enough to take advantage of cache optimisations.

-2

u/todo_code Sep 03 '26

imo. zig does this well once again. you get access to comptime and can use it at your leisure, but then most other interfaces they force you down the vtable route.

Maybe I don't like this at the same time because they don't have implementations that can skip the vtable. but these are optimizations ive never had to write.

4

u/SputnikCucumber Sep 03 '26

Is that different to C++? C++ by default uses a vtable for inheritance, but you can manually set up compile time type deduction with complex templating.

-1

u/todo_code Sep 03 '26

I don't think that is different then. I do not know if at some number or size of generated code in C++, they then swap to using vtables even though you made const templates.

C++ has a bunch of rules you have to know, and they can be difficult to know the full ruleset sometimes. whereas zig says use comptime type T for CTTI, use vtables for RTTI. no exceptions.

4

u/mccoyn Sep 03 '26

C++ can’t switch templates to vtables without violating the standard. A template might be a plain-old-data type that can be created without a constructor and therefore the compiler doesn’t have a way to initialize the vtable pointer.

5

u/gingerbill Sep 03 '26

Zig is an example of the naïve printing I describe. It has to produce a new instantiation for each unique ordering of argument types that get passed to, since people typically produce an anonymous struct (i.e. tuple syntax): .print("...", .{...}) style call.

So no, it does a really poor job because of its naïve use of CTTI here.

-8

u/todo_code Sep 03 '26

NO it doesn't do a poor job. they are explicit about what happens with CTTI. They tell you this is how it is done. If you want to avoid it, you use the vtable method. It's up to you to implement it the correct way. This is their entire mantra. They do not want to say oh hey you hit this combinatorial number of functions. now its vtables.

2

u/gingerbill Sep 03 '26

What is the canonical way you print in Zig? The thing I described. So yes, it does an extremely poor job for such a common operation by default.

Vtables are a different solution to a different problem, as RTTI and CTTI are there to solve serialization problems (of which printing is one such example). And you cannot put a vtable on every type, especially trivial basic types like integers and strings.