为什么JVM还不支持尾调用优化? [英] Why does the JVM still not support tail-call optimization?

查看:28
本文介绍了为什么JVM还不支持尾调用优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两年后 does-the-jvm-prevent-tail-call-优化,似乎有一个原型 实施MLVM 将该功能列为proto 80%"已有一段时间了.

Two years after does-the-jvm-prevent-tail-call-optimizations, there seems to be a prototype implementation and MLVM has listed the feature as "proto 80%" for some time now.

Sun/Oracle 方面是否对支持尾调用没有积极的兴趣,或者只是尾调用[...] 注定要在每个功能优先级列表中排在第二位 [...]",如 JVM 语言峰会?

Is there no active interest from Sun's/Oracle's side in supporting tail calls or is it just that tail calls are "[...] fated to come in second place on every feature priority list [...]" as mentioned at the JVM Language Summit?

如果有人测试过 MLVM 构建并且可以分享它的工作效果(如果有的话),我会非常感兴趣.

I would be really interested if someone has tested a MLVM build and could share some impressions of how well it works (if at all).

更新: 请注意,某些虚拟机如 Avian 支持正确的尾调用,没有任何问题.

Update: Note that some VMs like Avian support proper tail-calls without any issues.

推荐答案

诊断 Java 代码:提高 Java 代码的性能 (alt) 解释了 JVM 不支持尾调用优化的原因.

Diagnosing Java Code: Improving the Performance of Your Java Code (alt) explains why the JVM does not support tail-call optimization.

尽管众所周知如何将尾递归函数自动转换为简单循环,但 Java 规范并不要求进行这种转换.据推测,它不是必需的一个原因是,通常不能在面向对象的语言中静态地进行转换.相反,从尾递归函数到简单循环的转换必须由 JIT 编译器动态完成.

But although it is well known how to automatically transform a tail-recursive function into a simple loop, the Java specification doesn't require that this transformation be made. Presumably, one reason it is not a requirement is that, in general, the transformation can't be made statically in an object-oriented language. Instead, the transformation from tail-recursive function to simple loop must be done dynamically by a JIT compiler.

然后给出了一个不会转换的 Java 代码示例.

It then gives an example of Java code that won't transform.

因此,如清单 3 中的示例所示,我们不能指望静态编译器在保留语言语义的同时对 Java 代码执行尾递归转换.相反,我们必须依靠 JIT 的动态编译.根据 JVM,JIT 可能会也可能不会这样做.

So, as the example in Listing 3 shows, we cannot expect static compilers to perform transformation of tail recursion on Java code while preserving the semantics of the language. Instead, we must rely on dynamic compilation by the JIT. Depending on the JVM, the JIT may or may not do this.

然后它提供了一个测试,您可以使用它来确定您的 JIT 是否执行此操作.

Then it gives a test you can use to figure out if your JIT does this.

当然,由于这是一篇 IBM 论文,它包含一个插件:

Naturally, since this is an IBM paper, it includes a plug:

我用几个运行这个程序Java SDK,结果是奇怪.在 Sun 的热点上运行1.3 版的 JVM 显示热点不执行转型.在默认设置下,堆栈空间用完了更少在我的机器上超过一秒钟.在另一方面,IBM 的 JVM 版本 1.3毫无问题地发出咕噜声,表明它确实改变了以这种方式编码.

I ran this program with a couple of the Java SDKs, and the results were surprising. Running on Sun's Hotspot JVM for version 1.3 reveals that Hotspot doesn't perform the transformation. At default settings, the stack space is exhausted in less than a second on my machine. On the other hand, IBM's JVM for version 1.3 purrs along without a problem, indicating that it does transform the code in this way.

这篇关于为什么JVM还不支持尾调用优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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