Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Performance Engineer's Guide To HotSpot Just-in-Time Compilation

1,117 views

Published on

Adaptive compilation and runtime in the OpenJDK Hotspot VM offers significant performance enhancements for our tools and applications in Java and other JVM languages. Understanding how it works provides developers with critical information on the Java HotSpot JIT compilation and runtime techniques such as vectorization, compressed OOPs etc., to assist in understanding performance for both client and server applications. We will focus on the internals of OpenJDK 8, the reference implementation for Java SE 8.

Published in: Technology
  • Be the first to comment

The Performance Engineer's Guide To HotSpot Just-in-Time Compilation

  1. 1. 1 THE PERFORMANCE ENGINEER’S GUIDE TO HOTSPOT JIT COMPILATION Monica Beckwith CODE KARAM LLC
  2. 2. 2 About Me … Java/JVM/GC performance person Current Life: Consultant who solves Managed Runtime performance issues Past Life: Worked with Oracle, Sun, AMD… - Worked with HotSpot VM: JVM heuristics, JIT compiler, GCs: Parallel(Old) GC, G1 GC, CMS GC www.codekaram.com https://www.linkedin.com/in/monicabeckwith Tweet @mon_beck
  3. 3. ©2016 CodeKaram Establishing the Characters and The Plot • HotSpot Virtual Machine • Runtime Goal • Common Compilation Techniques • C1, C2 & Tiered Compilations • Adaptive Optimization - The Plot 3
  4. 4. ©2016 CodeKaram The Storyline • Dynamic Deoptimization • Inlining • Intrinsics 4
  5. 5. ©2016 CodeKaram The StoryLine • Compressed Oops + Compressed Class Pointers • Escape Analysis • Vectorization (Auto and otherwise) 5
  6. 6. 6 HotSpot Virtual Machine
  7. 7. ©2016 CodeKaram Let’s Peek Under The Hood 7 Java Application Java APIs Execution Engine Java VM OS + Hardware Runtime
  8. 8. ©2016 CodeKaram8 Java Application Java API Java VM OS + HardwareJRE Execution Engine Runtime Java Runtime Environment
  9. 9. ©2016 CodeKaram The Helpers 9 Java VM Execution Engine Runtime
  10. 10. ©2016 CodeKaram HotSpot VM 10 Execution Engine Heap Management - Garbage Collection JIT Compilation - Tiered (C1 + C2)
  11. 11. ©2016 CodeKaram HotSpot VM 11 Runtime VM Class loading Interpreter Bytecode Verification, Exception Handling, Synchronization , Thread Management, ….http://openjdk.java.net/groups/hotspot/ docs/RuntimeOverview.html
  12. 12. ©2016 CodeKaram Runtime Goal 12 Bytecode Native code
  13. 13. ©2016 CodeKaram HotSpot VM - Template Interpreter 13 Bytecode 1 Bytecode 2 Template Native Code 1 Template Native Code 2 TemplateTable
  14. 14. ©2016 CodeKaram Common Compilation Techniques 14 Compilation Adaptive Optimization Ahead Of Time Profile- guided Static
  15. 15. ©2016 CodeKaram HotSpot VM 15 Compilation Adaptive Optimization Ahead Of Time Profile- guided Static
  16. 16. ©2016 CodeKaram • CompileThreshold • Identify root of compilation • Method Compilation or On-stack replacement (Loop)? 16 Identifying Performance Critical Methods
  17. 17. 17 JIT Optimization - Client Compiler (C1)
  18. 18. ©2016 CodeKaram • Start in interpreter • CompileThreshold = 1500 • Compile with client compiler • Few optimizations 18 Client Compiler
  19. 19. 19 JIT Optimization - Server Compiler (C2)
  20. 20. ©2016 CodeKaram • Start in interpreter • CompileThreshold = 10000 • Compile with optimized server compiler • High performance optimizations 20 Server Compiler
  21. 21. 21 JIT Optimization - Tiered Compilation
  22. 22. 22
  23. 23. ©2016 CodeKaram • Start in interpreter • Tiered optimization with client compiler • Code profiled information • Enable server compiler 23 Tiered Compilation
  24. 24. ©2016 CodeKaram24 Tiered Compilation - Effect on Code Cache Tiered Compilation Code Cache Compiled Native Code
  25. 25. ©2016 CodeKaram • C1 compilation threshold for tiered is about a 100 invocations • Tiered Compilation has a lot more profiled information for C1 compiled methods • CodeCache needs to be 5x larger than non-tiered • Default on JDK8 when tiered is enabled (240MB vs 48MB) • Need more? Use -XX:ReservedCodeCacheSize 25 Tiered Compilation - Effect on Code Cache
  26. 26. 26 Advanced Optimizations - Adaptive Optimization
  27. 27. ©2016 CodeKaram27 The Plot Startup (Adaptive) JIT Optimizations Profiling of critical hot- spots Interpreter
  28. 28. ©2016 CodeKaram28 The Plot Startup (Adaptive) JIT Optimizations Profiling of critical hot- spots Interpreter
  29. 29. 29 Adaptive Optimization: Dynamic Deoptimization
  30. 30. 30 Dynamic Deoptimization
  31. 31. ©2016 CodeKaram • Oopsie! • dependencies invalidation • classes unloading and redefinition • uncommon path in compiled code • misguided profiled information 31 Dynamic Deoptimization
  32. 32. 32 Understanding Adaptive Optimization - PrintCompilation
  33. 33. ©2016 CodeKaram PrintCompilation 33 timestamp compilation-id flags tiered- compilation-level Method <@ osr_bci> code- size <deoptimization>
  34. 34. ©2016 CodeKaram PrintCompilation 34 Flags: %: is_osr_method s: is_synchronized !: has_exception_handler b: is_blocking n: is_native
  35. 35. ©2016 CodeKaram PrintCompilation 35 567 693 % ! 3 org.h2.command.dml.Insert::insertRows @ 76 (513 bytes) 656 797 n 0 java.lang.Object::clone (native) 779 835 s 4 java.lang.StringBuffer::append (13 bytes)
  36. 36. ©2016 CodeKaram36 573 704 2 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie Dynamic Deoptimization
  37. 37. ©2016 CodeKaram37 573 704 2 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie Dynamic Deoptimization
  38. 38. ©2016 CodeKaram38 573 704 2 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie Dynamic Deoptimization
  39. 39. ©2016 CodeKaram39 573 704 2 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie Dynamic Deoptimization
  40. 40. ©2016 CodeKaram40 573 704 2 org.h2.table.Table::fireAfterRow (17 bytes) 7963 2223 4 org.h2.table.Table::fireAfterRow (17 bytes) 7964 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made not entrant 33547 704 2 org.h2.table.Table::fireAfterRow (17 bytes) made zombie Dynamic Deoptimization
  41. 41. 41 JIT Optimization - Inlining
  42. 42. ©2016 CodeKaram HotSpot has studied Inlining to it’s max potential! 42 Inlining
  43. 43. ©2016 CodeKaram MinInliningThreshold, MaxFreqInlineSize, InlineSmallCode, MaxInlineSize, MaxInlineLevel, DesiredMethodLimit … 43 Inlining
  44. 44. 44 Understanding Inlining Optimization - PrintInlining
  45. 45. ©2016 CodeKaram PrintInling* 45 @ 76 java.util.zip.Inflater::setInput (74 bytes) too big @ 80 java.io.BufferedInputStream::getBufIfOpen (21 bytes) inline (hot) @ 91 java.lang.System::arraycopy (0 bytes) (intrinsic) @ 2 java.lang.ClassLoader::checkName (43 bytes) callee is too large * needs -XX:+UnlockDiagnosticVMOptions
  46. 46. ©2016 CodeKaram PrintInling* 46 @ 76 java.util.zip.Inflater::setInput (74 bytes) too big @ 80 java.io.BufferedInputStream::getBufIfOpen (21 bytes) inline (hot) @ 91 java.lang.System::arraycopy (0 bytes) (intrinsic) @ 2 java.lang.ClassLoader::checkName (43 bytes) callee is too large * needs -XX:+UnlockDiagnosticVMOptions
  47. 47. 47 JIT Optimization - Intrinsics
  48. 48. ©2016 CodeKaram48 Without Intrinsics Java Method JIT Compilation Execute Generated Code
  49. 49. ©2016 CodeKaram49 Intrinsics Java Method JIT Compilation Execute Optimized Code Call Hand- Optimized Assembly Code
  50. 50. ©2016 CodeKaram PrintInling* 50 @ 76 java.util.zip.Inflater::setInput (74 bytes) too big @ 80 java.io.BufferedInputStream::getBufIfOpen (21 bytes) inline (hot) @ 91 java.lang.System::arraycopy (0 bytes) (intrinsic) @ 2 java.lang.ClassLoader::checkName (43 bytes) callee is too large * needs -XX:+UnlockDiagnosticVMOptions
  51. 51. 51 Advanced Optimizations - Compressed Oops + Compressed Class Pointers
  52. 52. 52 A Java Object
  53. 53. ©2016 CodeKaram53 Header Body
  54. 54. ©2016 CodeKaram Objects, Fields & Alignment • Objects are 8 byte aligned (default). • Fields: • are aligned by their type. • can fill a gap that maybe required for alignment. • are accessed using offset from the start of the object 54
  55. 55. 55 Java Object Layout in Memory
  56. 56. ©2016 CodeKaram56 -Tool to analyze Java object layout :) Java Object Layout (JOL)
  57. 57. ©2016 CodeKaram JOL Command Line Options $ java -jar jol-cli.jar Usage: jol-cli.jar <mode> [optional arguments]* Available modes: estimates: Simulate the class layout in different VM modes. externals: Show the object externals: the objects reachable from a given instance. footprint: Estimate the footprint of all objects reachable from a given instance heapdump: Consume the heap dump and estimate the savings in different layout strategies. heapdumpstats: Consume the heap dump and print the most frequent instances. idealpack: Compute the object footprint under different field layout strategies. internals: Show the object internals: field layout and default contents, object header shapes: Dump the object shapes present in JAR files or heap dumps. string-compress: Consume the heap dumps and figures out the savings attainable with compressed strings. 57
  58. 58. ©2016 CodeKaram JOL Command Line Options $ java -jar jol-cli.jar Usage: jol-cli.jar <mode> [optional arguments]* Available modes: estimates: Simulate the class layout in different VM modes. externals: Show the object externals: the objects reachable from a given instance. footprint: Estimate the footprint of all objects reachable from a given instance heapdump: Consume the heap dump and estimate the savings in different layout strategies. heapdumpstats: Consume the heap dump and print the most frequent instances. idealpack: Compute the object footprint under different field layout strategies. internals: Show the object internals: field layout and default contents, object header shapes: Dump the object shapes present in JAR files or heap dumps. string-compress: Consume the heap dumps and figures out the savings attainable with compressed strings. 58
  59. 59. ©2016 CodeKaram Compressed OOPs and Compressed Class Pointers 59 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift.
  60. 60. ©2016 CodeKaram Compressed OOPs and Compressed Class Pointers 60 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift.
  61. 61. 61 A Java Object Header
  62. 62. ©2016 CodeKaram A Java Object Header 62 Header Klass Mark Word Array Length
  63. 63. 63 Java Object Layout Samples - Uncompressed
  64. 64. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 64
  65. 65. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 65 Mark Word
  66. 66. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 66 Klass
  67. 67. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 67
  68. 68. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 68 Array Length
  69. 69. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 69
  70. 70. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) a8 07 c0 34 (10101000 00000111 11000000 00110100) (885000104) 12 4 (object header) 02 00 00 00 (00000010 00000000 00000000 00000000) (2) 16 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 20 4 (alignment/padding gap) N/A 24 8 byte [B.<elements> N/A Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 70
  71. 71. 71 Java Object Layout Samples - Compressed
  72. 72. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 72
  73. 73. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 73 Mark Word
  74. 74. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 74
  75. 75. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 75 Klass
  76. 76. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 76
  77. 77. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 77 Array Length
  78. 78. ©2016 CodeKaram JOL: 8 Byte Array [B object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1) 4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0) 8 4 (object header) f5 00 00 f8 (11110101 00000000 00000000 11111000) (-134217483) 12 4 (object header) 08 00 00 00 (00001000 00000000 00000000 00000000) (8) 16 8 byte [B.<elements> N/A Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total 78
  79. 79. 79 Java Object Layout Header Comparison
  80. 80. ©2016 CodeKaram Compressed vs Uncompressed Compressed: Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total vs Uncompressed: Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 80
  81. 81. ©2016 CodeKaram Compressed vs Uncompressed Compressed: Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total vs Uncompressed: Instance size: 32 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total 81
  82. 82. 82 A Java Object
  83. 83. ©2016 CodeKaram83 Header Body
  84. 84. ©2016 CodeKaram Compressed OOPs and Compressed Class Pointers 84 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift.
  85. 85. 85 Java Object Layout Samples - UnCompressed
  86. 86. ©2016 CodeKaram JOL: Class java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) N/A 16 8 Constructor Class.cachedConstructor N/A 24 8 Class Class.newInstanceCallerCache N/A 32 8 String Class.name N/A 40 8 Module Class.module N/A 48 8 (alignment/padding gap) N/A 56 8 String Class.packageName N/A 64 8 Class Class.componentType N/A 72 8 SoftReference Class.reflectionData N/A 80 8 ClassRepository Class.genericInfo N/A 88 8 Object[] Class.enumConstants N/A 96 8 Map Class.enumConstantDirectory N/A 104 8 AnnotationData Class.annotationData N/A 112 8 AnnotationType Class.annotationType N/A 120 8 ClassValueMap Class.classValueMap N/A 128 32 (alignment/padding gap) N/A 160 4 int Class.classRedefinedCount N/A 164 4 (loss due to the next object alignment) Instance size: 168 bytes Space losses: 40 bytes internal + 4 bytes external = 44 bytes total 86
  87. 87. ©2016 CodeKaram java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) N/A 16 8 Constructor Class.cachedConstructor N/A 24 8 Class Class.newInstanceCallerCache N/A 32 8 String Class.name N/A 40 8 Module Class.module N/A 48 8 (alignment/padding gap) N/A 56 8 String Class.packageName N/A 64 8 Class Class.componentType N/A 72 8 SoftReference Class.reflectionData N/A 80 8 ClassRepository Class.genericInfo N/A 88 8 Object[] Class.enumConstants N/A 96 8 Map Class.enumConstantDirectory N/A 104 8 AnnotationData Class.annotationData N/A 112 8 AnnotationType Class.annotationType N/A 120 8 ClassValueMap Class.classValueMap N/A 128 32 (alignment/padding gap) N/A 160 4 int Class.classRedefinedCount N/A 164 4 (loss due to the next object alignment) Instance size: 168 bytes Space losses: 40 bytes internal + 4 bytes external = 44 bytes total 87 JOL: Class
  88. 88. ©2016 CodeKaram java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) N/A 16 8 Constructor Class.cachedConstructor N/A 24 8 Class Class.newInstanceCallerCache N/A 32 8 String Class.name N/A 40 8 Module Class.module N/A 48 8 (alignment/padding gap) N/A 56 8 String Class.packageName N/A 64 8 Class Class.componentType N/A 72 8 SoftReference Class.reflectionData N/A 80 8 ClassRepository Class.genericInfo N/A 88 8 Object[] Class.enumConstants N/A 96 8 Map Class.enumConstantDirectory N/A 104 8 AnnotationData Class.annotationData N/A 112 8 AnnotationType Class.annotationType N/A 120 8 ClassValueMap Class.classValueMap N/A 128 32 (alignment/padding gap) N/A 160 4 int Class.classRedefinedCount N/A 164 4 (loss due to the next object alignment) Instance size: 168 bytes Space losses: 40 bytes internal + 4 bytes external = 44 bytes total 88 JOL: Class
  89. 89. 89 Java Object Layout Samples - Compressed
  90. 90. ©2016 CodeKaram java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 Constructor Class.cachedConstructor N/A 16 4 Class Class.newInstanceCallerCache N/A 20 4 String Class.name N/A 24 4 Module Class.module N/A 28 4 (alignment/padding gap) N/A 32 4 String Class.packageName N/A 36 4 Class Class.componentType N/A 40 4 SoftReference Class.reflectionData N/A 44 4 ClassRepository Class.genericInfo N/A 48 4 Object[] Class.enumConstants N/A 52 4 Map Class.enumConstantDirectory N/A 56 4 AnnotationData Class.annotationData N/A 60 4 AnnotationType Class.annotationType N/A 64 4 ClassValueMap Class.classValueMap N/A 68 28 (alignment/padding gap) N/A 96 4 int Class.classRedefinedCount N/A 100 4 (loss due to the next object alignment) 90 JOL: Class
  91. 91. ©2016 CodeKaram java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 Constructor Class.cachedConstructor N/A 16 4 Class Class.newInstanceCallerCache N/A 20 4 String Class.name N/A 24 4 Module Class.module N/A 28 4 (alignment/padding gap) N/A 32 4 String Class.packageName N/A 36 4 Class Class.componentType N/A 40 4 SoftReference Class.reflectionData N/A 44 4 ClassRepository Class.genericInfo N/A 48 4 Object[] Class.enumConstants N/A 52 4 Map Class.enumConstantDirectory N/A 56 4 AnnotationData Class.annotationData N/A 60 4 AnnotationType Class.annotationType N/A 64 4 ClassValueMap Class.classValueMap N/A 68 28 (alignment/padding gap) N/A 96 4 int Class.classRedefinedCount N/A 100 4 (loss due to the next object alignment) 91 JOL: Class
  92. 92. ©2016 CodeKaram java.lang.Class object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 Constructor Class.cachedConstructor N/A 16 4 Class Class.newInstanceCallerCache N/A 20 4 String Class.name N/A 24 4 Module Class.module N/A 28 4 (alignment/padding gap) N/A 32 4 String Class.packageName N/A 36 4 Class Class.componentType N/A 40 4 SoftReference Class.reflectionData N/A 44 4 ClassRepository Class.genericInfo N/A 48 4 Object[] Class.enumConstants N/A 52 4 Map Class.enumConstantDirectory N/A 56 4 AnnotationData Class.annotationData N/A 60 4 AnnotationType Class.annotationType N/A 64 4 ClassValueMap Class.classValueMap N/A 68 28 (alignment/padding gap) N/A 96 4 int Class.classRedefinedCount N/A 100 4 (loss due to the next object alignment) 92 JOL: Class
  93. 93. ©2016 CodeKaram Compressed OOPs and Compressed Class Pointers 93 # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift. P P
  94. 94. 94 … but what is the 3-bit shift?
  95. 95. ©2016 CodeKaram Compressed OOPs 95 <wide-oop> = <narrow-oop-base> + (<narrow-oop> << 3) + <field-offset>
  96. 96. ©2016 CodeKaram Compressed OOPs 96 Heap Size? <4 GB (no encoding/ decoding needed) >4GB; <28GB (zero-based) <wide-oop> = <narrow-oop> <narrow-oop> << 3
  97. 97. ©2016 CodeKaram Compressed OOPs 97 Heap Size? >28 GB; <32 GB (regular) >32 GB; <64 GB * (change alignment) <wide-oop> = <narrow-oop-base> + (<narrow-oop> << 3) + <field- offset> <narrow-oop-base> + (<narrow-oop> << 4) + <field- offset>
  98. 98. ©2016 CodeKaram Compressed OOPs 98 Heap Size? <4 GB >4GB; <28GB <32GB <64GB Object Alignment? 8 bytes 8 bytes 8 bytes 16 bytes Offset Required? No No Yes Yes Shift by? No shift 3 3 4
  99. 99. 99 Other Advanced Optimizations
  100. 100. ©2016 CodeKaram • Fast dynamic type tests for type safety • Range check elimination • Loop unrolling • Escape Analysis • Vectorization! 100 Other Optimizations
  101. 101. ©2016 CodeKaram • Fast dynamic type tests for type safety • Range check elimination • Loop unrolling • Escape Analysis • Vectorization! 101 Other Optimizations
  102. 102. 102 Advanced Optimization - Escape Analysis
  103. 103. ©2016 CodeKaram • Entire IR graph • escaping allocations? • not stored to a static field or non-static field of an external object, • not returned from method, • not passed as parameter to another method where it escapes. 103 Escape Analysis
  104. 104. ©2016 CodeKaram104 Escape Analysis allocated object doesn’t escape the compiled method allocated object not passed as a parameter + remove allocation and keep field values in registers =
  105. 105. ©2016 CodeKaram105 Escape Analysis allocated object is passed as a parameter + remove locks associated with object and use optimized compare instructions = allocated object doesn’t escape the compiled method
  106. 106. ©2016 CodeKaram • Fast dynamic type tests for type safety • Range check elimination • Loop unrolling • Escape Analysis • Vectorization! 106 Other JIT Compiler Optimizations
  107. 107. 107 Advanced Optimization - Vectorization
  108. 108. 108 Vectorization in HotSpot VM??
  109. 109. ©2016 CodeKaram Vectorization 109 • Utilize SIMD (Single Instruction Multiple Data) instructions offered by the processor. • Generate assembly stubs • Get benefits of operating on cache line size data chunks
  110. 110. 110 Advanced Optimization - Auto-Vectorization
  111. 111. 111 Auto-Vectorization in HotSpot VM??
  112. 112. 112 Look Ma, no stubs!!
  113. 113. 113
  114. 114. ©2016 CodeKaram SuperWord Level Parallelism 114 http://groups.csail.mit.edu/cag/slp/SLP-PLDI-2000.pdf
  115. 115. ©2016 CodeKaram SuperWord Level Parallelism 115 • Loop Unrolling • Alignment Analysis • Pre-Optimization • Identifying Adjacent Memory References • Extending the “PackSet” • Combination and SIMD operation http://groups.csail.mit.edu/cag/slp/SLP-PLDI-2000.pdf
  116. 116. ©2016 CodeKaram Q & A 116
  117. 117. 117 Appendix
  118. 118. ©2016 CodeKaram Compressed Class Pointers 118 • JDK 8 —> Perm Gen Removal —> Class Data outside of heap • Compressed class pointer space • contains class metadata • is a part of Metaspace
  119. 119. ©2016 CodeKaram Compressed Class Pointers 119 PermGen Removal Overview by Coleen Phillimore + Jon Masamitsu @JavaOne 2013
  120. 120. ©2016 CodeKaram120 Mark Word - 32 bit vs 64 bit Klass - 32 bit vs 64 bit Array Length - 32 bit on both boolean, byte, char, float, int, short - 32 bit on both double, long - 64 bit on both ILP32 vs. LP64 Field Sizes

×