Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off type checks in code generated by dart2js?

Tags:

dart

I'm writing library for simple matrix operations. For example: to multiply by transposed matrix I have a method that looks like this:

Matrix multTransp(Matrix m2) {
    Matrix res = new Matrix(rows, m2.rows);
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < m2.rows; c++) {
            double sum = 0.0;
            for (int i = 0; i < cols; i++) {
                sum += get(r, i) * m2.get(c, i);
            }
            res.set(r, c, sum);
        }
    }
    return res;
}

dart2js (with checked: false) generates code with type and range checks:

  multTransp$1: function(m2) {
    var t1, t2, res, t3, t4, t5, t6, t7, t8, r, t9, t10, c, sum, i, t11, t12;
    t1 = this.rows;
    t2 = m2.rows;
    res = L.Matrix$(t1, t2);
    for (t3 = res.m, t4 = res.rows, t5 = t3.length, t6 = this.cols, t7 = this.m, t8 = t7.length, r = 0; r < t1; ++r)
      for (t9 = m2.m, t10 = t9.length, c = 0; c < t2; ++c) {
        for (sum = 0, i = 0; i < t6; ++i) {
          t11 = r + i * t1;
          if (t11 < 0 || t11 >= t8)
            return H.ioore(t7, t11);
          t11 = t7[t11];
          t12 = c + i * t2;
          if (t12 < 0 || t12 >= t10)
            return H.ioore(t9, t12);
          t12 = J.$mul$ns(t11, t9[t12]);
          if (typeof t12 !== "number")
            return H.iae(t12);
          sum += t12;
        }
        t11 = r + c * t4;
        if (t11 < 0 || t11 >= t5)
          return H.ioore(t3, t11);
        t3[t11] = sum;
      }
    return res;
  }

How can I turn off all those checks? The code inside those for loops seems to be a performance bottleneck of my app so any optimization here will be big win for me.

like image 234
kolar Avatar asked Mar 10 '17 05:03

kolar


1 Answers

See https://dart.dev/tools/dart2js#options

Specifically -O3 to turn off type checks.

Make sure you test well, though!

like image 135
Kevin Moore Avatar answered Oct 03 '22 08:10

Kevin Moore