Passing structs/objects to functions
132 views (last 30 days)
When applying functions to structs, I often pass the entire struct as an input argument even if not all the variables within it are needed by the function in order to keep the input argument list short. AFAIK functions are JIT compiled on their first execution. That makes me wonder: Does the compiler realize which parts of the struct are actually needed and pass only these to the function? Or is the entire struct loaded into the function even if just one element is modified (which could potentially cause enormous overhead)? I would guess it's the former since with objects, the entire object is usually passed to each method:
function obj = whatever(obj,otherArgument1,etc)
% function code
Is that correct?
Bruno Luong on 16 Apr 2021
Edited: Bruno Luong on 16 Apr 2021
"Does the compiler realize which parts of the struct are actually needed and pass only these to the function? "
MATLAB don't pass input values of function like C and C++, it passe input (structure) "address" (mxArray pointer). So your question is not applicable thus irrelevant.
More Answers (2)
Steven Lord on 16 Apr 2021
Just because MATLAB passes a large array into a function doesn't mean it needs to make a copy of that array if you don't modify the input in the function
S = repmat(dir, 1000, 1000);
y = myfun(S);
It took much less time to perform the operation in myfun than it did to create S, and if myfun were copying S you'd expect that time to be closer to the creation time. As a different example using a numeric array rather than a struct:
A = rand(4000, 4000);
y = myfun2(A); % Does not modify A
y = myfun3(A); % Modifies A
There's a limit to how big I can make A in a MATLAB Answers post, but try the code yourself in a desktop installation of MATLAB with a larger A if you want to see a larger difference in the times.
function y = myfun(S)
y = fieldnames(S); % S is not modified and so not copied
function y = myfun2(A)
y = A(42, 999);
function y = myfun3(A)
A = A + 1;
y = A(42, 999);
James Tursa on 19 Apr 2021
Edited: James Tursa on 19 Apr 2021
MATLAB typically passes shared data copies of input arguments to functions. That means creating a separate mxArray header for the variable and then sharing the data pointers. For cell arrays and structs, that means that only one top level data pointer is copied. The individual addresses of all of the cell and field variables (perhaps hundreds or thousands of these) are not copied ... they are part of the "shared data" that is pointed to by the one top level data pointer. Passing cell or struct variables to functions takes about the same amount of overhead as passing a regular numeric variable. I.e., it takes about the same amount of effort to pass a struct with thousands of field elements as it does to pass a small 2D numeric matrix.
If you subsequently modify one of those input arguments inside the function, then MATLAB needs to make a deep copy of the variable (or deep copy of the cell or field you are modifying) first, which will take additional time and memory.