Finding the method byte code size

I am trying to figure out the size of the method bytecode because I want to be sure that it will be small enough to be built into the compiler optimization.

I saw that the default maximum size for blending methods is 35, so if the method is larger than me, I will revise the code or split it into several methods.

I have a method that generates bytecode below (disassembled via ASM Bytecode Outline plugin for IntelliJ IDEA).

How can I specify the bytecode size of this method? LINENUMBER seems to refer to the line numbers of the source code.

TIA

public static mergeNativeArrays([Ljava/lang/Object;[Ljava/lang/Object;IZ)[Ljava/lang/Object; L0 LINENUMBER 865 L0 ALOAD 0 ASTORE 4 L1 LINENUMBER 867 L1 ILOAD 2 IFGE L2 L3 LINENUMBER 868 L3 ALOAD 0 ARRAYLENGTH ISTORE 2 L2 LINENUMBER 870 L2 FRAME APPEND [[Ljava/lang/Object;] ILOAD 2 ALOAD 1 ARRAYLENGTH IADD ISTORE 5 L4 LINENUMBER 872 L4 ALOAD 4 ARRAYLENGTH ILOAD 5 IF_ICMPGE L5 L6 LINENUMBER 874 L6 ILOAD 3 IFEQ L7 L8 LINENUMBER 875 L8 ILOAD 5 INVOKESTATIC railo/commons/math/MathUtil.nextPowerOf2 (I)I ISTORE 5 L7 LINENUMBER 877 L7 FRAME APPEND [I] ILOAD 5 ANEWARRAY java/lang/Object ASTORE 4 L9 LINENUMBER 878 L9 ALOAD 0 ICONST_0 ALOAD 4 ICONST_0 ALOAD 0 ARRAYLENGTH INVOKESTATIC java/lang/System.arraycopy (Ljava/lang/Object;ILjava/lang/Object;II)V L5 LINENUMBER 881 L5 FRAME SAME ALOAD 1 ICONST_0 ALOAD 4 ILOAD 2 ALOAD 1 ARRAYLENGTH INVOKESTATIC java/lang/System.arraycopy (Ljava/lang/Object;ILjava/lang/Object;II)V L10 LINENUMBER 883 L10 ALOAD 4 ARETURN L11 LOCALVARIABLE dst [Ljava/lang/Object; L0 L11 0 LOCALVARIABLE src [Ljava/lang/Object; L0 L11 1 LOCALVARIABLE dstPosition I L0 L11 2 LOCALVARIABLE doPowerOf2 Z L0 L11 3 LOCALVARIABLE result [Ljava/lang/Object; L1 L11 4 LOCALVARIABLE newSize I L4 L11 5 MAXSTACK = 5 MAXLOCALS = 6 
+6
source share
1 answer

How can I specify the bytecode size of this method?

One way is to simply add them :-)

Each bytecode instruction consists of 1 byte for the primary instruction plus a fixed number of bytes of the operand.


A more practical way is to dump the class file containing bytecodes using javap -c . The output includes byte offsets for each command.

Link: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html


1) I can add ALOAD 0 ASTORE 4 as 4 bytes, but what should I do with the ARRAYLENGTH or INVOKESTATIC method-name?

Instructions are given in section 6.5 of the JVM specification - http://docs.oracle.com/javase/specs/jvms/se7/html/index.html

  • Scroll down to the corresponding part of the index.
  • Click the link for instructions.
  • Read the “format” and “description” to find out how many bytes are used.

Following this procedure, I concluded that ARRAYLENGTH is 1 byte and INVOKESTATIC is 3 bytes.

2) I tried using javap, but for some reason I did not get the class (it is inside the jar and I went through -classpath filename.jar in javap, but it did not work).

Read the manual javap entry javap . It works if you use it correctly. (You may not have provided the full class name in the correct format.)

+5
source

All Articles