Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unoptimized destructuring compilation #11779

Open
holiber opened this issue Oct 21, 2016 · 6 comments
Open

Unoptimized destructuring compilation #11779

holiber opened this issue Oct 21, 2016 · 6 comments
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@holiber
Copy link

holiber commented Oct 21, 2016

TypeScript Version: 2.0

Code

// source ts code
let i = 1000;
while (i--) {
  let [a, b, c] = [1, 2, 3];
}

Expected behavior:

// compiled by babel
var i = 1000;
while (i--) {
  var a = 1;
  var b = 2;
  var c = 3;
}

Actual behavior:

// compiled by tsc
var i = 1000;
while (i--) {
    var _a = [1, 2, 3], a = _a[0], b = _a[1], c = _a[2];
}

On each iteration tsc creates new unnecessary array _a.

@holiber holiber changed the title Not optimized destructuring compilation Unoptimized destructuring compilation Oct 21, 2016
@HerringtonDarkholme
Copy link
Contributor

I would rather say this adheres to spec.
Just consider code like this

var a = {};
var b = a;
[a.x, a.y, a.x] = [1, (a = {}), 3];

@holiber
Copy link
Author

holiber commented Oct 21, 2016

@HerringtonDarkholme, I agree that in your example it's impossible to compile code without allocation of new array. But if we look at babeljs compiler we can found what it generate optimized code if it's possible

// source

function possibleToOptimizeThisFunction() {
  let i = 1000;
  while (i--) {
    let [a, b, c] = [1, 2, 3];
  }
}

function impossibleToOptimizeThisFunction() {
  var a = {};
  var b = a;
  [a.x, a.y, a.x] = [1, (a = {}), 3];
}
// compiled by babel

function possibleToOptimizeThisFunction() {
  var i = 1000;
  while (i--) {
    var a = 1;
    var b = 2;
    var c = 3;
  }
}

function impossibleToOptimizeThisFunction() {
  var i = 1000;
  while (i--) {
    var a = {};
    var b = a;
    var _ref = [1, a = {}, 3];
    a.x = _ref[0];
    a.y = _ref[1];
    a.x = _ref[2];
  }
}

https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-2&code=function%20example1()%20%7B%0A%20%20let%20i%20%3D%201000%3B%0A%20%20while%20(i--)%20%7B%0A%20%20%20%20let%20%5Ba%2C%20b%2C%20c%5D%20%3D%20%5B1%2C%202%2C%203%5D%3B%0A%20%20%7D%0A%7D%0A%0Afunction%20example2()%20%7B%0A%20%20let%20i%20%3D%201000%3B%0A%20%20while%20(i--)%20%7B%0A%20%20%20%20var%20a%20%3D%20%7B%7D%3B%0A%20%20%20%20var%20b%20%3D%20a%3B%0A%20%20%20%20%5Ba.x%2C%20a.y%2C%20a.x%5D%20%3D%20%5B1%2C%20(a%20%3D%20%7B%7D)%2C%203%5D%3B%0A%20%20%7D%0A%7D

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Oct 21, 2016
@RyanCavanaugh
Copy link
Member

Idly curious why you're writing the intializers this way? Why not just var a = 1, b = 2, c = 3 ?

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Oct 22, 2016

I like how the output always matches the syntax being transformed, it is nicely predictable. Also, if this were added, I would want to disable it during development. I would only enable it when creating a production build because there is likely to be a performance penalty for these kinds of heuristic optimizations during transpilation. It may be small, but I would want to disable it.

Unfortunately, just the notion of toggleable optimizations would introduce a lot of additional, and in my mind unnecessary complexity into the language. I real don't like the idea of having TS dev and TS prod modes as these are in the domain of other tools.

You could argue that I am being hyperbolic by even hinting at this but I can imagine the issues requesting potential optimization x, or y, which breaks z that would crop up overnight.

@holiber
Copy link
Author

holiber commented Oct 24, 2016

@RyanCavanaugh, it's not problem to use var a = 1, b = 2, c = 3. But I migrating legacy project from es6 babel to typescript and I detected some losing of performance. It's hard to detect all places with such code. But it is also possible that problem not only in this issue.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Nov 15, 2016
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Nov 15, 2016
@RyanCavanaugh
Copy link
Member

Accepting PRs to emit the optimized code in the case where the initializer is an array literal of sufficient length

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants