Main Content

Avoid Data Copies of Function Inputs in Generated Code

You can reduce the number of copies in your generated code by writing functions that use the same variable as both an input and an output. For example:

function A = foo(A,B) %#codegen
A = A * B;
end

This coding practice uses a reference parameter optimization. When a variable acts as both input and output, the generated code passes the variable by reference instead of redundantly copying the input to a temporary variable. In the preceding example, input A is passed by reference in the generated code because it also acts as an output for function foo:

...
/* Function Definitions */
void foo(double *A, double B)
{
    *A *= B;
}
...

The reference parameter optimization reduces memory usage and execution time, especially when the variable passed by reference is a large data structure. To achieve these benefits at the call site, call the function with the same variable as both input and output.

By contrast, suppose that you rewrite function foo without the optimization:

function y = foo2(A,B) %#codegen
y = A * B;
end

The generated code passes the inputs by value and returns the value of the output:

...
/* Function Definitions */
double foo2(double A, double B)
{
   return A * B;
}
...

The reference parameter optimization does not apply to constant inputs. If the same variable is an input and an output, and the input is constant, the code generator treats the output as a separate variable. For example, consider the function foo:

function A = foo(A,B) %#codegen
A = A * B;
end

Generate code in which A has a constant value 2.

codegen -config:lib foo -args {coder.Constant(2) 3} -report

The generated code elides the constant variable A and passes the non-constant variable B by value.

...
/* Function Definitions */
double foo(double B)
{
  return 2.0 * B;
}
...

Related Topics