Automatic Object Inlining in a Java Virtual Machine
Total Page:16
File Type:pdf, Size:1020Kb
Christian Wimmer Automatic Object Inlining in a Java Virtual Machine A thesis submitted in partial satisfaction of the requirements for the degree of Doctor of Technical Sciences Institute for System Software Johannes Kepler University Linz Accepted on the recommendation of Univ.‐Prof. Dipl.‐Ing. Dr. Dr.h.c. Hanspeter Mössenböck Johannes Kepler University Linz Univ.‐Prof. Dipl.‐Ing. Dr. Michael Franz University of California, Irvine Linz, February 2008 Johannes Kepler University Linz, Altenberger Straße 69, 4040 Linz, Austria, www.jku.at, DVR 0093696 Sun, Sun Microsystems, Java, HotSpot, JDK, and all Java‐based trademarks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. All other product names mentioned herein are trademarks or registered trademarks of their respective owners. Abstract Object‐oriented Java applications allocate many small objects linked together by field references. Such fields are frequently loaded and thus impose a run‐time overhead. Object inlining reduces these costs by eliminating unnecessary field loads. It embeds referenced objects into their referencing object. The order of objects on the heap is changed in such a way that objects accessed together are placed next to each other. Then their offset is fixed, i.e. the objects are colocated. This allows field loads to be replaced by address arithmetic. Array inlining expands the concepts of object inlining to arrays, which are frequently used for the implementation of dynamic data structures. Therefore, the length of arrays often varies, and fields referencing such arrays have to be changed. An efficient code pattern detects these changes and allows the optimized access of such array fields. These optimizations are integrated into Sun Microsystems’ Java HotSpot™ virtual machine. The analysis is performed automatically at run time, requires no actions on the part of the programmer, and supports dynamic class loading. It is embedded into the just‐in‐time compiler and garbage collector and does not need a global data flow analysis. The evaluation shows that the dynamic approach successfully identifies and optimizes frequently accessed fields for several benchmark suites with a reasonable low compilation and analysis overhead. Kurzfassung Objektorientierte Java‐Anwendungen verwenden viele kleine Objekte, die über Felder miteinander verbunden sind. Solche Felder werden häufig geladen und erhöhen die Ausführungszeit. Objekt‐Inlining reduziert die Kosten, indem unnötige Feldzugriffe entfernt werden. Die Reihenfolge von einander referenzierenden Objekten wird so geändert, dass verbundene Objekte im Speicher hintereinander liegen. Dadurch ist der Abstand zwischen den Objekten konstant und Feldzugriffe können durch Adressberechnungen ersetzt werden. Array‐Inlining erweitert die Optimierung von Objekten auf Arrays. Diese werden oft für dynamische Datenstrukturen verwendet. Dadurch ist ihre Länge nicht bekannt, und Felder, die auf Arrays zeigen, werden häufig geändert. Da diese Änderungen effizient erkannt werden können, ist die Optimierung solcher Felder möglich. Diese Optimierungen sind in die Java HotSpot™ VM von Sun Microsystems integriert. Die Analyse wird automatisch zur Laufzeit ausgeführt, benötigt keine speziellen Zusatzinformationen und unterstützt das dynamische Laden von Klassen. Sie ist in den Just‐in‐Time‐Compiler und den Garbage Collector integriert und verwendet keine globale Datenfluss‐Analyse. Die Evaluierung zeigt, dass dieser dynamische Ansatz mit geringem Laufzeit‐Aufwand wichtige Felder erkennt und optimiert. Table of Contents 1 Introduction..........................................................................................................................1 1.1 Java .............................................................................................................................1 1.2 Feedback‐Directed Optimizations..........................................................................2 1.3 Problem Statement ...................................................................................................3 1.4 State of the Art ..........................................................................................................6 1.5 Project History...........................................................................................................6 1.6 Structure of the Thesis .............................................................................................8 2 The Java HotSpot VM .........................................................................................................9 2.1 System Structure .......................................................................................................9 2.1.1 Client and Server Configuration ..............................................................11 2.2 Object Layout ..........................................................................................................12 2.2.1 Mark Word ..................................................................................................12 2.2.2 Class Hierarchy...........................................................................................13 2.3 Garbage Collection .................................................................................................14 2.3.1 Stop‐and‐Copy Algorithm ........................................................................15 2.3.2 Mark‐and‐Compact Algorithm ................................................................16 2.4 Client Compiler.......................................................................................................17 2.4.1 Bytecodes.....................................................................................................17 2.4.2 Front End.....................................................................................................19 2.4.3 Back End ......................................................................................................20 2.5 Deoptimization .......................................................................................................23 3 Architecture of Object and Array Inlining.....................................................................25 3.1 Source Code of Example........................................................................................25 3.2 Definition of Terms.................................................................................................27 3.2.1 Example .......................................................................................................28 3.2.2 Method Inlining..........................................................................................28 i Table of Contents 3.3 Design Principles ................................................................................................... 29 3.3.1 Memory Layout ......................................................................................... 30 3.3.2 Preconditions for a Field........................................................................... 32 3.4 Components for Object Inlining .......................................................................... 33 3.4.1 Method Tracking ....................................................................................... 35 3.4.2 Hot‐Field Detection ................................................................................... 36 3.4.3 Object Colocation....................................................................................... 36 3.4.4 Co‐allocation of Objects............................................................................ 37 3.4.5 Guards for Field Stores ............................................................................. 38 3.4.6 Transition to Object Inlining .................................................................... 39 3.4.7 Optimized Field Loads ............................................................................. 39 3.4.8 Run‐Time Monitoring ............................................................................... 40 4 Hot‐Field Detection .......................................................................................................... 41 4.1 Read Barriers .......................................................................................................... 42 4.1.1 Machine Code Pattern............................................................................... 43 4.1.2 Processing of Field Counters.................................................................... 43 4.2 Hot‐Field Tables..................................................................................................... 45 4.2.1 Graph Representation ............................................................................... 46 5 Object Colocation.............................................................................................................. 47 5.1 Colocation Tables................................................................................................... 48 5.1.1 Creation of Colocation Tables before Garbage Collection................... 49 5.1.2 Filling the Colocation Tables with Objects............................................. 50 5.2 Stop‐and‐Copy Algorithm.................................................................................... 52 5.3 Mark‐and‐Compact Algorithm............................................................................ 54 6 Object Inlining..................................................................................................................