How the JVM Optimizes Generic Code - A Deep Dive
summary
Uses a QuickSort benchmark on JDK 25 to show JVM generics can match C++ when the VM produces monomorphic code: a hand‑specialized int sort runs ~10.5μs for 1,000 elements, boxed integers are ~3× slower, bimorphic profiles slow from ~12μs to ~47μs and saturated profiles hit ~70μs. Two practical fixes are demonstrated: hidden‑class cloning of the algorithm via method handles to get clean profiles, and aggressive caller‑side inlining/method‑handle customization; both restore near hand‑specialized speed. Project Valhalla’s specialized generics is the long‑term roadmap.
AI-written summary · about
chapters (11)
- 0:00Introduction
- 1:37Generic performance concepts
- 4:02Benchmarks and quicksort analysis
- 7:27Reflective quicksort and Valhalla
- 10:59Hotspot optimization techniques
- 13:35Monomorphism and performance cliffs
- 18:04Degradation of saturated profiles
- 24:11Strategies for reclaiming static performance
- 30:30Experimental repair techniques
- 35:12Force-inline and future directions
- 40:21Comparison with C++ and concluding thoughts
related talks
01h 01mAn Introduction to JVM Performance
51mJava at Speed: Getting the Most Out of Modern Hardware
11mJVM internals — bytecode, JIT, GC, speculative optimization
35mJava at Speed: Building a Better JVM
20mCache Me If You Can: Speed Up Your JVM with Project Valhalla
48mJava 9 Performance
44mJava at Speed: Building a Better JVM.
45mHow difficult can it be to write efficient code?
