类似com.example.Dog
的类型名称将编译为.class文件中的com/example/Dog
。大多数JVM如何优化它?似乎更长的类型名称(或成员名称)需要更多的字符串比较。
答案 0 :(得分:8)
长类型(类和接口)名称占用符号表中的空间。这些名称用于链接时间,详见Chapter 5 of the Java Virtual Machine Specification中所述。完成所有这些链接后,名称对运行时性能没有影响,除非您使用反射。
答案 1 :(得分:5)
java编译器生成字节码,没有字符串比较。
使用javap检查此类并查看字节码。
例如,这个
class A extends java.lang.Object{
static final boolean $assertionsDisabled;
A();
public static void main(java.lang.String[]);
static {};
}
转过来
class A extends java.lang.Object{
static final boolean $assertionsDisabled;
A();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field $assertionsDisabled:Z
3: ifne 14
6: new #3; //class java/lang/AssertionError
9: dup
10: invokespecial #4; //Method java/lang/AssertionError."<init>":()V
13: athrow
14: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream;
17: ldc #6; //String hi
19: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
22: return
static {};
Code:
0: ldc_w #8; //class A
3: invokevirtual #9; //Method java/lang/Class.desiredAssertionStatus:()Z
6: ifne 13
9: iconst_1
10: goto 14
13: iconst_0
14: putstatic #2; //Field $assertionsDisabled:Z
17: return
}
内部jvm使用数字ID。长变量名和短变量名之间没有区别。
编辑:添加-l输出
Compiled from "A.java"
class A extends java.lang.Object{
static final boolean $assertionsDisabled;
A();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field $assertionsDisabled:Z
3: ifne 14
6: new #3; //class java/lang/AssertionError
9: dup
10: invokespecial #4; //Method java/lang/AssertionError."<init>":()V
13: athrow
14: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream;
17: ldc #6; //String hi
19: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
22: return
LineNumberTable:
line 6: 0
line 7: 14
line 8: 22
static {};
Code:
0: ldc_w #8; //class A
3: invokevirtual #9; //Method java/lang/Class.desiredAssertionStatus:()Z
6: ifne 13
9: iconst_1
10: goto 14
13: iconst_0
14: putstatic #2; //Field $assertionsDisabled:Z
17: return
LineNumberTable:
line 3: 0
}
并添加-verbose -s
Compiled from "A.java"
class A extends java.lang.Object
SourceFile: "A.java"
minor version: 0
major version: 50
Constant pool:
const #1 = Method #10.#23; // java/lang/Object."<init>":()V
const #2 = Field #8.#24; // A.$assertionsDisabled:Z
const #3 = class #25; // java/lang/AssertionError
const #4 = Method #3.#23; // java/lang/AssertionError."<init>":()V
const #5 = Field #26.#27; // java/lang/System.out:Ljava/io/PrintStream;
const #6 = String #28; // hi
const #7 = Method #29.#30; // java/io/PrintStream.println:(Ljava/lang/String;)V
const #8 = class #31; // A
const #9 = Method #32.#33; // java/lang/Class.desiredAssertionStatus:()Z
const #10 = class #34; // java/lang/Object
const #11 = Asciz $assertionsDisabled;
const #12 = Asciz Z;
const #13 = Asciz <init>;
const #14 = Asciz ()V;
const #15 = Asciz Code;
const #16 = Asciz LineNumberTable;
const #17 = Asciz main;
const #18 = Asciz ([Ljava/lang/String;)V;
const #19 = Asciz StackMapTable;
const #20 = Asciz <clinit>;
const #21 = Asciz SourceFile;
const #22 = Asciz A.java;
const #23 = NameAndType #13:#14;// "<init>":()V
const #24 = NameAndType #11:#12;// $assertionsDisabled:Z
const #25 = Asciz java/lang/AssertionError;
const #26 = class #35; // java/lang/System
const #27 = NameAndType #36:#37;// out:Ljava/io/PrintStream;
const #28 = Asciz hi;
const #29 = class #38; // java/io/PrintStream
const #30 = NameAndType #39:#40;// println:(Ljava/lang/String;)V
const #31 = Asciz A;
const #32 = class #41; // java/lang/Class
const #33 = NameAndType #42:#43;// desiredAssertionStatus:()Z
const #34 = Asciz java/lang/Object;
const #35 = Asciz java/lang/System;
const #36 = Asciz out;
const #37 = Asciz Ljava/io/PrintStream;;
const #38 = Asciz java/io/PrintStream;
const #39 = Asciz println;
const #40 = Asciz (Ljava/lang/String;)V;
const #41 = Asciz java/lang/Class;
const #42 = Asciz desiredAssertionStatus;
const #43 = Asciz ()Z;
{
static final boolean $assertionsDisabled;
Signature: Z
A();
Signature: ()V
LineNumberTable:
line 3: 0
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
public static void main(java.lang.String[]);
Signature: ([Ljava/lang/String;)V
LineNumberTable:
line 6: 0
line 7: 14
line 8: 22
Code:
Stack=2, Locals=1, Args_size=1
0: getstatic #2; //Field $assertionsDisabled:Z
3: ifne 14
6: new #3; //class java/lang/AssertionError
9: dup
10: invokespecial #4; //Method java/lang/AssertionError."<init>":()V
13: athrow
14: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream;
17: ldc #6; //String hi
19: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
22: return
LineNumberTable:
line 6: 0
line 7: 14
line 8: 22
StackMapTable: number_of_entries = 1
frame_type = 14 /* same */
static {};
Signature: ()V
LineNumberTable:
line 3: 0
Code:
Stack=1, Locals=0, Args_size=0
0: ldc_w #8; //class A
3: invokevirtual #9; //Method java/lang/Class.desiredAssertionStatus:()Z
6: ifne 13
9: iconst_1
10: goto 14
13: iconst_0
14: putstatic #2; //Field $assertionsDisabled:Z
17: return
LineNumberTable:
line 3: 0
StackMapTable: number_of_entries = 2
frame_type = 13 /* same */
frame_type = 64 /* same_locals_1_stack_item */
stack = [ int ]
}
答案 2 :(得分:1)
这部分取决于在JIT编译时是否可以解析实际的精确方法。如果可以,则编译代码可以简单地分支到相关代码。例如,您可以在final vs non-final method calls的这些时间中看到Hotspot这样做的证据。实际上,没有专门针对最终方法的这种优化(这是我在开发人员中发现的常见误解)。但正如这些数据所显示的那样, 是一种优化方法,可以识别精确方法,最终与否。
如果无法识别精确方法,则VM至少可以对方法签名的哈希码进行操作,以便一旦为给定字节码进行的调用计算了一次哈希码给定方法的偏移量,不需要再次计算。我承认我没有看过Hotspot或其他JVM是否真的这样做,但原则上这似乎是一件非常明显的事情。