moovweb / tritium

Tritium is a magical document modifying language. It's JavaScript-like and simple to learn. Think XSLT without the nightmare. It was designed by Hampton Catlin (@hcatlin), and has been heavily influenced by Aaron Leung (@akhleung).
http://tritium.io
Mozilla Public License 2.0
33 stars 7 forks source link

Implement tail call elimination #20

Closed akhleung closed 6 years ago

akhleung commented 12 years ago

(Would take too long to explain here, so look it up if you don't know what it is.)

It's relatively easy for the parser to identify function calls in tail position (i.e., a tail call) and mark them as such. It would then be the interpreter's job to re-use the current stack frame (rather than allocate a new one) whenever it encounters a tail call.

In my opinion, it's worth doing because (a) it's not really difficult, and (b) we're always using things like:

$("foo") {
  foo()
  $("bar") {
    bar()
    $("hux") {
      hux()
    }
  }
}

With tail call optimization, none of the $'s should allocate any new stack frames (assuming that $'s internal call to yield() is also in tail position, of course).