条件 JIT 编译
在 Java 中,我们可以像这样进行条件编译。
private static final boolean DO_CHECK = false;
...
if (DO_CHECK) {
// code here
}
编译器会看到 DO_CHECK 始终为 false,并删除整个 if 语句。然而,有时,特别是在库代码中,我们不能使用条件编译,但我想知道,我们可以使用条件JIT编译吗?
private final boolean doCheck;
public LibraryClass(boolean d) {
doCheck = d;
}
public void oftenCalledMethod() {
if (doCheck) {
...
}
}
如果我们使用 doCheck = false 构造 LibraryClass,JIT 编译器(在 Hotspot 中)是否也会删除 if 语句?
更新:我刚刚意识到 JIT 编译很可能不是在实例级别完成的,所以我认为这不起作用,但也许有一种静态方法?
In Java we can do conditional compilation like so
private static final boolean DO_CHECK = false;
...
if (DO_CHECK) {
// code here
}
The compiler will see that DO_CHECK is always false and remove the entire if-statement. However, sometimes, especially in library code, we can't use conditional compilation, but I'm wondering, can we use conditional JIT-compilation?
private final boolean doCheck;
public LibraryClass(boolean d) {
doCheck = d;
}
public void oftenCalledMethod() {
if (doCheck) {
...
}
}
If we construct LibraryClass with doCheck = false, will the JIT-compiler (in Hotspot) remove the if-statement as well?
Update: I just realised that JIT-compilation is most probably not done on instance level, so I think this wouldn't work, but maybe there's a static way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JIT 代表“及时”。这意味着在虚拟机认为需要它之前就编译了一些东西。因此,根据检查的原子性级别,您可能会发现永远不会运行的代码无论如何都不会被 JIT 编译。
JIT stands for "just in time". That means stuff is compiled just before the VM thinks it needs it. So depending on the level of atomicity of checking you might find the code that never gets run never gets JIT compiled anyway.