Python开启尾递归优化!

djapr8fq 2年前
   <h2><strong>一般递归与尾递归</strong></h2>    <p><strong>一般递归</strong></p>    <pre>  <code class="language-python">def normal_recursion(n):      if n == 1:          return 1      else:          return n + normal_recursion(n-1)</code></pre>    <p>执行:</p>    <pre>  <code class="language-python">normal_recursion(5)  5 + normal_recursion(4)  5 + 4 + normal_recursion(3)  5 + 4 + 3 + normal_recursion(2)  5 + 4 + 3 + 2 + normal_recursion(1)  5 + 4 + 3 + 3  5 + 4 + 6  5 + 10  15</code></pre>    <p>可以看到, 一般递归, 每一级递归都需要调用函数, 会创建新的栈,</p>    <p>随着递归深度的增加, 创建的栈越来越多, 造成爆栈:boom:</p>    <p><strong>尾递归</strong></p>    <p><strong>尾递归基于函数的尾调用</strong>, 每一级调用直接返回函数的返回值更新调用栈,而不用创建新的调用栈, 类似迭代的实现, 时间和空间上均优化了一般递归!</p>    <pre>  <code class="language-python">def tail_recursion(n, total=0):      if n == 0:          return total      else:          return tail_recursion(n-1, total+n)</code></pre>    <p>执行:</p>    <pre>  <code class="language-python">tail_recursion(5)  tail_recursion(4, 5)  tail_recursion(3, 9)  tail_recursion(2, 12)  tail_recursion(1, 14)  tail_recursion(0, 15)  15</code></pre>    <p>可以看到, 每一级递归的函数调用变成"线性"的形式.</p>    <h2><strong>深入理解尾递归</strong></h2>    <p>呃, 所以呢? 是不是感觉还不够过瘾... 谁说尾递归调用就不用创建新的栈呢?</p>    <p>还是让我们去底层一探究竟吧</p>    <pre>  <code class="language-python">int tail_recursion(int n, int total) {      if (n == 0) {          return total;      }      else {          return tail_recursion(n-1, total+n);      }  }    int main(void) {      int total = 0, n = 4;      tail_recursion(n, total);      return 0;  }</code></pre>    <p><strong>反汇编</strong></p>    <ul>     <li> <p>$ gcc -S tail_recursion.c -o normal_recursion.S</p> </li>     <li> <p>$ gcc -S -O2 tail_recursion.c -o tail_recursion.S gcc开启尾递归优化</p> </li>    </ul>    <p>对比反汇编代码如下(AT&T语法)</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/2d3ec63f93b5ffa4a8ae4ec91a0c1fca.png"></p>    <p>可以看到, 开启尾递归优化前, 使用call调用函数, 创建了新的调用栈(LBB0_3);</p>    <p>而开启尾递归优化后, 就没有新的调用栈生成了, 而是直接pop</p>    <p>bp指向的 _tail_recursion 函数的地址(pushq %rbp)然后返回,</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/7caba13a80a501a125dc1018f7202109.png"></p>    <p>仍旧用的是同一个调用栈!</p>    <h2><strong>存在的问题</strong></h2>    <p>虽然尾递归优化很好, 但python 不支持尾递归,递归深度超过1000时会报错</p>    <pre>  <code class="language-python">RuntimeError: maximum recursion depth exceeded</code></pre>    <h2><strong>一个牛人想出的解决办法</strong></h2>    <p>实现一个 tail_call_optimized 装饰器</p>    <pre>  <code class="language-python">#!/usr/bin/env python2.4  # This program shows off a python decorator(  # which implements tail call optimization. It  # does this by throwing an exception if it is  # it's own grandparent, and catching such  # exceptions to recall the stack.    import sys    class TailRecurseException:      def __init__(self, args, kwargs):          self.args = args          self.kwargs = kwargs    def tail_call_optimized(g):      """      This function decorates a function with tail call      optimization. It does this by throwing an exception      if it is it's own grandparent, and catching such      exceptions to fake the tail call optimization.        This function fails if the decorated      function recurses in a non-tail context.      """      def func(*args, **kwargs):          f = sys._getframe()          # 为什么是grandparent, 函数默认的第一层递归是父调用,          # 对于尾递归, 不希望产生新的函数调用(即:祖父调用),          # 所以这里抛出异常, 拿到参数, 退出被修饰函数的递归调用栈!(后面有动图分析)          if f.f_back and f.f_back.f_back \              and f.f_back.f_back.f_code == f.f_code:              # 抛出异常              raise TailRecurseException(args, kwargs)          else:              while 1:                  try:                      return g(*args, **kwargs)                  except TailRecurseException, e:                      # 捕获异常, 拿到参数, 退出被修饰函数的递归调用栈                      args = e.args                      kwargs = e.kwargs      func.__doc__ = g.__doc__      return func    @tail_call_optimized  def factorial(n, acc=1):      "calculate a factorial"      if n == 0:          return acc      return factorial(n-1, n*acc)    print factorial(10000)</code></pre>    <p>为了更清晰的展示开启尾递归优化前、后调用栈的变化和tail_call_optimized装饰器抛异常退出递归调用栈的作用, 我这里利用 <a href="/misc/goto?guid=4959727429528079231" rel="nofollow,noindex">pudb调试工具</a> 做了动图 <br/></p>    <p>开启尾递归优化前的调用栈</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/ed74449a3e936c7a2d9be5c745c1b3f8.gif"></p>    <p>开启尾递归优化后(tail_call_optimized装饰器)的调用栈</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/b22861b173fc2b8edddd92c8e43ef8be.gif"></p>    <p>通过pudb右边栏的stack, 可以很清晰的看到调用栈的变化.</p>    <p>因为尾递归没有调用栈的嵌套, 所以Python也不会报 RuntimeError: maximum recursion depth exceeded 错误了!</p>    <p>这里解释一下 sys._getframe() 函数:</p>    <pre>  <code class="language-python">sys._getframe([depth]):  Return a frame object from the call stack.  If optional integer depth is given, return the frame object that many calls below the top of the stack.  If that is deeper than the call stack, ValueEfror is raised. The default for depth is zero,  returning the frame at the top of the call stack.    即返回depth深度调用的栈帧对象.    import sys    def get_cur_info():      print sys._getframe().f_code.co_filename  # 当前文件名      print sys._getframe().f_code.co_name  # 当前函数名      print sys._getframe().f_lineno # 当前行号      print sys._getframe().f_back # 调用者的帧</code></pre>    <p> </p>    <p> </p>    <p>来自:https://segmentfault.com/a/1190000007641519</p>    <p> </p>