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

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

问题描述

执行-jvm-prevent-tail-call-两年后优化,似乎有一个原型 实施 MLVM 已将该功能列为原型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构建并且可以共享s,我会非常感兴趣ome对其运作情况的印象(如果有的话)。

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的Hotspot上运行
版本1.3的JVM显示
Hotspot不执行
转换。在默认设置下,
堆栈空间耗尽少于
,而不是我机器上的秒。另一方面,在
上,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天全站免登陆