在Java中内联 [英] Inlining in Java

查看:201
本文介绍了在Java中内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,我可以声明一个方法内联,编译器可能会内联它。据我所知,Java中没有这样的关键字。



如果JVM决定这样做,内联是否完成?我可以以某种方式影响这个决定吗?

解决方案

其他几个答案表明只有最终方法可以内联 - 这是不正确,因为HotSpot非常聪明,能够内联非最终方法,只要它们没有被覆盖尚未。当加载一个覆盖该方法的类时,它可以撤消其优化。显然,使方法最终意味着永远不需要...



基本上让JVM完成它的工作 - 它可能会比你更好地计算内联的位置你是否确信JVM没有做好工作?假设您正在使用HotSpot,您是否尝试过使用服务器版本而不是客户端?这可能会产生巨大的差异。


In C++ I can declare a method "inline" and the compiler is likely to inline it. As far as I understand there is no such keyword in Java.

Inlining is done if the JVM decides to do so? Can I influence this decision somehow?

解决方案

A couple of the other answers have suggested that only final methods can be inlined - this is not true, as HotSpot is smart enough to be able to inline non-final methods so long as they haven't been overridden yet. When a class is loaded which overrides the method, it can undo its optimisation. Obviously making the method final mean that's never required...

Basically let the JVM do its job - it's likely to be a lot better at working out where to inline than you are.

Do you have a situation where you're convinced that the JVM isn't doing a good job? Assuming you're using HotSpot, have you tried using the server version instead of client? That can make a huge difference.

这篇关于在Java中内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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