MATLAB Answers

Passing structs/objects to functions

37 views (last 30 days)
broken_arrow
broken_arrow on 16 Apr 2021 at 11:42
Edited: James Tursa on 19 Apr 2021 at 18:50
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
end
Is that correct?

Accepted Answer

Bruno Luong
Bruno Luong on 16 Apr 2021 at 11:48
Edited: Bruno Luong on 16 Apr 2021 at 11:57
"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.
  5 Comments
broken_arrow
broken_arrow on 16 Apr 2021 at 12:59
Yes, I'd imagine that the adress of the first level of a struct would contain the adresses of the second level and so on, so one can either pass the adress of level 1 and let Matlab do the work or pass the sublevel adresses directly. Anyways, I take it that it's fine to just pass the whole thing as input.

Sign in to comment.

More Answers (2)

Steven Lord
Steven Lord on 16 Apr 2021 at 14:22
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
tic
S = repmat(dir, 1000, 1000);
toc
Elapsed time is 0.590862 seconds.
whos S
Name Size Bytes Class Attributes S 2000x1000 1464000384 struct
tic
y = myfun(S);
toc
Elapsed time is 0.008938 seconds.
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);
tic
y = myfun2(A); % Does not modify A
toc
Elapsed time is 0.009651 seconds.
tic
y = myfun3(A); % Modifies A
toc
Elapsed time is 0.107321 seconds.
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
end
function y = myfun2(A)
y = A(42, 999);
end
function y = myfun3(A)
A = A + 1;
y = A(42, 999);
end

James Tursa
James Tursa on 19 Apr 2021 at 18:48
Edited: James Tursa on 19 Apr 2021 at 18:50
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.

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!