Java JVM 是否会阻止尾调用优化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/105834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:21:07  来源:igfitidea点击:

Does the JVM prevent tail call optimizations?

javajvmscalatail-recursion

提问by Jason Dagit

I saw this quote on the question: What is a good functional language on which to build a web service?

我在这个问题上看到了这句话:什么是构建 Web 服务的好的函数式语言?

Scala in particular doesn't support tail-call elimination except in self-recursive functions, which limits the kinds of composition you can do (this is a fundamental limitation of the JVM).

Scala 特别不支持尾调用消除,除了自递归函数,这限制了您可以执行的组合类型(这是 JVM 的基本限制)。

Is this true? If so, what is it about the JVM that creates this fundamental limitation?

这是真的?如果是这样,造成这种基本限制的 JVM 是什么?

采纳答案by Michael Myers

This post: Recursion or Iteration?might help.

这篇文章:递归还是迭代?可能有帮助。

In short, tail call optimization is hard to do in the JVM because of the security model and the need to always have a stack trace available. These requirements could in theory be supported, but it would probably require a new bytecode (see John Rose's informal proposal).

简而言之,由于安全模型和始终有可用的堆栈跟踪的需要,在 JVM 中很难进行尾调用优化。理论上可以支持这些要求,但它可能需要一个新的字节码(参见John Rose 的非正式提案)。

There is also more discussion in Sun bug #4726340, where the evaluation (from 2002) ends:

Sun bug #4726340 中还有更多讨论,评估(从 2002 年开始)在此处结束:

I believe this could be done nonetheless, but it is not a small task.

我相信这仍然可以做到,但这不是一项小任务。

Currently, there is some work going on in the Da Vinci Machineproject. The tail call subproject's status is listed as "proto 80%"; it is unlikely to make it into Java 7, but I think it has a very good chance at Java 8.

目前,达芬奇机器项目正在进行一些工作。尾调用子项目的状态列为“proto 80%”;它不太可能进入 Java 7,但我认为它在 Java 8 中很有可能。

回答by faran

In addition to the paper linked in Lambda The Ultimate (from the link mmyers posted above), John Rose from Sun has some more to say about tail call optimization.

除了 Lambda The Ultimate 中链接的论文(来自上面发布的链接 mmyers)之外,来自 Sun 的 John Rose 对尾调用优化还有更多要说的内容。

http://blogs.oracle.com/jrose/entry/tail_calls_in_the_vm

http://blogs.oracle.com/jrose/entry/tail_calls_in_the_vm

I have heard that it might be implemented on the JVM someday. Tail call support amongst other things are being looked at on the Da Vinci Machine.

我听说有一天它可能会在 JVM 上实现。Da Vinci Machine 正在研究尾部调用支持等。

http://openjdk.java.net/projects/mlvm/

http://openjdk.java.net/projects/mlvm/

回答by Jon Harrop

The fundamental limitation is simply that the JVM does not provide tail calls in its byte code and, consequently, there is no direct way for a language built upon the JVM to provide tail calls itself. There are workarounds that can achieve a similar effect (e.g. trampolining) but they come at the grave cost of awful performance and obfuscating the generated intermediate code which makes a debugger useless.

基本的限制很简单,JVM 在其字节码中不提供尾调用,因此,构建在 JVM 上的语言没有直接的方法来提供尾调用本身。有一些解决方法可以实现类似的效果(例如蹦床),但它们的代价是糟糕的性能和混淆生成的中间代码,从而使调试器无用。

So the JVM cannot support any production-quality functional programming languages until Sun implement tail calls in the JVM itself. They have been discussing it for years but I doubt they will ever implement tail calls: it will be very difficult because they have prematurely optimized their VM before implementing such basic functionality, and Sun's effort is strongly focused on dynamic languages rather than functional languages.

因此,在 Sun 在 JVM 本身中实现尾调用之前,JVM 无法支持任何生产质量的函数式编程语言。他们已经讨论了多年,但我怀疑他们是否会实现尾调用:这将非常困难,因为他们在实现这些基本功能之前过早地优化了他们的 VM,而 Sun 的努力主要集中在动态语言而不是函数式语言上。

Hence there is a very strong argument that Scala is not a real functional programming language: these languages have regarded tail calls as an essential feature since Scheme was first introduced over 30 years ago.

因此,有一个非常有力的论点认为 Scala 不是真正的函数式编程语言:自从 30 多年前首次引入 Scheme 以来,这些语言就将尾调用视为一项基本功能。

回答by Daniel C. Sobral

Scala 2.7.x supports tail-call optimization for self-recursion (a function calling itself) of final methods and local functions.

Scala 2.7.x 支持对 final 方法和局部函数的自递归(调用自身的函数)进行尾调用优化。

Scala 2.8 might come with library support for trampoline too, which is a technique to optimize mutually recursive functions.

Scala 2.8 也可能附带对蹦床的库支持,这是一种优化相互递归函数的技术。

A good deal of information about the state of Scala recursion can be found in Rich Dougherty's blog.

关于 Scala 递归状态的大量信息可以在Rich Dougherty 的博客中找到

回答by fthinker

All sources point to the JVM being unable to optimize in the case of tail recursion, but upon reading Java performance tuning(2003, O'reilly) I found the author claiming he can achieve greater recursion performance by implementing tail recursion.

所有来源都指出 JVM 在尾递归的情况下无法优化,但是在阅读Java 性能调优(2003, O'reilly) 时,我发现作者声称他可以通过实现尾递归来获得更高的递归性能。

You can find his claim on page 212 (search for 'tail recursion' it should be the second result). What gives?

您可以在第 212 页找到他的声明(搜索“尾递归”应该是第二个结果)。是什么赋予了?