Where do all the empty methods go?

In my game all AI behaviors implement an interface that defines two methods: plan() and act(). Plan is for expensive calculations and is only executed infrequently, while act is for fine-grained updates like incrementing position. A lot of my behaviors don’t need to plan, or don’t need to act, so many of the methods have empty bodies.

What does Java do to empty methods? I’m guessing there could be some clever JIT optimizations for this.

When it comes to inlining, the result should be obvious. Interface methods are however a tad harder to inline, as in your case, the callsite will probably call multiple interface implementations. This typically leads to a switch-table, jumping to the implementation (empty or not).

In the cases where the implementation is provably monomorphic - that is, there is only ever one implementation that can be called - then it’ll get inlined. Empty methods calls are completely removed.

Apparently Hotspot can also inline “probably monomorphic” code, when nearly all calls to a particular implementation are to the same one out of a choice of many, which inserts a tiny fast check before the inline call to make sure it’s actually doing the right thing. Someone who enjoys looking at machine code could show us this in action (I don’t :D)

Cas :slight_smile:

No, this code isn’t monomorphic so it won’t get optimized.