JVM 会阻止尾调用优化吗?
我在这个问题上看到了这句话:构建 Web 服务的好的函数式语言是什么?
I saw this quote on the question: What is a good functional language on which to build a web service?
特别是 Scala 不支持尾调用消除,除非在自递归函数中,这限制了您可以执行的组合类型(这是 JVM 的基本限制).
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).
这是真的吗?如果是这样,那么造成这种基本限制的 JVM 是什么?
Is this true? If so, what is it about the JVM that creates this fundamental limitation?
推荐答案
这篇文章:递归还是迭代? 可能会有所帮助.
This post: Recursion or Iteration? might help.
简而言之,由于安全模型和需要始终提供可用的堆栈跟踪,尾调用优化很难在 JVM 中进行.这些要求理论上可以得到支持,但它可能需要一个新的字节码(参见 John Rose 的非正式提案).
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).
Sun bug #4726340 中也有更多讨论,评估(从 2002 年开始)在哪里结束:
There is also more discussion in Sun bug #4726340, where the evaluation (from 2002) ends:
我相信这仍然可以完成,但这不是一项小任务.
I believe this could be done nonetheless, but it is not a small task.
目前,达芬奇机器正在进行一些工作项目.尾调用子项目的状态列为proto 80%";它不太可能进入 Java 7,但我认为它很有可能进入 Java 8.
Currently, there is some work going on in the Da Vinci Machine project. 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.
相关文章