Java 静态最终字段与TrustFinalOnStaticFields
假设我有一个简单的方法:Java 静态最终字段与TrustFinalOnStaticFields,java,jvm,jit,java-15,Java,Jvm,Jit,Java 15,假设我有一个简单的方法: 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) {
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;
}
}
我不知道装配是否很好,但这是显而易见的:
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;
}
}
我在大会上确实看到:
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
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
应该显示mov eax,0x4
,但令我惊讶的是,它没有显示,代码保持为:
shl eax,1
有人能解释一下发生了什么事以及我遗漏了什么吗?
TrustFinalOnStaticFields
支持从常量对象折叠最终
实例字段。但是,在您的示例中,实例
字段是非常量的,因此折叠me
字段的负载是不正确的,因为编译后,实例
对象可能仍会在某个点发生更改
此外,您正在打印go
方法的程序集,其中this
将不会被视为一个常量,如果该方法是单独编译的。要查看TrustFinalOnStaticFields
的效果,您需要查看程序集中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;
}
}
生成程序集,我们可以在其中看到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
顺便说一下,VM信任记录和隐藏类中的非静态final字段AFAIK。但是你仍然需要一个“静态的最终根”。约翰尼斯是对的,关于记录也是如此。有关隐式受信任的最终实例字段的标准,请参见(注意
if
链如何在return TrustFinalNonStaticFields
中触底)。@jornverne关于链接代码的一些问题:1)它不应该只检查非静态最终字段吗?系统没有任何实例-或实例成员。2) jl.String不在java.lang包中吗?3) box类不是java.lang中的吗?@JohannesKuhn似乎都很粗糙(AFAICS);系统中也没有实例字段,该检查仅用于非静态字段。java/lang
include是作为JDK14中第一个内存访问API孵化器的一部分添加的。我不知道为什么添加了java/lang
,但无论如何,当时似乎没有清理j.l.String和box类的案例。谢谢@jornverne。这是我的怀疑,但我对代码了解不够,甚至没有提出任何行动建议。所以我只是问一些愚蠢的问题,所以我可能在某个时候能够做到。
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
# {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
# {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