Do Immutable.js or Lazy.js perform short-cut fusion? Do Immutable.js or Lazy.js perform short-cut fusion? javascript javascript

Do Immutable.js or Lazy.js perform short-cut fusion?


I'm the author of Immutable.js (and a fan of Lazy.js).

Does Lazy.js and Immutable.js's Seq use short-cut fusion? No, not exactly. But they do remove intermediate representation of operation results.

Short-cut fusion is a code compilation/transpilation technique. Your example is a good one:

var a = [1,2,3,4,5].map(square).map(increment);

Transpiled:

var a = [1,2,3,4,5].map(compose(square, increment));

Lazy.js and Immutable.js are not transpilers and will not re-write code. They are runtime libraries. So instead of short-cut fusion (a compiler technique) they use iterable composition (a runtime technique).

You answer this in your TLDR:

As far as I know they get rid of intermediate arrays by emulating lazy evaluation via sequence objects (i.e. iterators). However, I believe that these iterators are chained: one iterator feeding its values lazily to the next. They are not merged into a single iterator. Hence they do not "eliminate intermediate representations". They only transform arrays into constant space sequence objects.

That is exactly right.

Let's unpack:

Arrays store intermediate results when chaining:

var a = [1,2,3,4,5];var b = a.map(square); // b: [1,4,6,8,10] created in O(n)var c = b.map(increment); // c: [2,5,7,9,11] created in O(n)

Short-cut fusion transpilation creates intermediate functions:

var a = [1,2,3,4,5];var f = compose(square, increment); // f: Function created in O(1)var c = a.map(f); // c: [2,5,7,9,11] created in O(n)

Iterable composition creates intermediate iterables:

var a = [1,2,3,4,5];var i = lazyMap(a, square); // i: Iterable created in O(1)var j = lazyMap(i, increment); // j: Iterable created in O(1)var c = Array.from(j); // c: [2,5,7,9,11] created in O(n)

Note that using iterable composition, we have not created a store of intermediate results. When these libraries say they do not create intermediate representations - what they mean is exactly what is described in this example. No data structure is created holding the values [1,4,6,8,10].

However, of course some intermediate representation is made. Each "lazy" operation must return something. They return an iterable. Creating these is extremely cheap and not related to the size of the data being operated on. Note that in short-cut fusion transpilation, an intermediate representation is also made. The result of compose is a new function. Functional composition (hand-written or the result of a short-cut fusion compiler) is very related to Iterable composition.

The goal of removing intermediate representations is performance, especially regarding memory. Iterable composition is a powerful way to implement this and does not require the overhead that parsing and rewriting code of an optimizing compiler which would be out of place in a runtime library.


Appx:

This is what a simple implementation of lazyMap might look like:

function lazyMap(iterable, mapper) {  return {    "@@iterator": function() {      var iterator = iterable["@@iterator"]();      return {        next: function() {          var step = iterator.next();          return step.done ? step : { done: false, value: mapper(step.value) }        }      };    }  };}