In this blog post i will try to explain the basic concept of recursion and then show why recursion can be so inefficient and how to optimize it using Call Tail Optimization!
Normal Recursion, A Factorial Example
Most of us tech nerds have already dealt with the good ‘ol recursion, let’s refresh our understanding using the iconic factorial program.
$$0! = 1$$
$$n! = n (n-1)!$$
Python Implementation:
|
|
But python is just too mainstream and overrated, let’s use Lisp!
|
|
ain’t Scheme just too beautiful?
Now, let’s inspect the program behavior!
Tracing The Recursion
let’s say we want to execute (fact 5)
which supposedly evaluates to 120.
here is the trace of the factorial operation:
|
|
here’s the pythonic version for those who are struggling with lisp (it’s way easier believe me)
|
|
Did you figure out the flaw of our simple recursion implementation yet?
It’s pretty simple, the way we expand the factorial on each iteration so that it grows and keeps growing until we fully expand it is just so inefficient and wastes memory space.
The waste of memory space comes from the fact that each call of (fact x)
will allocate a new stack frame to store its data, so we have used around 6 stack frames for this simple calculation, allocating and popping stack frames is a relatively intensive operation for the CPU.
The source of this flaw is the multiplication that we are performing with our recurred call.
So Tail Call Optimization or Tail Recursion are just fancy names for a simple rule we need to follow in order to optimize our recursive functions.
“The recurred call shouldn’t be combined with other operations”
i.e: we need to move the multiplication operator out of the recurred call in the factorial function
Using Tail Recursion
let’s rewrite the factorial function in Tail Recursion:
|
|
Pythonic version:
|
|
what we did in that snippet above is pretty simple, we just split the work across two functions, the first function (fact-tail x accum)
will iterate and the second function (fact x)
will call the first function and returns the value of each iteration (we have also moved the multiplication operation to it’s own variable) so we basically have no extra operations going on, in fact calling (fact 0)
is now the same as calling (fact 10000)
in terms of memory size.
let’s step through each iteration and see for ourselves how great is Tail Recursion:
|
|
Pythonic Version:
|
|
is this even recursion anymore, that’s just fancy iteration!
we have used recursion in such a way that we store all the data to perform our evalutaion in each individual reccured call!
All Hail Tail Call Optimization!
More Tail Recursion!
here is one more example with the infamous fibonacci function in both normal Recursion and then Tail Recursion:
(you try to implement it in python this time :p)
Normal Recursion
|
|
Tail Recursion
|
|
All Hail The Tail Recursion