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.18k stars 1.57k forks source link

Please implement native multidimensional arrays in Dart. #21790

Open DartBot opened 9 years ago

DartBot commented 9 years ago

This issue was originally filed by @Emasoft


We need true, typed, native multidimensional arrays in Dart. Not arrays of arrays, but real multidimensional arrays and slices as part of the language.

The arguments for language level multidimensional arrays are four:

   1) INTEROPERABILITY - Multidimensional arrays could be implemented in Dart as reusable types in       external libraries, but multiple libraries with different array representations always result in       a huge headache when the same program pulls in multiple packages.       Conversion code has to be written and called. This is historically a headache       in language who lacks native multidimensional arrays ( like Python, C, and C++ )       but not in FORTRAN or Matlab, where multidimensional arrays are       first class citizens. It's easy to be compatible if there's one standard way to       do it, built into the language.       If Dart would not implement multidimensional arrays natively it will be headed toward       a scenario with diverse set of representations from different external       libraries, each of which represents matrices in its own way. Code       built on top of those libraries will not interoperate properly.           2) SPEED - Without native support for multidimensional arrays, a language must        emulate them with a inefficient syntax that causes loss of performances.        For example in Java the arbitrary shape of arrays and the fact that the language does not support        arrays of rank greater than one leads to the implementation of multidimensional arrays        as arrays-of-arrays, requiring multiple dereferencing and address calculations during        element accesses. This compares very unfavorably to languages featuring true        multidimensional arrays, where the offset for each element can be calculated statically        and only a single memory access needs to be executed.        The most obvious problem with arrays-of-arrays is that to allocate a d-dimensional array-of-arrays        with O(n) elements per each element, you need to store O(n^{d-1}) extra bytes worth of memory        in pointers and intermediate arrays. Additionally, accessing an element in an array-of-arrays        requires O(d) dependent memory dereferences. These operations can’t be pipelined and are        terrible from a cache performance perspective. As a result, the performance of an algorithm        requiring multidimensional arrays implemented as arrays-of-arrays can be as low as one percent        of the same implemented in typed multidimensional arrays. See i.e. those benchmarks:
       http://0fps.net/2013/05/22/implementing-multidimensional-arrays-in-javascript/         3) BETTER LOOP ITERATIONS - With native multidimensional arrays all the subscript checking          for FOR loops can be lifted out of inner loops. Performance can reach or exceed the levels of          languages that don't have subscript checking.

     4) USEFUL IN GAMES - In games having a square data model made up of rows and columns is          frequently useful. Given how common matrices are in game design, having native multidimensional          arrays would make much easier game development in Dart. Not only for performances, but also because          in a typed multidimensional array it is possible to add natively optimized accessors and methods          that includes moves (left/right/up/down/diagonally), changing and retrieving values with rock-solid          out-of-bounds testing, and an observer method that fires if anything wants to modify the size of          the board, quick rows/columns swapping methods, plus using accessors with custom          identifiers (a…, 1…) to identify tiles.

Please implement native multidimensional arrays in Dart.

DartBot commented 9 years ago

This comment was originally written by @kaendfinger


This would be amazing.

kevmoo commented 9 years ago

Added Area-Language, Triaged labels.

DartBot commented 9 years ago

This comment was originally written by contac...@reseau-emploi.com


Hello,

please do it !!!

srawlins commented 6 years ago

I really like this feature, but I feel like the primary use case, as laid out by all of the points above, would be multidimensional _typeddata arrays. For example, Uint8Matrix. I bet it would be easier to land as well, if it was not a language feature, but a feature of the typed_data library.