很简单,什么是尾部调用优化?
更具体地说,有哪些小代码段可以应用,哪些地方不可以,并解释原因?
很简单,什么是尾部调用优化?
更具体地说,有哪些小代码段可以应用,哪些地方不可以,并解释原因?
当前回答
我们应该确保函数本身没有goto语句。函数调用是被调用函数的最后一个内容。 大规模递归可以使用它进行优化,但在小规模中,使函数调用成为尾部调用的指令开销降低了实际目的。 TCO可能导致一个永远运行的函数: 空白永恒() { 永恒(); }
其他回答
看这里:
http://tratt.net/laurie/tech_articles/articles/tail_call_optimization
你可能知道,递归函数调用会对堆栈造成严重破坏;堆栈空间很容易很快用完。尾部调用优化是一种方法,通过它你可以创建一个使用常量堆栈空间的递归式算法,因此它不会不断增长,你会得到堆栈错误。
尾部调用优化可以避免为函数分配新的堆栈框架,因为调用函数将简单地返回从被调用函数获得的值。最常见的用法是尾部递归,其中为利用尾部调用优化而编写的递归函数可以使用常量堆栈空间。
Scheme是少数几种在规范中保证任何实现都必须提供这种优化的编程语言之一,因此这里有Scheme中的阶乘函数的两个示例:
(define (fact x)
(if (= x 0) 1
(* x (fact (- x 1)))))
(define (fact x)
(define (fact-tail x accum)
(if (= x 0) accum
(fact-tail (- x 1) (* x accum))))
(fact-tail x 1))
第一个函数不是尾部递归的,因为当进行递归调用时,函数需要跟踪调用返回后需要与结果进行的乘法运算。因此,堆栈看起来如下所示:
(fact 3)
(* 3 (fact 2))
(* 3 (* 2 (fact 1)))
(* 3 (* 2 (* 1 (fact 0))))
(* 3 (* 2 (* 1 1)))
(* 3 (* 2 1))
(* 3 2)
6
相反,尾部递归阶乘的堆栈跟踪如下所示:
(fact 3)
(fact-tail 3 1)
(fact-tail 2 3)
(fact-tail 1 6)
(fact-tail 0 6)
6
正如您所看到的,对于每次调用fact-tail,我们只需要跟踪相同数量的数据,因为我们只是返回我们直接到达顶部的值。这意味着即使我要调用(事实1000000),我只需要与(事实3)相同的空间。对于非尾部递归事实,情况并非如此,如此大的值可能会导致堆栈溢出。
首先请注意,并非所有语言都支持它。
TCO适用于递归的特殊情况。它的要点是,如果你在函数中做的最后一件事是调用自己(例如,它从“尾部”位置调用自己),这可以被编译器优化为像迭代一样而不是标准的递归。
You see, normally during recursion, the runtime needs to keep track of all the recursive calls, so that when one returns it can resume at the previous call and so on. (Try manually writing out the result of a recursive call to get a visual idea of how this works.) Keeping track of all the calls takes up space, which gets significant when the function calls itself a lot. But with TCO, it can just say "go back to the beginning, only this time change the parameter values to these new ones." It can do that because nothing after the recursive call refers to those values.
TCO (Tail Call Optimization) is the process by which a smart compiler can make a call to a function and take no additional stack space. The only situation in which this happens is if the last instruction executed in a function f is a call to a function g (Note: g can be f). The key here is that f no longer needs stack space - it simply calls g and then returns whatever g would return. In this case the optimization can be made that g just runs and returns whatever value it would have to the thing that called f.
这种优化可以使递归调用占用恒定的堆栈空间,而不是爆炸。
示例:这个阶乘函数不是TCOptimizable:
from dis import dis
def fact(n):
if n == 0:
return 1
return n * fact(n-1)
dis(fact)
2 0 LOAD_FAST 0 (n)
2 LOAD_CONST 1 (0)
4 COMPARE_OP 2 (==)
6 POP_JUMP_IF_FALSE 12
3 8 LOAD_CONST 2 (1)
10 RETURN_VALUE
4 >> 12 LOAD_FAST 0 (n)
14 LOAD_GLOBAL 0 (fact)
16 LOAD_FAST 0 (n)
18 LOAD_CONST 2 (1)
20 BINARY_SUBTRACT
22 CALL_FUNCTION 1
24 BINARY_MULTIPLY
26 RETURN_VALUE
这个函数除了在它的return语句中调用另一个函数之外还做其他事情。
下面这个函数是TCOptimizable:
def fact_h(n, acc):
if n == 0:
return acc
return fact_h(n-1, acc*n)
def fact(n):
return fact_h(n, 1)
dis(fact)
2 0 LOAD_GLOBAL 0 (fact_h)
2 LOAD_FAST 0 (n)
4 LOAD_CONST 1 (1)
6 CALL_FUNCTION 2
8 RETURN_VALUE
这是因为在这些函数中最后发生的事情是调用另一个函数。
我所发现的关于尾部调用、递归尾部调用和尾部调用优化的最好的高级描述可能是博客文章
“这到底是什么鬼:一个跟踪电话”
作者:Dan Sugalski。关于尾部调用优化,他写道:
Consider, for a moment, this simple function: sub foo (int a) { a += 15; return bar(a); } So, what can you, or rather your language compiler, do? Well, what it can do is turn code of the form return somefunc(); into the low-level sequence pop stack frame; goto somefunc();. In our example, that means before we call bar, foo cleans itself up and then, rather than calling bar as a subroutine, we do a low-level goto operation to the start of bar. Foo's already cleaned itself out of the stack, so when bar starts it looks like whoever called foo has really called bar, and when bar returns its value, it returns it directly to whoever called foo, rather than returning it to foo which would then return it to its caller.
关于尾递归:
尾递归发生在函数作为其最后一个操作返回时 调用自身的结果。尾递归更容易处理 因为与其跳到某个随机的开头 函数,你只需要回到开始 你自己,这是件非常简单的事情。
所以:
Sub foo (int a, int b) { 如果(b == 1) { 返回一个; }其他{ 返回foo(a*a + a, b - 1); }
悄然变成:
Sub foo (int a, int b) { 标签: 如果(b == 1) { 返回一个; }其他{ A = A * A + A; B = B - 1; goto标签; }
我喜欢这个描述的地方在于,对于那些有命令式语言(C、c++、Java)背景的人来说,它是如此的简洁和容易掌握。