现代Java编译器/JVM内联函数/方法是否完全从一个地方调用? [英] Do modern Java compilers/JVM inline functions/methods which are called exactly from one place?

查看:54
本文介绍了现代Java编译器/JVM内联函数/方法是否完全从一个地方调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现C ++编译器这样做了,但是我想知道Java编译器是否做同样的事情,因为他们在回答中说添加static可以这样做,但是static在Java和C ++中是不同的.就我而言,性能很重要,因为我使用的功能是在游戏循环中每帧仅调用一次,而在其他地方则不调用,以使其更具可读性
在我的代码中,除了有很多调用之外,我的设置与此类似

I found out that the C++ compiler does so but I want to know if the Java compiler does the same since in that answer they said adding static would do so but static is different in java and C++. In my case performance would matter since am using functions that are called only once per frame in a game loop and called nowhere else, to make it more readable
In my code I have it setup up similar to this, except with many more calls

while(running)
{
    update();
    sync();
}

然后update(),render()将调用更多调用其他方法的方法

and then update(), render() would call more methods that call other methods

private final void update()
{
    switch(gameState)
    {
        case 0:
            updateMainMenu();
            renderMainMenu();
            break;
        case 1:
            updateInGame();
            renderInGame();
            break;
         //and so on
    }
}

private final void updateInGame()
{
    updatePlayerData();
    updateDayCycle();
    //and so on
}

private final void updatePlayerData()
{
    updateLocation();
    updateHealth();
    //and so on
}

编译器是否会内联这些功能,因为它们在同一位置每帧仅使用一次?

So would the compiler inline these functions since they are only used once per frame in the same location?

如果这是一个不好的问题,请告诉我,我将其删除.

If this is a bad question, plz tell me and I will remove it.

推荐答案

Java JITC会尝试内联任何经常被调用的功能(基于运行时统计信息),以使其值得使用.只需在一个地方还是几十个地方调用该函数都没关系-每个调用站点都会分别进行分析.

A Java JITC will attempt to inline any functions that appear (based on runtime statistics) to be called often enough to merit it. It doesn't matter whether the function is called in only one place or dozens of places -- each calling site is analyzed separately.

请注意,此决定基于多个因素.该方法有多大?-如果有很多潜在的内联候选人,则仅内联最有利可图的候选人,以避免代码膨胀".但是通话频率(乘以所感知的通话费用)是最大的得分"因素.

Note that the decision is based on several factors. How big the method is is one -- if there are a lot of potential inlining candidates only the most profitable will be inlined, to avoid "code bloat". But the frequency of the call (multiplied by the perceived expense of the call) is the biggest "score" factor.

将阻止内联的一件事是明显的多态调用.如果调用可能是多态的,则如果到达的类不是预期的类,则必须由执行原始调用的代码保护".如果统计数据证明呼叫经常是多态的(并且不值得包含所有多态变体),则内联可能没有足够的利润.静态或最终方法最吸引人,因为它不需要保护.

One thing that will discourage inlining is obvious polymorphic calls. If a call might be polymorphic it must be "guarded" by code that will execute the original call if the arriving class is not the expected one. If statistics prove that a call is frequently polymorphic (and including all the polymorphic variants is not worthwhile) then it's likely not sufficiently profitable to inline. A static or final method is the most attractive, since it requires no guard.

奇怪的是,另一种会阻止内联(以及很多其他东西)的事情是从该方法返回失败.如果您输入了一个方法,然后在内部循环了1000万次而没有返回,则JITC将永远没有机会交换"出已解释的方法并交换"出已编译的方法.但是JITC通过使用仅编译一部分方法的技术来在某种程度上克服了这一问题,而其余部分则得到了解释.

Another thing that can discourage inlining (and a lot of other stuff) is, oddly enough, a failure to return from the method. If you have a method that's entered and then loops 10 million times internally without returning, the JITC never gets a chance to "swap out" the interpreted method and "swap in" the compiled one. But JITCs overcome this to a degree by using techniques for compiling only part of a method, leaving the rest interpreted.

这篇关于现代Java编译器/JVM内联函数/方法是否完全从一个地方调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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