静态常量字段 vs 可信的非静态字段

18

假设我有这个简单的方法:

static final Integer me = Integer.parseInt("2");

static int go() {
    return me * 2;
}

对于javac而言,me不是一个常量(根据JLS规则),但对于JIT来说可能大多数是。

我尝试用以下代码进行测试:

 public class StaticFinal {

    public static void main(String[] args) {
        int hash = 0;
        for(int i=0;i<1000_000;++i){
            hash = hash ^ go();
        }
        System.out.println(hash);
    }

    static final Integer me = Integer.parseInt("2");

    static int go() {
        return me * 2;
    }
}

并使用以下方式运行:

  java -XX:+UnlockDiagnosticVMOptions 
       -XX:-TieredCompilation  
       "-XX:CompileCommand=print,StaticFinal.go"  
       -XX:PrintAssemblyOptions=intel  
       StaticFinal.java

我不太懂汇编语言,但这一点很明显:

mov    eax,0x4

执行 go 的结果立即为 4,即: JIT“信任”me是一个常数,因此 2 * 2 = 4

如果我去掉 static 并将代码更改为:

public class NonStaticFinal {

    static NonStaticFinal instance = new NonStaticFinal();

    public static void main(String[] args) {
        int hash = 0;
        for(int i=0;i<1000_000;++i){
            hash = hash ^ instance.go();
        }
        System.out.println(hash);
    }

    final Integer me = Integer.parseInt("2");

    int go() {
        return me * 2;
    }
}

并使用以下方式运行:

java -XX:+UnlockDiagnosticVMOptions 
     -XX:-TieredCompilation  
     "-XX:CompileCommand=print,NonStaticFinal.go"  
     -XX:PrintAssemblyOptions=intel  
     NonStaticFinal.java

我在汇编中看到:

shl    eax,1

其实这是通过移位运算来将 me 乘以 2,所以 JIT 不信任 me 是一个常量,这是可以预料的。

现在的问题是:我认为如果我添加 TrustFinalNonStaticFields 标志,我会看到相同的 mov eax 0x4,也就是说:

 java -XX:+UnlockDiagnosticVMOptions 
      -XX:-TieredCompilation  
      "-XX:CompileCommand=print,NonStaticFinal.go"  
      -XX:+UnlockExperimentalVMOptions 
      -XX:+TrustFinalNonStaticFields 
      -XX:PrintAssemblyOptions=intel  
      NonStaticFinal.java

我本以为会展示mov eax,0x4,但惊讶的是它没有出现,代码保持原样:

shl    eax,1

有人可以解释一下正在发生什么以及我错过了什么吗?


2
顺便提一下,虚拟机信任记录和隐藏类中的非静态 final 字段,据我所知。但你仍然需要一个“静态 final 根”。 - Johannes Kuhn
3
约翰内斯的看法是正确的,对于记录也是如此。请参见 ciField::trust_final_non_static_fields 以了解隐式信任的最终实例字段的标准(请注意,if链底部为 return TrustFinalNonStaticFields)。 - Jorn Vernee
@JornVernee 关于链接代码的一些问题:1)它应该只检查非静态final字段吗? jl.System没有任何实例或实例成员。 2)jl.String不在java.lang包中吗?3)装箱类不在java.lang中吗? - Johannes Kuhn
1
@JohannesKuhn 两者似乎都是无用的(据我所见); 在添加j.l.System排除时,System中也没有实例字段,检查仅用于非静态字段。 java/lang包含在JDK 14中的第一个内存访问API孵化器中。 我不知道为什么添加了java/lang,但无论如何,似乎错过了清理j.l.String和盒子类的情况。 - Jorn Vernee
感谢@JornVernee。这正是我怀疑的,但我对代码的理解不足以提出任何建议。所以我只是问一些愚蠢的问题,以便在某个时候能够做到那一点。 - Johannes Kuhn
1个回答

20
TrustFinalNonStaticFields 可以折叠常量对象中的 final 实例字段。然而,在您的示例中,instance 字段是非常量的,因此折叠对于加载 me 字段是不正确的,因为 instance 对象在编译后某个时刻仍可能被更改。

此外,您正在打印出 go 方法的汇编代码,单独编译该方法时,其中的 this 将不会被视为常量。要查看 TrustFinalNonStaticFields 的效果,您需要查看内联版本的 go 方法的汇编代码,其中接收者是常量。例如:

 public class NonStaticFinal {

    static final NonStaticFinal instance = new NonStaticFinal();

    public static void main(String[] args) {
        for (int i = 0; i < 20_000; i++) { // trigger compilation of 'payload'
            payload();
        }
    }
    
    static int payload() {
        return instance.go();
    }

    final Integer me = Integer.parseInt("2");

    int go() {
        return me * 2;
    }

}

使用以下配置运行:

java 
  -XX:+UnlockDiagnosticVMOptions 
  -XX:-TieredCompilation
  "-XX:CompileCommand=print,NonStaticFinal.payload"
  "-XX:CompileCommand=dontinline,NonStaticFinal.payload"
  -XX:+UnlockExperimentalVMOptions
  -XX:+TrustFinalNonStaticFields
  -XX:PrintAssemblyOptions=intel
  -Xbatch
  NonStaticFinal.java

生成汇编代码,我们可以看到payload方法中me字段的加载和乘法操作被折叠在一起。

  # {method} {0x0000016238c59470} 'payload' '()I' in 'NonStaticFinal'
  #           [sp+0x20]  (sp of caller)
  // set up frame
  0x00000162283d2500:   sub     rsp,18h
  0x00000162283d2507:   mov     qword ptr [rsp+10h],rbp     ;*synchronization entry
                                                            ; - NonStaticFinal::payload@-1 (line 12)
  // load a constant 4
  0x00000162283d250c:   mov     eax,4h     <-------------
  // clean up frame
  0x00000162283d2511:   add     rsp,10h
  0x00000162283d2515:   pop     rbp
  // safepoint poll
  0x00000162283d2516:   mov     r10,qword ptr [r15+110h]
  0x00000162283d251d:   test    dword ptr [r10],eax         ;   {poll_return}
  // return
  0x00000162283d2520:   ret
与禁用TFNSF版本相比,在其中仍会发生me字段的负载:
  # {method} {0x00000245f9669470} 'payload' '()I' in 'NonStaticFinal'
  #           [sp+0x20]  (sp of caller)
  // stack bang
  0x00000245e8d52a00:   mov     dword ptr [rsp+0ffffffffffff9000h],eax
  // set up frame
  0x00000245e8d52a07:   push    rbp
  0x00000245e8d52a08:   sub     rsp,10h                     ;*synchronization entry
                                                            ; - NonStaticFinal::payload@-1 (line 12)
  // load the 'instance' field. It's a constant, so the address here is constant
  0x00000245e8d52a0c:   mov     r10,70ff107a8h              ;   {oop(a 'NonStaticFinal'{0x000000070ff107a8})}
  // load the (compressed) oop 'me' field at 0ch (first field after the object header)
  0x00000245e8d52a16:   mov     r11d,dword ptr [r10+0ch]    ;*getfield me {reexecute=0 rethrow=0 return_oop=0}
                                                            ; - NonStaticFinal::go@1 (line 18)
                                                            ; - NonStaticFinal::payload@3 (line 12)
  // Load the 'value' field from the Integer object.
  // r12 is the heap base, r11 the compressed oop 'Integer', *8 here to uncompress it,
  // and again loading the first field after the header at 0ch
  0x00000245e8d52a1a:   mov     eax,dword ptr [r12+r11*8+0ch]; implicit exception: dispatches to 0x00000245e8d52a31
  // multiply by 2
  // ABI returns ints in the 'eax' register, so no need to shuffle afterwards
  0x00000245e8d52a1f:   shl     eax,1h                      ;*imul {reexecute=0 rethrow=0 return_oop=0}
                                                            ; - NonStaticFinal::go@8 (line 18)
                                                            ; - NonStaticFinal::payload@3 (line 12)
  // clean up frame
  0x00000245e8d52a21:   add     rsp,10h
  0x00000245e8d52a25:   pop     rbp
  // safepoint poll
  0x00000245e8d52a26:   mov     r10,qword ptr [r15+110h]
  0x00000245e8d52a2d:   test    dword ptr [r10],eax         ;   {poll_return}
  // return
  0x00000245e8d52a30:   ret

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接