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

List.from is fixed length, but loop through list still checks for ioore #10489

Closed
sethladd opened this issue May 7, 2013 · 17 comments
Closed

Comments

@sethladd
Copy link
Contributor

sethladd commented May 7, 2013

Consider this code:

List<String> fruits = new List.from(['apples', 'oranges']);

void main() {
  for (int i = 0; i < fruits.length; i++) {
    print(fruits[i]);
  }
}

this compiles to the following JS code:

$.main = function() {
  var i, t1;
  for (i = 0; i < $.get$fruits().length; ++i) {
    t1 = $.get$fruits();
    if (i >= t1.length)
      throw $.ioore(i);
    $.Primitives_printString($.toString$0(t1[i]));
  }
};

I would expect that the constructor for List.from() returns a fixed length list. Thus, dart2js could inline the list length in a for loop and avoid the ioore check.

@sethladd
Copy link
Contributor Author

sethladd commented May 7, 2013

FWIW, if I change fruits to a const list, the JS code looks like:

$.main = function() {
  for (var i = 0; i < 2; ++i)
    $.Primitives_printString($.toString$0($.List_apples_oranges[i]));
};

@DartBot
Copy link

DartBot commented May 7, 2013

This comment was originally written by ngeoffray@google.com


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

@lrhn
Copy link
Member

lrhn commented May 7, 2013

Actually, List.from returns a growable list by default. You need to pass the "growable:false" named parameter to get a fixed list.

In this case, it might be able to be specialized because the argument is a literal (growable list, but obviously not modified before being passeed).

@sethladd
Copy link
Contributor Author

sethladd commented May 7, 2013

Thanks Lasse, I misread this in the API docs:

"The returned list is growable if growable is true, otherwise it's a fixed length list."

I read that as "unless you set growable to true, it's fixed"

@sethladd
Copy link
Contributor Author

sethladd commented May 7, 2013

So I changed the code to:

NBodySystem bodies = new NBodySystem(new List.from([Body.Sun(),
                                      Body.Jupiter(),
                                      Body.Saturn(),
                                      Body.Uranus(),
                                      Body.Neptune()], growable: false));

but the generated JS is still:

    for (dx = null, dy = null, dz = null, distance = null, mag = null, i = 0; i < size; i = j) {
      if (i >= t1.length)
        throw $.ioore(i);

@kasperl
Copy link

kasperl commented May 23, 2013

Added this to the Later milestone.

@kasperl
Copy link

kasperl commented May 23, 2013

Added TriageForM5 label.

@kasperl
Copy link

kasperl commented May 28, 2013

Removed TriageForM5 label.

@DartBot
Copy link

DartBot commented Dec 19, 2013

This comment was originally written by ngeoffray@google.com


Added Triaged label.

@DartBot
Copy link

DartBot commented Dec 19, 2013

This comment was originally written by ngeoffray@google.com


Set owner to @floitschG.

@DartBot
Copy link

DartBot commented Feb 2, 2014

This comment was originally written by @bp74


I tried to optimize some of the JavaScript code generated by dart2js and found the same problem with the ".toList(growable: false)" method. The JavaScript code is smaller and runs faster if i create a "new List(size)" and copy all elements to the new list - which is a non growable list recognized by the compiler.

So it would be great if the compiler would optimize for List methods returning non growable lists.

@floitschG
Copy link
Contributor

Removed the owner.
Added Optimization label.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@sethladd
Copy link
Contributor Author

Friendly ping. Did we address this recently? https://code.google.com/p/dart/issues/detail?id=20012


cc @floitschG.

@floitschG
Copy link
Contributor

No. These issues are unrelated.


cc @herhut-ggl.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@karlklose
Copy link
Contributor

Verified fix in 79a8654

@kevmoo kevmoo removed the triaged label Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants