The JVM JIT compiles hot code paths. Understanding dispatch helps you write code
that compiles efficiently.
Dispatch Types
Type
Implementations
JIT Behavior
Relative Cost
Monomorphic
1
Inlined
1x
Bimorphic
2
Inlined with type guard
~1.2x
Megamorphic
3+
Virtual call (no inline)
~2x
Monomorphic (1 type)
Single implementation at a call site: JIT inlines the call.
// JIT inlines this
interface Coder { int work(byte[] data); }
class Coder0 implements Coder { ... }
Coder c = new Coder0();
c.work(data); // Fast: inlined
Bimorphic (2 types)
Two implementations seen: JIT generates a type check + both inlined branches.
// Two types seen at call site
interface Coder { int work(byte[] data); }
class Coder0 implements Coder { ... }
class Coder1 implements Coder { ... }
// JIT generates two inlined paths + type check
Megamorphic (3+ types)
Three or more types: JIT cannot optimize, falls back to virtual call.
// Slow: megamorphic dispatch
interface Coder { int work(byte[] data); }
// Three or more implementations used -> virtual call
Benchmarks (non-inlined)
Monomorphic: ~2.8 ns/op
Bimorphic: ~3.3 ns/op
Megamorphic: ~4.9 ns/op
Benchmarks (inlined)
Monomorphic: ~1.6 ns/op
Bimorphic: ~1.6 ns/op
Megamorphic: ~4.3 ns/op
Key insight: Type guards (for bimorphic) still allow inlining and are fast.
Megamorphic cannot be inlined - avoid 3+ implementations in hot paths.
Actionable Rules
Use static methods when receiver is known at compile time - fastest
dispatch
Limit interface implementations to 2 or fewer for hot paths
Use final classes/methods to enable inlining
Prefer abstract classes over interfaces for performance-critical
polymorphism (better CHA)
Batch similar types to keep call sites monomorphic or bimorphic
Deep hierarchies don't matter - hierarchy depth has no impact on call
cost
final keyword alone doesn't help - unless combined with refactoring that
reduces polymorphism