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

查看:26
本文介绍了为什么 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天全站免登陆