为什么javac不优化甚至简单的代码? [英] Why javac does not optimize even simple code?

查看:116
本文介绍了为什么javac不优化甚至简单的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

public class MainClass {
    public static int f(){
        int i=0;
        i++;
        return i;
    }
}

编译器javac生成以下代码:

the compiler javac produces the following code:

Compiled from "MainClass.java"
public class latte_jvm.MainClass {

  public static int f();
    Code:
       0: iconst_0
       1: istore_0
       2: iinc          0, 1
       5: iload_0
       6: ireturn
}

函数f确实很简单 - 它只返回1.它是直接翻译的,这让我很难相信java编译器会做任何优化。为什么java编译器创建者决定不在编译阶段进行这样的优化?

Function f does really simple thing - it just returns 1. It's so directly translated that it makes me hard to believe that java compiler does any optimizations at all. Why java compiler creators decided to not do such optimizations in the compilation phase?

推荐答案


直接如此翻译使我很难相信java编译器会进行任何优化。

Is so directly translated that it makes me hard to believe that java compiler does any optimizations at all.

确实如此。大多数Java优化都是在JIT时执行的。很久以前,Java维护者发现在很多情况下,在编译时执行的优化实际上阻碍了JIT时间更重要的优化。

Indeed. Most Java optimization is performed at JIT-time instead. The Java maintainers found out quite a while ago that in many cases, optimizations performed at compile-time actually hindered more important optimizations at JIT-time.

几年来, -O 命令行参数什么也没做 - 而且非常刻意。

For a few years now, the -O command-line argument has done nothing - and very deliberately so.

这篇关于为什么javac不优化甚至简单的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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