dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.09k stars 1.56k forks source link

optimize dart2js typed_data setRange method #11632

Open DartBot opened 11 years ago

DartBot commented 11 years ago

This issue was originally filed by brendandunc...@gmail.com


Float32List a = new Float32List(4);
for (int i = 0; i < 4; ++i) a[i] = i.toDouble();
Float32List b = new Float32List(2);
for (int i = 0; i < 2; ++i) b[i] = i.toDouble();

b.setRange(0, 1, a, 0);
b.setRange(1, 2, a, 3);

b will result in [0,3]. This seems good in the dart vm, I believe it's doing memory copies for setRange if the iterable is also a typed_data. But with dart2js, setRange results in a for-loop copy.

Can typed_data_dart2js.dart be optimized? Something like:

class Float32List ... native "Float32Array" {
  @­JSName('set')
  void _set(Float32List array, int offset) native;

  @­JSName('subarray')
  _subarray(int begin, int end);

  void setRange(int start, int end, Iterable<num> iterable,
                [int skipCount=0]) {
    if (iterable is Float32List) {
      _set(iterable._subarray(skipCount, skipCount + (end - start)),
           start);
    } else {
      IterableMixinWorkaround.setRangeList(this, start, end,
                                           iterable, skipCount);
    }
  }
}

Which would use the native Float32Array set and subarray methods to more efficiently copy a subrange than a for-loop, if the iterable is the same type.

The above doesn't quite work, as I'm not sure how to properly expose the native javascript methods...

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

What version of the product are you using? On what operating system? Dart SDK version 0.5.20.4_r24275

Please provide any additional information below.

DartBot commented 11 years ago

This comment was originally written by brendandunc...@gmail.com


Woops, typo above in my example optimization. It should be:

class Float32List ... native "Float32Array" {
  @­JSName('set')
  void _set(Float32List array, int offset) native;

  @­JSName('subarray')
  _subarray(int begin, int end) native;

  void setRange(int start, int end, Iterable<num> iterable, 
                [int skipCount=0]) {
    if (iterable is Float32List) {
      _set(iterable._subarray(skipCount, skipCount + (end - start)),
           start);
    } else {
      IterableMixinWorkaround.setRangeList(this, start, end, 
                                           iterable, skipCount);
    }
  }
}

Which does work in exposing the native javascript set and subarray methods, causing setRange to use those methods if the iterable type is also a Float32List.

anders-sandholm commented 11 years ago

Removed Type-Defect label. Added Type-Enhancement, Area-Dart2JS, Triaged labels.

DartBot commented 11 years ago

This comment was originally written by ngeoffray@google.com


Seems like a bug in the library code, where VM and dart2js don't have the same behavior. Florian, can you take a look?


Set owner to @floitschG. Removed Area-Dart2JS label. Added Area-Library label.

floitschG commented 11 years ago

Should be feasible by using the native 'set' method.


Added Ready-to-implement, Accepted labels.