View on GitHub

continuation.js

A module for tail call optimization by Continuation Passing Style (CPS) transformation with trampoline technique for Node.js

Download this project as a .zip file Download this project as a tar.gz file

continuation.js

A module for tail call optimization by Continuation Passing Style (CPS) transformation with trampoline technique for Node.js

JavaScript is a nice programming language, but compared to Scheme, it doesn't handle tail calls properly. Node.js is often used with callback functions, which tend to be tail calls (but not necessarily recursions) consuming call stacks.

This module allows to transform native JavaScript code into CPS code in a best effort manner. It utilizes so-called trampoline technique to avoid a stack overflow error. Transforming all functions into CPS is not very easy (and sometimes not very efficient), hence it has a fallback mechanism, that is, only supported function style is transformed into CPS and other functions are called in an original style. Because of the fallback mechanism, mixing CPS code and non-CPS code is possible.

Comparison

Here is the table showing modules that support tail call optimization.

NAME continuation.js Brushtail tailrec.js thunk.js tail-call tailopt.js
Tail call optimization Yes Yes Yes Yes Yes Yes
Mutual recursion Yes No No Yes No Yes
Native JavaScript Yes Yes No No More or less More or less
require() integration Yes No No No No No

How to use

GitHub

% git clone https://github.com/dai-shi/continuation.js.git
% cd continuation.js
% ./bin/continuation-compile sample/fact.js > cps_fact.js

NPM

% npm install continuation.js

and add the following:

require('continuation.js').enable_on_require();

which transforms all following .js files by require.

Examples

Simple factorial function:

% cat sample/fact.js
function fact(x) {
  function fact_tail(x, r) {
    if (x === 0) {
      return r;
    } else {
      return fact_tail(x - 1, x * r);
    }
  }
  return fact_tail(x, 1);
}

exports.fact = fact;

% node -e "console.log(require('./sample/fact.js').fact(100000))"

.../continuation.js/sample/fact.js:2
  function fact_tail(x, r) {
                        ^
RangeError: Maximum call stack size exceeded

% node -e "require('./lib/continuation.js').enable_on_require();console.log(require('./sample/fact.js').fact(100000))"
Infinity

Mutual recursion example:

% cat sample/mutual.js
function isEven(x) {
  if (x === 0) {
    return true;
  } else {
    return isOdd(x - 1);
  }
}

function isOdd(x) {
  if (x === 0) {
    return false;
  } else {
    return isEven(x - 1);
  }
}

exports.isEven = isEven;
exports.isOdd = isOdd;

% node -e "console.log(require('./sample/mutual.js').isOdd(1234567))"

.../sample/mutual.js:1
tion isEven(x) {
       ^
RangeError: Maximum call stack size exceeded
% node -e "require('./lib/continuation.js').enable_on_require();console.log(require('./sample/mutual.js').isOdd(1234567))"
true

How it works

Benchmark results

The following is the results of Octane benchmark suites (except for one).

Suite name Original CPS transformed
Richards.Richards 364 ops/sec 28.14 ops/sec
DeltaBlue.DeltaBlue 181 ops/sec 23.87 ops/sec
Crypto.Encrypt 172 ops/sec 160 ops/sec
Crypto.Decrypt 9.08 ops/sec 8.52 ops/sec
RayTrace.RayTrace 18.54 ops/sec 5.21 ops/sec
EarleyBoyer.Earley 280 ops/sec 71.44 ops/sec
EarleyBoyer.Boyer 18.86 ops/sec 4.53 ops/sec
RegExp.RegExp 7.11 ops/sec 7.13 ops/sec
Splay.Splay 121 ops/sec 110 ops/sec
NavierStokes.NavierStokes 3.61 ops/sec 2.89 ops/sec
PdfJS.PdfJS 2.85 ops/sec 2.83 ops/sec
Gameboy.Gameboy 1.08 ops/sec 0.59 ops/sec
CodeLoad.CodeLoadClosure 382 ops/sec 368 ops/sec
CodeLoad.CodeLoadJQuery 10.01 ops/sec 11.12 ops/sec
Box2D.Box2D 2.53 ops/sec 2.55 ops/sec

Since trampoline is costly, performance drops in most suites especially basic ones. Whereas in relatively complex suites, there are some cases when performance is comparable.

TODOs