开发者

Conditional JIT-compilation

In Java we can do conditional compilation like so

p开发者_如何学JAVArivate 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜