项目作者: dai-shi

项目描述 :
通过用于Node.js的trampoline技术的Continuation Passing Style(CPS)变换进行尾调用优化的模块
高级语言: JavaScript
项目地址: git://github.com/dai-shi/continuation.js.git
创建时间: 2012-12-11T09:23:10Z
项目社区:https://github.com/dai-shi/continuation.js

开源协议:

下载


continuation.js

npm version

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.















































NAMEcontinuation.jsBrushtailtailrec.jsthunk.jstail-calltailopt.js
Tail call optimizationYesYesYesYesYesYes
Mutual recursionYesNoNoYesNoYes
Native JavaScriptYesYesNoNoMore or lessMore or less
require() integrationYesNoNoNoNoNo

How to use

GitHub

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

NPM

  1. $ npm install continuation.js

and add the following:

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

which transforms all following .js files by require.

Examples

Simple factorial function:

  1. $ cat sample/fact.js
  2. function fact(x) {
  3. function fact_tail(x, r) {
  4. if (x === 0) {
  5. return r;
  6. } else {
  7. return fact_tail(x - 1, x * r);
  8. }
  9. }
  10. return fact_tail(x, 1);
  11. }
  12. exports.fact = fact;
  13. $ node -e "console.log(require('./sample/fact.js').fact(100000))"
  14. .../continuation.js/sample/fact.js:2
  15. function fact_tail(x, r) {
  16. ^
  17. RangeError: Maximum call stack size exceeded
  18. $ node -e "require('./lib/continuation.js').enable_on_require();console.log(require('./sample/fact.js').fact(100000))"
  19. Infinity

Mutual recursion example:

  1. $ cat sample/mutual.js
  2. function isEven(x) {
  3. if (x === 0) {
  4. return true;
  5. } else {
  6. return isOdd(x - 1);
  7. }
  8. }
  9. function isOdd(x) {
  10. if (x === 0) {
  11. return false;
  12. } else {
  13. return isEven(x - 1);
  14. }
  15. }
  16. exports.isEven = isEven;
  17. exports.isOdd = isOdd;
  18. $ node -e "console.log(require('./sample/mutual.js').isOdd(1234567))"
  19. .../sample/mutual.js:1
  20. tion isEven(x) {
  21. ^
  22. RangeError: Maximum call stack size exceeded
  23. $ node -e "require('./lib/continuation.js').enable_on_require();console.log(require('./sample/mutual.js').isOdd(1234567))"
  24. true

How it works

  • 4 classes are defined in the global scope.
    • CpsFunction
    • CpsContinuation
    • CpsResult
    • CpsRun
  • CPS enabled functions have the CpsEnabled=true property.
  • Traversing AST to transform into CPS in a best effort manner.
  • Keeping original code so that non-CPS is always possible.

Benchmark results

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




















































































Suite nameOriginalCPS transformed
Richards.Richards364 ops/sec28.14 ops/sec
DeltaBlue.DeltaBlue181 ops/sec23.87 ops/sec
Crypto.Encrypt172 ops/sec160 ops/sec
Crypto.Decrypt9.08 ops/sec8.52 ops/sec
RayTrace.RayTrace18.54 ops/sec5.21 ops/sec
EarleyBoyer.Earley280 ops/sec71.44 ops/sec
EarleyBoyer.Boyer18.86 ops/sec4.53 ops/sec
RegExp.RegExp7.11 ops/sec7.13 ops/sec
Splay.Splay121 ops/sec110 ops/sec
NavierStokes.NavierStokes3.61 ops/sec2.89 ops/sec
PdfJS.PdfJS2.85 ops/sec2.83 ops/sec
Gameboy.Gameboy1.08 ops/sec0.59 ops/sec
CodeLoad.CodeLoadClosure382 ops/sec368 ops/sec
CodeLoad.CodeLoadJQuery10.01 ops/sec11.12 ops/sec
Box2D.Box2D2.53 ops/sec2.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

  • Work with try…catch and throw.
  • Transform simple non-tail recursive calls into CPS.