Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Strange optimization function optimization #2387

Open
NTillmann opened this issue Aug 3, 2018 · 0 comments
Open

Strange optimization function optimization #2387

NTillmann opened this issue Aug 3, 2018 · 0 comments
Labels
bug optimized functions Issues around optimizing not global code, but additional functions

Comments

@NTillmann
Copy link
Contributor

We have the following test case.

(function() {
    function f(g) {
        let x = 23;
        function incrementX() {
            x = x + 42;
        }
        global.__optimize && __optimize(incrementX);
        g(incrementX);
        return x;
    }
    global.__optimize && __optimize(f);
    global.inspect = function() {
        return f(g => g());
    };
})();

which Prepacks to

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

  var _2 = function (g) {
    var __leaked_0;

    var _6 = function () {
      var _$2 = __leaked_0;

      var _$3 = _$2 + 42;

      __leaked_0 = _$3;
      return void 0;
    };

    __leaked_0 = 23;

    var _$0 = g(_6);

    var _$1 = __leaked_0;
    return _$1;
  };

  var _1 = function () {
    return _2(g => g());
  };

  _$6.inspect = _1;
}).call(this);

Okay. But if you move up the definition of x as follows...

(function() {
    let x = 23;
    function f(g) {
        function incrementX() {
            x = x + 42;
        }
        global.__optimize && __optimize(incrementX);
        g(incrementX);
        return x;
    }
    global.__optimize && __optimize(f);
    global.inspect = function() {
        return f(g => g());
    };
})();

then we get

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

  var _2 = function (g) {
    var _5 = function () {
      return void 0;
    };

    var _$0 = g(_5);

    return 23;
  };

  var _1 = function () {
    return _2(g => g());
  };

  _$2.inspect = _1;
}).call(this);

So in this case, incoming x is not leaking, but treated as a constant, as least as far as the return value of f is concerned. But somehow incrementX treats x as undefined? That seems wrong.

@NTillmann NTillmann added bug optimized functions Issues around optimizing not global code, but additional functions labels Aug 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug optimized functions Issues around optimizing not global code, but additional functions
Projects
None yet
Development

No branches or pull requests

1 participant