Opened 9 years ago

Last modified 6 years ago

#3844 assigned defect

Unnecessary array instantiations — at Version 2

Reported by: Rüdiger Franke Owned by: somebody
Priority: high Milestone: 2.0.0
Component: Frontend Version:
Keywords: Cc: Niklas Worschech, Francesco Casella

Description (last modified by Rüdiger Franke)

Examples that use arrays often result in huge code because OpenModelica instantiates these arrays over and over again. Have for instance a look the Modelica.Media.Examples.R134a.* examples.

Here is a simpler version:

package ArrayData
  constant Integer[:] cdata = {1, 2, 3, 4, 5, 6, 7, 8, 9};

  function f
    input Integer[:] idata;
    input Integer idx;
    output Integer y;
  algorithm
    y := idata[idx] + cdata[idx] + cdata[idx + 1];
  end f;
  
  model Test
    input Integer idx = 0;
    parameter Integer[:] pdata = cdata;
    Integer y = f(pdata, idx+1);
  end Test;
end ArrayData;

ArrayData.Test is instantiated to:

function ArrayData.f
  input Integer[:] idata;
  input Integer idx;
  output Integer y;
algorithm
  y := idata[idx] + {1, 2, 3, 4, 5, 6, 7, 8, 9}[idx] + {1, 2, 3, 4, 5, 6, 7, 8, 9}[1 + idx];
end ArrayData.f;

class ArrayData.Test
  input Integer idx = 0;
  parameter Integer pdata[1] = 1;
  parameter Integer pdata[2] = 2;
  parameter Integer pdata[3] = 3;
  parameter Integer pdata[4] = 4;
  parameter Integer pdata[5] = 5;
  parameter Integer pdata[6] = 6;
  parameter Integer pdata[7] = 7;
  parameter Integer pdata[8] = 8;
  parameter Integer pdata[9] = 9;
  Integer y = ArrayData.f({pdata[1], pdata[2], pdata[3], pdata[4], pdata[5], pdata[6], pdata[7], pdata[8], pdata[9]}, 1 + idx);
end ArrayData.Test;

It is bad if a temporary array is created when calling the function f, instead of just passing pdata. It is worse if the whole cdata array is created temporarily again and again whenever the function f wants to access one element of it.

Change History (2)

comment:1 by Rüdiger Franke, 9 years ago

Description: modified (diff)

comment:2 by Rüdiger Franke, 9 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.