Can Java 8 default interface methods be inlined by the JIT compiler?
Eugene's example shows that default methods can be inlined.
In fact, I think that the criteria for inlining should be the same as for any other non-static method.
- The size of the code to be inlined must be smaller than a tunable threshold.
- The method must not be overridden by a method in any (currently loaded) subclass of the class or interface.
In your example, I think that inlining should be possible, assuming that this is all of the code involved in the example.
However, there may be other limitations with / in the specific JIT that is being used here. For example, a default method calling another default method might be an edge case that is rare enough that it was deemed not worth supporting. Another possible explanation is that the C1 compiler doesn't do deep monomorphic dispatch analysis / optimization.
And the flip-side of this is that this could be premature optimization ... unless your performance profiling has identified a specific hotspot in your code where inlining could make a significant difference. Normally, the best strategy is to leave this to the compiler. If you micro-optimize your code to give optimal performance for a given Java version, there is a good chance that you will need to redo the work when you change to a newer version.