查找方法的字节码大小 [英] Finding the Bytecode Size of a Method

查看:182
本文介绍了查找方法的字节码大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚方法的字节码大小,因为我想确保它足够小,可以通过编译器优化来内联。

I am trying to figure out the bytecode size of a method because I want to be sure that it will be small enough to be inlined by compiler optimizations.

我看到内联方法的默认最大大小为35,所以如果方法大于此值,我将修改代码或将其分解为多种方法。

I saw that the default max size for inlining methods is 35, so if the method is larger than that I will revise the code or break it into multiple methods.

我有一个生成下面字节码的方法(通过IntelliJ IDEA的ASM字节码大纲插件进行反汇编)。

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

如何判断该方法的字节码大小? LINENUMBER似乎引用了原始源代码的行号。

How can I tell the bytecode size of that method? the LINENUMBERs seem to reference the line numbers of the original 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


推荐答案


如何判断该方法的字节码大小?

How can I tell the bytecode size of that method?

一种方法是将它们加起来: - )

One way is to just add them up :-)

每个字节码指令由1个字节组成,用于主指令加上固定数量的操作数字节。

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

更实用的方法是使用 javap -c转储包含字节码的类文件。输出包括每条指令的字节偏移量。

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

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


1)我可以将ALOAD 0 ASTORE 4添加为4个字节,但是如何处理ARRAYLENGTH或INVOKESTATIC方法名?

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

JVM规范的第6.5节中列出了这些说明 - http://docs.oracle.com/javase/specs/jvms/se7/html/index.html

The instructions are listed in Section 6.5 of the JVM spec - http://docs.oracle.com/javase/specs/jvms/se7/html/index.html


  1. 向下滚动到索引的相关部分。

  2. 点击指令的链接。

  3. 读取格式和描述以确定使用了多少字节。

按照这个程序,我推断出ARRAYLENGTH是1个字节,INVOKESTATIC是3个字节。

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


2)我试图使用javap但由于某种原因我找不到类(它在jar中,我将-classpath filename.jar传递给javap但是它不起作用)。

2) I tried to use javap but for some reason I get class not found (it's inside a jar and I passed -classpath filename.jar to javap but it didn't work).

再次阅读 javap 手册条目。如果正确使用它, 可以正常工作。 (也许您没有以正确的格式提供完全限定的类名。)

Read the javap manual entry again. It does work if you use it correctly. (Perhaps you didn't supply the fully-qualified classname in the correct format.)

这篇关于查找方法的字节码大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆