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

Simple constructors get converted to verbose javascript #15070

Closed
DartBot opened this issue Nov 14, 2013 · 3 comments
Closed

Simple constructors get converted to verbose javascript #15070

DartBot opened this issue Nov 14, 2013 · 3 comments
Labels
closed-duplicate Closed in favor of an existing report web-dart2js

Comments

@DartBot
Copy link

DartBot commented Nov 14, 2013

This issue was originally filed by k.nei...@gmail.com


What steps will reproduce the problem?

When running:
dart2js --out=bugreport.js example.dart
java -jar compiler.jar --js bugreport.js --js_output_file bugreport.min.js --warning_level QUIET --language_in ECMASCRIPT5

I tried using the --minify parameter, but it was not helpful.
The closure compiler could not compress it much.

This dart code:

class BadPerson{
  String _name;
  int _age;
  BadPerson(String name, int age){
    this._name = name;
    this._age = age;
  }
  String getName() => _name;
  int getAge() => _age;
  toString() => "{BadPerson " + _name + " " + _age.toString() + "}";
}

class GoodPerson{
  final String name;
  final int age;
  const GoodPerson(this.name, this.age);
  String getName() => name;
  int getAge() => age;
  toString() => "{GoodPerson " + name + " " + age.toString() + "}";
}

var badPeople = [
  new BadPerson('Kimberly', 14),
  new BadPerson('Bertha', 22),
  new BadPerson('Roberto', 30),
  new BadPerson('Gordon', 6),
  new BadPerson('Lisa', 27),
  new BadPerson('Gaye', 8),
  new BadPerson('Marian', 11),
  new BadPerson('Julie', 5),
  new BadPerson('June', 16),
  new BadPerson('John', 6)
];

var goodPeople = [
  new GoodPerson('Barbara', 14),
  new GoodPerson('Joshua', 24),
  new GoodPerson('Bertha', 20),
  new GoodPerson('Gary', 15),
  new GoodPerson('William', 16),
  new GoodPerson('Stanley', 11),
  new GoodPerson('Kelli', 30),
  new GoodPerson('Joanna', 2),
  new GoodPerson('Sylvia', 2),
  new GoodPerson('Amber', 29),
  new GoodPerson('Charlene', 19)
];

Gets compiled to this javascript:

Isolate.$lazy($, "badPeople", "badPeople", "get$badPeople", function() {
  var t1, t2, t3, t4, t5, t6, t7, t8, t9, t10;
  t1 = new D.BadPerson(null, null);
  t1._name = "Kimberly";
  t1._age = 14;
  t2 = new D.BadPerson(null, null);
  t2._name = "Bertha";
  t2._age = 22;
  t3 = new D.BadPerson(null, null);
  t3._name = "Roberto";
  t3._age = 30;
  t4 = new D.BadPerson(null, null);
  t4._name = "Gordon";
  t4._age = 6;
  t5 = new D.BadPerson(null, null);
  t5._name = "Lisa";
  t5._age = 27;
  t6 = new D.BadPerson(null, null);
  t6._name = "Gaye";
  t6._age = 8;
  t7 = new D.BadPerson(null, null);
  t7._name = "Marian";
  t7._age = 11;
  t8 = new D.BadPerson(null, null);
  t8._name = "Julie";
  t8._age = 5;
  t9 = new D.BadPerson(null, null);
  t9._name = "June";
  t9._age = 16;
  t10 = new D.BadPerson(null, null);
  t10._name = "John";
  t10._age = 6;
  return [t1, t2, t3, t4, t5, t6, t7, t8, t9, t10];
});
Isolate.$lazy($, "goodPeople", "goodPeople", "get$goodPeople", function() {
  return [new D.GoodPerson("Barbara", 14), new D.GoodPerson("Joshua", 24), new D.GoodPerson("Bertha", 20), new D.GoodPerson("Gary", 15), new D.GoodPerson("William", 16), new D.GoodPerson("Stanley", 11), new D.GoodPerson("Kelli", 30), new D.GoodPerson("Joanna", 2), new D.GoodPerson("Sylvia", 2), new D.GoodPerson("Amber", 29), new D.GoodPerson("Charlene", 19)];
});

What is the expected output? What do you see instead?

The BadPerson class is just as simple as the GoodPerson class.
I would expect them to convert to somewhat similar javascript.
This can make a huge filesize difference when large arrays of Person objects are constructed.
Simply using final and const changed my .js filesize from 3 MB to 1 MB.

What version of the product are you using? On what operating system?
Dart 1.0.0.3_r30188 on Windows 7, 64bit

Please provide any additional information below.

In attachment you'll find a simple example which clearly shows compilation to javascript generates much larger files while there doesn't seem to be any need for it.


Attachment:
bugreport.7z (2.60 MB)

@sethladd
Copy link
Contributor

Added Area-Dart2JS, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Nov 16, 2013

This comment was originally written by ngeoffray@google.com


Set owner to ngeoffray@google.com.
Added Accepted label.

@peter-ahe-google
Copy link
Contributor

I believe this is a duplicate of 9757 (which describes the problem of inlining constructors (and other things) when this doesn't pay off).

But I think k.neirynck can get much better code without waiting for 9757 to be fixed. Simply change:

  BadPerson(String name, int age){
    this._name = name;
    this._age = age;
  }

to:

  BadPerson(String name, int age)
      : this._name = name,
        this._age = age;

or even shorter:

  BadPerson(this._name, this._age);

This generates:

Isolate.$lazy($, "badPeople", "badPeople", "get$badPeople", function() {
  return [new V.BadPerson("Kimberly", 14), new V.BadPerson("Bertha", 22), new V.BadPerson("Roberto", 30), new V.BadPerson("Gordon", 6), new V.BadPerson("Lisa", 27), new V.BadPerson("Gaye", 8), new V.BadPerson("Marian", 11), new V.BadPerson("Julie", 5), new V.BadPerson("June", 16), new V.BadPerson("John", 6)];
});
Isolate.$lazy($, "goodPeople", "goodPeople", "get$goodPeople", function() {
  return [new V.GoodPerson("Barbara", 14), new V.GoodPerson("Joshua", 24), new V.GoodPerson("Bertha", 20), new V.GoodPerson("Gary", 15), new V.GoodPerson("William", 16), new V.GoodPerson("Stanley", 11), new V.GoodPerson("Kelli", 30), new V.GoodPerson("Joanna", 2), new V.GoodPerson("Sylvia", 2), new V.GoodPerson("Amber", 29), new V.GoodPerson("Charlene", 19)];
});


Added Duplicate label.
Marked as being merged into #9757.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-duplicate Closed in favor of an existing report web-dart2js
Projects
None yet
Development

No branches or pull requests

4 participants