Matlab coder prevent memcpy/ name alias without copy
3 views (last 30 days)
I use Matlab 2018A (soon B).
I have a data structue like:
My function gets Structure1 as argument.
For easier use i declare
myList = Structure1.structure2.list;
Then i use myList for my calculations and save it back to the stucture.
Structure1.structure2.list = myList;
When i generate the C-Code witgh Matlab Coder the myList is created using memcpy and saving back leads to a memcpy as well, which is quite painful for the performance. Is there anything like coder.ref to force a pointer for myList?
Our workaround is to pass myList (and all other lists/arrays we need from the structure) directly to the function, which leads to a lot of input and output arguments, but has a lot better performance.
David Fink on 20 Mar 2019
If Structure1.structure2.list is a Handle Class, you can assign to another name (myList) and modify the original object via myList. This also makes the final reassignment unnecessary.
Another option is the workaround you mentioned.
As another option, you could try pulling the logic dealing with myList into a new function, where myList is both an input and output.
This (with some caveats) should pass Structure1.structure2.list by pointer, or directly inline that function without a data copy.
The caveats are that a copy is generated when it is unsafe to pass by pointer. For example:
[x,x.f] = myfun(x,x.f);
Within myfun, the input values are distinct, so if one is modified, the other should not be. Thus one of x and x.f must be copied before calling myfun.