Java优化器会删除空方法调用的参数构造吗? [英] Will the Java optimizer remove parameter construction for empty method calls?

查看:79
本文介绍了Java优化器会删除空方法调用的参数构造吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有类似的代码:

log.info("Now the amount" + amount + " seems a bit high")

然后我将log方法替换为虚拟实现,例如:

and I would replace the log method with a dummy implementation like :

class Logger {
   ...
   public void info() {}

}

优化器是否将其内联,并且如果未检测到副作用,死代码删除会不会删除参数构造?

Will the optimizer inline it and will the dead code removal remove the parameter construction if no side effects are detected?

推荐答案

我想编译器(javac)不会,但是即时编译器很有可能会.

I would guess that the compiler (javac) will not, but that the just in time compiler very likely will.

但是,要使其工作,必须能够推断出您用来生成参数的任何指令都没有副作用.对于字符串的特殊情况,它应该能够做到这一点,但对于其他方法则可能不行.

For it to work however, it has to be able to deduct that whatever instructions you use to generate the parameter have no side effects. It should be able to do that for the special case of strings, but it might not for other methods.

可以肯定的是,做一个小的基准测试,将其与Jesper建议并查看的代码进行比较,以较快者或同样快者为准.

To be sure, make a small benchmark that compares this with the code that Jesper suggested and see, whichever is faster or if they are equally fast.

另请参见此处: http://www .ibm.com/developerworks/java/library/j-jtp12214/index.html#3.0

这篇关于Java优化器会删除空方法调用的参数构造吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆