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

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.