静态最终字段 vs TrustFinalNonStaticFields

2022-09-03 02:05:40

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

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

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

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

我试图用以下方法测试它:

 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

的结果立即,即:JIT“可信”为常量,因此.go4me2 * 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

这实际上是 with 的乘法,通过移位完成。因此,JIT不相信它是一个常量,这是意料之中的。me2me

现在的问题是。我以为如果我添加标志,我会看到相同的,即:运行:TrustFinalNonStaticFieldsmov 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

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


答案 1

TrustFinalNonStaticFields允许从常量对象折叠实例字段。但是,在您的示例中,字段是非恒定的,因此折叠字段的负载是不正确的,因为对象在编译后的某个时刻可能仍会更改。finalinstancemeinstance

此外,您正在打印出该方法的程序集,如果该方法单独编译,则不会将其视为常量。若要查看 的效果,需要查看该方法的内联版本的程序集,其中接收器是一个常量。例如:gothisTrustFinalNonStaticFieldsgo

 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

生成程序集,我们可以在其中看到字段的负载+乘法正在方法中折叠:mepayload

  # {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

答案 2

推荐