Probability of getters and setters getting inlined by the compiler

南楼画角 提交于 2019-12-01 03:59:20

The compiler (javac) tend to have negligible impact on optimization, as optimization happens at run time.

As of the JIT yes,it will probably inline either sooner or later.depending on how heavily the code is used, so a function call overhead may be seen at first, but when the getter/setter has been called sufficiently often then it is inlined.

I imagine about zero (at least in the non-JIT case), since these are Java Bean conventions and they always need to be public, so the compiler cannot predict who might call the methods. You might change the implementation of one of these which would break an inlined caller. There is nothing to require that they need to be implemented only to set a field.

The compiler may inline when a method is final and the accessed fields are accessible to the caller. Then it's up to the compiler to determine whether the method is "simple" enough for inlining.

In practice, setting or getting a field is generally considered simple enough, so a final accessor for an accessible field will be inlined. Accessors for private fields will be inlined within their declaring class; accessors for protected fields will be inlined throughout the declaring package and any derived classes; &c.

At runtime, the JIT is likely to perform additional inlining based on analysis of running code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!