Skip to content

Improved temp variable inlining. - #221

Closed
shinew wants to merge 2 commits into
babel:masterfrom
shinew:inline-impure
Closed

shinew wants to merge 2 commits into
babel:masterfrom
shinew:inline-impure

Conversation

@shinew

@shinew shinew commented Oct 25, 2016

Copy link
Copy Markdown
Contributor

Now inline impure rvals assigned to temp lvals if the lval is used in a "simple" way immediately after the assignment.

Shine Wang added 2 commits October 25, 2016 12:23
Now inline impure rvals assigned to temp lvals if the lval
is used in a "simple" way immediately after the assignment.
@kangax

kangax commented Oct 25, 2016

Copy link
Copy Markdown
Member

There's couple issues here:

  1. It seems to miss var (var x = foo(); return x; doesn't get inlined)
  2. It does crude analysis which is easy to mess up: var x = foo(); { }; return x; is not inlined. Neither is (more realistic) var x = foo(); if (smth) return x;. Neither is var x = foo(); bar(x, y) (more than 1 args). And so on.

The way I see it, in an ideal implementation we should traverse a tree from node A (source) to node B (destination) checking if everything in between is pure.

Either that or we ditch this whole idea (due to complexity) and introduce something like "unsafeInlining" flag.

@kangax

kangax commented Nov 3, 2016

Copy link
Copy Markdown
Member

@boopathi @hzoo what do you think we should do here? Try harder or be simpler?

@kangax kangax added discussion Tag: New Feature Pull Request adding a new feature labels Nov 10, 2016
@boopathi

Copy link
Copy Markdown
Member

I'm not sure. There are too many cases where you shouldn't change at all. If it's not hard to detect if we maintain the order of impure calls, we can try harder.

assumption: all function calls cause side-effects and order matters.

Number of impure expressions

If there are more than 1 impure expression, then optimize if and ONLY if the order is maintained - between and including the binding declaration and the last usage of the variable.

function foo() {
  let x = Foo(),
      y = Bar();
  return x;
}
function bar() {
  let x = Foo();
  return Bar(x); // order is maintained. So can replace.
}
function foo1() {
  let a = Foo(), b = Bar();
  return b + a; // order changes
}
function foo2() {
  let a = Foo();
  return Bar(Baz() + a); // order changes
}

Async Callbacks

function foo() {
  var x = foo();
  return new Promise((resolve, reject) => resolve(x));
}
function bar() {
  var x = foo();
  callAsync(() => x);
}

Sync callbacks and Loops

function foo() {
  let x = foo();
  return items.map(() => x);
}
function bar() {
  let x = foo();
  for (;;) {
    if (a) bar(x);
  }
}

Side effecty getters and setters

  • pure_getters as an option - where we need not count getters as impure expression
function foo() {
  let x = bar.foo();
  bar.baz(x);
}
function foo2() {
  let x = { foo: foo(), bar: bar() };
  module.exports = x; // order doesn't change. can replace
}
function foo3() {
  let x = foo();
  module.foo.exports = x; // order changes because of getter.
}

Chain replacements

If we are doing this, we should chain them

function baz() {
  let x = Foo(),
      y = Bar(x),
      z = Baz(y);
  return z; // order is maintained
}

Destructuring

Right now this is disabled in DCE. May be we can use the pure_getters option here as well

function foo() {
  let {x} = a();
  return x; // can be optimized to a().x
}
// but should bail when
function bar() {
  let {x, y} = a();
  return x;
}

Probably array destructuring will be shorter with a temp variable than using the iterator protocol ?

@kangax

kangax commented Nov 16, 2016

Copy link
Copy Markdown
Member

Why should it bail on:

function bar() {
  let {x, y} = a();
  return x;
}

If we ignore a().x being a potential getter, then we don't care about unreferenced y and could still reduce it to return a().x, no?

@boopathi

boopathi commented Nov 16, 2016 •

Copy link
Copy Markdown
Member
let {x, y} = a(); 

x and y getters are called here. If we remove y, y getter is not called. So we should use the option pure_getters here too

@boopathi

boopathi commented Nov 7, 2017

Copy link
Copy Markdown
Member

Closing this as it's been inactive. Please feel free to open a new PR or leave a comment asking to reopen this PR. Thanks!

@boopathi boopathi closed this Nov 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

discussion Tag: New Feature Pull Request adding a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants