Skip to content

Should spread of a List use the Iterable protocol or direct iteration? #208

Closed
@leafpetersen

Description

@leafpetersen

The current proposed semantics given by the spread collections proposal specify spreading into a list using the Iterable protocol. So we have that

foo(List<int> x) => [...x];

de-sugars to (roughly):

foo(List<int> x) {
  var tmp = <int>[];
  for(var e in x) {
    tmp.add(e);
  }
  return tmp;
}

We could, in the case where we are statically spreading something of type List, use a direct loop as follows:

foo(List<int> x) {
  var len = x.length;
  var tmp = List(len);
  for(int i = 0; i < len; i++) {
     tmp[i] = x[i];
  }
  return tmp;
}

This is likely to be significantly more efficient, but is not a valid optimization that a compiler can do in the absence of extra information about the implementation type of the List.

cc @munificent @lrhn @ferhatb

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions