Let s make some Marc R. Hoffmann Eclipse Summit Europe 2012 24.10.2012
public class WhatIsFaster { int i; void inc1() { i = i + 1; } void inc2() { i += 1; } void inc3() { i++; } }
Why? Compilers Scrip;ng Engines Sta;c Analysis Dynamic Analysis Reverse Engineering Read Write
ca fe ba be 00 00 00 33 00 17 07 00 02 01 00 0c 57 68 61 74 49 73 46 61 73 74 65 72 07 00 04 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74 01 00 01 69 01 00 01 49 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29 56 01 00 04 43 6f 64 65 0a 00 03 00 0b 0c 00 07 00 08 01 00 0f 4c 69 6e 65 4e 75 6d 62 65 72 54 61 62 6c 65 01 00 12 4c 6f 63 61 6c 56 61 72 69 61 62 6c 65 54 61 62 6c 65 01 00 04 74 68 69 73 01 00 0e 4c 57 68 61 74 49 73 46 61 73 74 65 72 3b 01 00 04 69 6e 63 31
Versions 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 45.0 45.3 46.0 47.0 48.0 49.0 50.0 51.0 The JVM Spec First Edi;on The JVM Spec Second Edi;on The JVM Spec Java 7 SE Edi;on
Class File Naming java/lang/string.class java/util/map$entry.class Java/util/Collections$1.class
JVM Type Nota<on Base Types Z C B S I J F D V Object Types Ljava/lang/String; Array Types: [C [[Z [Ljava/lang/String;
ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[]; u2 fields_count; field_info fields[]; u2 methods_count; method_info methods[]; u2 attributes_count; attribute_info attributes[]; }
Class File Anatomy Class Constant Pool Fields ASributes Methods ASributes ASributes
Constant Pool Idx Idx Idx Idx Idx Idx Idx Idx u1 tag; u1[] info u1 tag; u1[] info u1 tag; u1[] info u1 tag; u1[] info u1 tag; u1[] info u1 tag; u1[] info u1 tag; u1[] info u1 tag; u1[] info Constant Pool Fields Methods
Constant Pool FieldRef MethodRef IntfMethRef String Class NameAndType 2 Utf8 Integer Float Long Double Constant Pool Fields Methods MethodHandle, MethodType, InvokeDynamic
Fields field_info { u2 access_flags; u2 name_index; Utf8 u2 descriptor_index; Utf8 u2 attributes_count; attribute_info attributes[]; } Constant Pool Fields Methods
ConstantValue AAribute Contant Pool int, short, char, byte, boolean float long double java.lang.string Integer Float Long Double String Constant Pool Fields Methods
Methods method_info { u2 access_flags; Utf8 u2 name_index; u2 descriptor_index; Utf8 u2 attributes_count; attribute_info attributes[]; } Where is the Code? getvalue run <init> <clinit> ()I ()V (LFoo;)V (II)I; Constant Pool Fields Methods
Code AAribute Code_attribute { u2 attribute_name_index; u4 attribute_length; u2 max_stack; u2 max_locals; u4 code_length; u1 code[]; u2 exception_table_length; { u2 start_pc; u2 end_pc; u2 handler_pc; u2 catch_type; } exception_table[]; u2 attributes_count; attribute_info attributes[]; }
void inc1() { i = i + 1; } 19 00 ALOAD 0 59 DUP B4 nn nn GETFIELD WhatIsFaster.i 04 ICONST_1 60 IADD B5 nn nn PUTFIELD WhatIsFaster.i B1 RETURN
Data Operand Stack Local Variables this Methods Args Local Vars References get/put field invoke new
Operand Stack ALOAD 0 DUP GETFIELD WhatIsFaster.i ICONST_1 IADD PUTFIELD WhatIsFaster.i RETURN
Operand Stack ALOAD 0 DUP GETFIELD WhatIsFaster.i ICONST_1 IADD PUTFIELD WhatIsFaster.i RETURN this
Operand Stack ALOAD 0 DUP GETFIELD WhatIsFaster.i ICONST_1 IADD PUTFIELD WhatIsFaster.i RETURN this this
Operand Stack ALOAD 0 DUP GETFIELD WhatIsFaster.i ICONST_1 IADD PUTFIELD WhatIsFaster.i RETURN i this
Operand Stack ALOAD 0 DUP GETFIELD WhatIsFaster.i ICONST_1 IADD PUTFIELD WhatIsFaster.i RETURN 1 i this
Operand Stack ALOAD 0 DUP GETFIELD WhatIsFaster.i ICONST_1 IADD PUTFIELD WhatIsFaster.i RETURN i+1 this
Operand Stack ALOAD 0 DUP GETFIELD WhatIsFaster.i ICONST_1 IADD PUTFIELD WhatIsFaster.i RETURN
Stack Frame Max Locals Max Stack 1 i this
205 Opcodes I Constants NOP ACONST_NULL ICONST_n LCONST_n FCONST_n DCONST_n BIPUSH SIPUSH LDC LDC_W LDC2_W Loads ILOAD LLOAD FLOAD DLOAD ALOAD IALOAD LALOAD FALOAD DALOAD AALOAD BALOAD Stores ISTORE LSTORE FSTORE DSTORE ASTORE IASTORE LASTORE FASTORE DASTORE AASTORE BASTORE
205 Opcodes II References GETSTATIC PUTSTATIC GETFIELD PUTFIELD INVOKExxx NEWARRAY ANEWARRAY ATHROW CHECKCAST INSTANCEOF MONITORENTER MONITOREXIT Control GOTO TABLESWITCH LOOKUPSWITCH xreturn IFEQ IFNE IFLT IFGE IFGT IFLE IFNULL IFNONNULL Comparison LCMP FCMPL FCMPG DCMPL DCMPG
Byte Code Tools javap Byte Code Outline Plug- in asm.objectweb.org
JVM Data Types top oneword twoword int float reference long double uninitialized Object Java Class Hierachy
Stack Map Frames L0: ILOAD 3 ISTORE 4 ILOAD 1 IFEQ L1 ILOAD 3 ICONST_2 IMUL ISTORE 3 L1: ILOAD 2 IFEQ L2 IINC 3 L2: ILOAD 4 IRETURN Locals Stack
Debug AAributes Class SourceFile Methods LineNumberTable LocalVariableTable LocalVariableTypeTable
What is Faster? void inc1() { } i = i + 1; void inc2() { } i += 1; void inc3() { } i++; ALOAD 0 DUP GETFIELD WhatIsFaster.i ICONST_1 IADD PUTFIELD WhatIsFaster.i RETURN ALOAD 0 DUP GETFIELD WhatIsFaster.i ICONST_1 IADD PUTFIELD WhatIsFaster.i RETURN ALOAD 0 DUP GETFIELD WhatIsFaster.i ICONST_1 IADD PUTFIELD WhatIsFaster.i RETURN
Thank You! Ques<ons? Please Vote! Marc R. Hoffmann Eclipse Summit Europe 2012 24.10.2012