限定方法调用是否会降低 Java 的性能?

发布于 2024-12-12 06:23:16 字数 507 浏览 0 评论 0原文

我猜测以下两个函数会编译为完全相同的字节码,但我想问这个问题。在不必要的地方限定方法调用是否会降低性能?

例如:

package com.my;

import android.app.Activity;
import android.os.Bundle;

public class Main extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.setContentView(R.layout.main); // Fully qualified 
        setContentView(R.layout.main);      // Not fully qualified
    }

}

标准是使用后者,但是完全限定的调用 this.setContentView() 有任何负面影响吗?

I'm guessing the following two functions compile to the exact same byte-code, but I beg to ask the question. Does qualifying a method call where it is not necessary degrade performance?

For example:

package com.my;

import android.app.Activity;
import android.os.Bundle;

public class Main extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.setContentView(R.layout.main); // Fully qualified 
        setContentView(R.layout.main);      // Not fully qualified
    }

}

The standard is to use the latter, but does the fully qualified call this.setContentView() have any negative effects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ˉ厌 2024-12-19 06:23:16

不,没有任何惩罚。这两个调用都会在字节码中使用 invokespecial 来调用成员方法。

在标准 Java 中,以下代码:

public class TestQualified {

   private void someMethod() {
   }

   public void otherMethod() {
       this.someMethod();
       someMethod();
   }
}

产生以下字节码:

Compiled from "TestQualified.java"
public class TestQualified extends java.lang.Object{
public TestQualified();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

private void someMethod();
  Code:
   0:   return

public void otherMethod();
  Code:
   0:   aload_0
   1:   invokespecial   #2; //Method someMethod:()V
   4:   aload_0
   5:   invokespecial   #2; //Method someMethod:()V
   8:   return

}

如您所见,两个调用是相同的。

现在,Android 使用 dx 工具将 .class 文件转换为 dalvik 字节码,该工具输出 .dex 文件。由于 .class 文件在两次调用之间没有显示任何区别,因此相应的 .dex 文件也不会显示任何差异:

Processing 'TestQualified.dex'...
Opened 'TestQualified.dex', DEX version '035'
Class #0            -
  Class descriptor  : 'LTestQualified;'
  Access flags      : 0x0001 (PUBLIC)
  Superclass        : 'Ljava/lang/Object;'
  Interfaces        -
  Static fields     -
  Instance fields   -
  Direct methods    -
    #0              : (in LTestQualified;)
      name          : '<init>'
      type          : '()V'
      access        : 0x10001 (PUBLIC CONSTRUCTOR)
      code          -
      registers     : 1
      ins           : 1
      outs          : 1
      insns size    : 4 16-bit code units
0000e4:                                        |[0000e4] TestQualified.<init>:()V
0000f4: 7010 0300 0000                         |0000: invoke-direct {v0}, Ljava/lang/Object;.<init>:()V // method@0003
0000fa: 0e00                                   |0003: return-void
      catches       : (none)
      positions     : 
        0x0000 line=1
      locals        : 
        0x0000 - 0x0004 reg=0 this LTestQualified; 

    #1              : (in LTestQualified;)
      name          : 'someMethod'
      type          : '()V'
      access        : 0x0002 (PRIVATE)
      code          -
      registers     : 1
      ins           : 1
      outs          : 0
      insns size    : 1 16-bit code units
0000fc:                                        |[0000fc] TestQualified.someMethod:()V
00010c: 0e00                                   |0000: return-void
      catches       : (none)
      positions     : 
        0x0000 line=4
      locals        : 
        0x0000 - 0x0001 reg=0 this LTestQualified; 

  Virtual methods   -
    #0              : (in LTestQualified;)
      name          : 'otherMethod'
      type          : '()V'
      access        : 0x0001 (PUBLIC)
      code          -
      registers     : 1
      ins           : 1
      outs          : 1
      insns size    : 7 16-bit code units
000110:                                        |[000110] TestQualified.otherMethod:()V
000120: 7010 0200 0000                         |0000: invoke-direct {v0}, LTestQualified;.someMethod:()V // method@0002
000126: 7010 0200 0000                         |0003: invoke-direct {v0}, LTestQualified;.someMethod:()V // method@0002
00012c: 0e00                                   |0006: return-void
      catches       : (none)
      positions     : 
        0x0000 line=7
        0x0003 line=8
        0x0006 line=9
      locals        : 
        0x0000 - 0x0007 reg=0 this LTestQualified; 

  source_file_idx   : 3 (TestQualified.java)

您可以看到这两个调用都是使用直接调用。因此这里也不存在性能损失。

No, there isn't any sort of penalty. Both calls will use invokespecial in the bytecode to invoke the member method.

In standard Java, the following code:

public class TestQualified {

   private void someMethod() {
   }

   public void otherMethod() {
       this.someMethod();
       someMethod();
   }
}

yields the following bytecode:

Compiled from "TestQualified.java"
public class TestQualified extends java.lang.Object{
public TestQualified();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

private void someMethod();
  Code:
   0:   return

public void otherMethod();
  Code:
   0:   aload_0
   1:   invokespecial   #2; //Method someMethod:()V
   4:   aload_0
   5:   invokespecial   #2; //Method someMethod:()V
   8:   return

}

As you can see, both calls are the same.

Now Android converts the .class files into dalvik bytecode using the dx tool, which outputs .dex files. Since the .class files don't show any distinction between the two invocations, the corresponding .dex file will show no difference either:

Processing 'TestQualified.dex'...
Opened 'TestQualified.dex', DEX version '035'
Class #0            -
  Class descriptor  : 'LTestQualified;'
  Access flags      : 0x0001 (PUBLIC)
  Superclass        : 'Ljava/lang/Object;'
  Interfaces        -
  Static fields     -
  Instance fields   -
  Direct methods    -
    #0              : (in LTestQualified;)
      name          : '<init>'
      type          : '()V'
      access        : 0x10001 (PUBLIC CONSTRUCTOR)
      code          -
      registers     : 1
      ins           : 1
      outs          : 1
      insns size    : 4 16-bit code units
0000e4:                                        |[0000e4] TestQualified.<init>:()V
0000f4: 7010 0300 0000                         |0000: invoke-direct {v0}, Ljava/lang/Object;.<init>:()V // method@0003
0000fa: 0e00                                   |0003: return-void
      catches       : (none)
      positions     : 
        0x0000 line=1
      locals        : 
        0x0000 - 0x0004 reg=0 this LTestQualified; 

    #1              : (in LTestQualified;)
      name          : 'someMethod'
      type          : '()V'
      access        : 0x0002 (PRIVATE)
      code          -
      registers     : 1
      ins           : 1
      outs          : 0
      insns size    : 1 16-bit code units
0000fc:                                        |[0000fc] TestQualified.someMethod:()V
00010c: 0e00                                   |0000: return-void
      catches       : (none)
      positions     : 
        0x0000 line=4
      locals        : 
        0x0000 - 0x0001 reg=0 this LTestQualified; 

  Virtual methods   -
    #0              : (in LTestQualified;)
      name          : 'otherMethod'
      type          : '()V'
      access        : 0x0001 (PUBLIC)
      code          -
      registers     : 1
      ins           : 1
      outs          : 1
      insns size    : 7 16-bit code units
000110:                                        |[000110] TestQualified.otherMethod:()V
000120: 7010 0200 0000                         |0000: invoke-direct {v0}, LTestQualified;.someMethod:()V // method@0002
000126: 7010 0200 0000                         |0003: invoke-direct {v0}, LTestQualified;.someMethod:()V // method@0002
00012c: 0e00                                   |0006: return-void
      catches       : (none)
      positions     : 
        0x0000 line=7
        0x0003 line=8
        0x0006 line=9
      locals        : 
        0x0000 - 0x0007 reg=0 this LTestQualified; 

  source_file_idx   : 3 (TestQualified.java)

You can see that both calls are made using invoke-direct. Hence there is no performance penalty here either.

岁吢 2024-12-19 06:23:16

不,没有。对于编译器来说都是一样的。使用 this 参考可以被视为阅读帮助,就像 java7 数字文字

No it doesn't. It's all the same for the compiler. Using this reference may be seen as a reading help, like the underscores in the java7 number literals

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文