Prepack: Initializing code inside functions are not eliminated

Created on 4 May 2017  Β·  6Comments  Β·  Source: facebook/prepack

Not sure, if this is an issue or is it meant to be like this. I was just trying out prepack in the REPL and tried this code.

(function() {
  let opr = {
    'sum': (x, y) => x + y,
    'mul': (x, y) => x * y,
    'div': (x, y) => x / y,
    'dif': (x, y) => x - y
  };
  
  let operands = [
    [1, 2],
    [2, 3],
    [3, 4],
    [4, 5]
  ];
    
  window.result = Object.keys(opr).map((key, index) => {
    return opr[key](...operands[index]);
  });
})();

This got compiled into:

result = [3, 6, 0.75, -1];

Which is awesome. But the thing I didn't understand is, as I converted the window.result global variable into a function without changing any other part of the code the initialized code wasn't eliminated at all.

Consider this code, all I changed was from window.result = to window.result = () =>:

(function() {
  let opr = {
    'sum': (x, y) => x + y,
    'mul': (x, y) => x * y,
    'div': (x, y) => x / y,
    'dif': (x, y) => x - y
  };
  
  let operands = [
    [1, 2],
    [2, 3],
    [3, 4],
    [4, 5]
  ];
    
  window.result = () => Object.keys(opr).map((key, index) => {
    return opr[key](...operands[index]);
  });
})();

Now this gets compiled to:

(function () {
  var _$0 = this;

  function _0() {
    return _$0.Object.keys(_1).map((key, index) => {
      return _1[key](..._6[index]);
    });
  }

  function _2(x, y) {
    return x + y;
  }

  function _3(x, y) {
    return x * y;
  }

  function _4(x, y) {
    return x / y;
  }

  function _5(x, y) {
    return x - y;
  }

  var _1 = {
    sum: _2,
    mul: _3,
    div: _4,
    dif: _5
  };
  var _6 = [[1, 2], [2, 3], [3, 4], [4, 5]];
  result = _0;
}).call(this);

I was expecting the output to be something like this:

function result() {
    return [3, 6, 0.75, -1];
}

But it didn't eliminate the computations at the compile time. IMO, these initializing codes in the second example should have been eliminated too, shouldn't it?

question

Most helpful comment

Prepack does not yet evaluate most functions that are not called on the initialization path (with a special exception for a specific require implementation). We have a plan to do this in the future #558

All 6 comments

Prepack does not yet evaluate most functions that are not called on the initialization path (with a special exception for a specific require implementation). We have a plan to do this in the future #558

Nice. BTW, do we have a roadmap or milestone set about what features are going to be implemented for the first stable release eg: v1.x.x and when that is expected to be?

@NTillmann may have a better idea, but we should have a stable product by the end of the year. It is unclear at this point what exactly it will include, but we would at least want to have complete ES5 conformance and mostly bug-free implementation. At that point we should have a complete model for React Native and the ability to easily drop it in the React Native toolchain, but we're not sure what support we'll have for the web then.

Great. Anyway, I'm looking forward to using this in our regular dev workflow as soon as this is stable and production-ready. For now, I think a milestone/roadmap with expected features for the next stable version in the website/github would be helpful.

The issue is tracked in #558. We'll post a roadmap soon.

Great. Thanks :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jtenner picture jtenner  Β·  5Comments

NTillmann picture NTillmann  Β·  3Comments

simon-yxl picture simon-yxl  Β·  3Comments

maxime1992 picture maxime1992  Β·  5Comments

aligoren picture aligoren  Β·  6Comments