What is tail call optimization?

前端 未结 10 1271
一整个雨季
一整个雨季 2020-11-21 06:36

Very simply, what is tail-call optimization?

More specifically, what are some small code snippets where it could be applied, and where not, with an explanation of wh

10条回答
  •  一向
    一向 (楼主)
    2020-11-21 07:23

    Probably the best high level description I have found for tail calls, recursive tail calls and tail call optimization is the blog post

    "What the heck is: A tail call"

    by Dan Sugalski. On tail call optimization he writes:

    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.

    And on tail recursion:

    Tail recursion happens if a function, as its last operation, returns the result of calling itself. Tail recursion is easier to deal with because rather than having to jump to the beginning of some random function somewhere, you just do a goto back to the beginning of yourself, which is a darned simple thing to do.

    So that this:

    sub foo (int a, int b) {
      if (b == 1) {
        return a;
      } else {
        return foo(a*a + a, b - 1);
      }
    

    gets quietly turned into:

    sub foo (int a, int b) {
      label:
        if (b == 1) {
          return a;
        } else {
          a = a*a + a;
          b = b - 1;
          goto label;
       }
    

    What I like about this description is how succinct and easy it is to grasp for those coming from an imperative language background (C, C++, Java)

提交回复
热议问题