3

我反编译了 Java(实际上是 Dalvik)字节码。在方法的开头,我直接访问实例成员的字段(即不通过getter)。

似乎 Java 调用Object.getClass()了访问的实例成员 ( mOther),但没有在任何地方使用结果。这是某种检查吗?为什么需要这个调用?我怀疑这是因为我直接访问了一个字段(在该类中定义),但我没有看到连接。

Java代码和反编译的字节码如下。

(请注意,最后一条指令加载lifeTime为常量0x0001,因为在 中MyOtherClass,我有lifeTime一个public final字段,并且当前是从代码初始化的。)

MyOtherClass other = mOther;
if (mAge >= other.lifeTime) { // lifeTime is initialized to 0x0001
   end();
   return;
}

.line 53
move-object/from16 v0, p0
iget-object v0, v0, Lcom/example/engine/MyClass1;->mOther:Lcom/example/engine/MyOtherClass;
move-object/from16 v16, v0

.line 54
.local v16, other:Lcom/example/engine/MyOtherClass;
move-object/from16 v0, p0

iget v0, v0, Lcom/example/engine/MyClass1;->mAge:I
move/from16 v18, v0

// Why is Object->getClass() called?
invoke-virtual/range {v16 .. v16}, Ljava/lang/Object;->getClass()Ljava/lang/Class;

const/16 v19, 0x0001

更新:

在评论中要求我提供该方法的完整源代码。请注意,这mOther是一个最终字段(出于性能原因)。你在这里:

@Override
public void doStep() {
    MyOtherClass other = mOther;
    if (mAge >= other.lifeTime) {
        end();
        return;
    }
    mAge += TICK_TIME;      

    boolean isSurrounded = false;
    if (mAge > mLastSurroundTime + other.surroundingTime) {
        int distance = (int)other.maxSurroundDistance;          

        for (int bx = bx0; bx <= bx1; ++bx) {
            if (bx < 0 || bx >= mSize) { continue; }
            for (int by = by0; by <= by1; ++by) {
                if (by < 0 || by >= mSize) { continue; }
                ArrayList<WorldObject> candidates = getCandidatesAtPos(bx, by);
                for (int i = 0; i < candidates.size(); ++i) {
                    WorldObject obj = candidates.get(i);
                    if (mSelf!= obj && mSelf.getDistanceFrom(obj) <= other.maxSurroundDistance) {
                        obj.notifyDangerImminent(mSelf);
                        isSurrounded = true;
                    }
                }
            }
        }
        if (isSurrounded) { mLastSurroundTime = mAge; }
    }
}
4

2 回答 2

6

我假设 lifeTime 是在声明时分配的最终字段:

 final int lifeTime = 0x0001;

如果是这样,字节码将通过以下方式进行优化(它与 VM 几乎无关,纯编译器魔法):

  • 不需要真正从内存中获取数据:只需要加载一个常量 1。
  • 但是,如果该字段的所有者恰好是null怎么办?在这种情况下,必须抛出 NullPointerException。为了保证这样的行为,编译器发出对 getClass() 的调用,因为
    • 实际上检查null,构造一个新的NullPointerException实例并抛出它是更多的字节码,
    • 此类调用在 VM 中非常优化,
    • 此方法始终可用,
    • 它不需要任何参数。

一个更简单的例子:

class Test {
    private final int myFinalField = 1;

    int test(Test t) {
        return t.myFinalField;
    }
}

如果我们看一下 test() 方法的字节码(这次是 JVM,但如果你把它翻译成 Dalvik,它本质上是一样的),这里也是对 getClass() 的调用:

 // access flags 0x0
  test(LTest;)I
   L0
    LINENUMBER 5 L0

    // load t
    ALOAD 1

    // if (t == null) throw new NullPointerException(); compressed in only two instructions
    INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class;
    POP

    // the actual value of myFinalField
    ICONST_1

    IRETURN
   L1
    LOCALVARIABLE this LTest; L0 L1 0
    LOCALVARIABLE t LTest; L0 L1 1
    MAXSTACK = 1
    MAXLOCALS = 2
于 2013-11-21T19:55:58.247 回答
1

安德烈的回答给出了这个问题的具体答案。但这里有一些与此类问题相关的注意事项:

  1. 显然,您可以在 Oracle / OpenJDK 工具链生成的字节码中看到类似的情况。这并不奇怪,因为 Davlik 字节码的一些生成路径涉及将 Java 源代码编译为 JVM 字节码,然后将它们转换为 Davlik 字节码。

  2. 如果您因为查看字节码以深入了解某些代码的性能而遇到这个奇怪的工件,那么您可能找错地方了。在现代 JVM / Davlik / ART 引擎中,字节码被翻译成本机代码,而本机代码是大部分时间或所有时间1执行的内容。

    为了更可靠地了解“微观”级别的代码性能,您需要检查 AOT 或 JIT 编译器生成的本机代码。

  3. 字节码编译器发出的字节码通常没有经过高度优化的原因之一是,这样做可能会使 AOT / JIT 更难以有效优化。

  4. Davlik 已被 ART 取代。


1 - With Hotspot JVMs, only JIT and direct bytecode interpretation are supported. Early versions of Davlik were interpret-only, and then JIT support was added, and improved. In ART, all three modes are supported in some form: interpret, JIT and AOT.

于 2017-10-29T00:26:14.117 回答