News for hackers. Stuff that matters.
Re:Matlab’s internal memory representation (2012) by imurray on Saturday August 22, 2015 @01:34PM (#10103071)
Here's an example:

    A = plus_diag(A, x);  % Using [1] below
Adds a vector x onto the diagonal elements of a huge matrix A. In old versions of Matlab, 2*huge memory would temporarily be allocated. Now the memory usage doesn't increase: A is altered in place. This optimization happens automatically when Matlab notices it can be done. The code has to be in a function though, not run from the command-line or a script.

[1] http://homepages.inf.ed.ac.uk/imurray2/code/imurray-matlab/p...

Re:Matlab’s internal memory representation (2012) by nimrody on Saturday August 22, 2015 @03:42PM (#10103497)
The fact that A is updated in-place is an optimization that happens only if you pass a variable and assign the output to the same variable.

It is not a characteristic of the function `plus_diag`. The function itself is completely pure.

Re:Matlab’s internal memory representation (2012) by imurray on Sunday August 23, 2015 @10:04AM (#10105504)
I'm well aware of how and when it works. It is the recognized idiom to perform in-place operations in practice in Matlab code [1].

It would be nice to take advantage of the same idea in mex code where possible. For example an API function that indicates if it is safe to over-write an input array and return it as the corresponding output or not. I don't think Mathworks currently provide documented and future-safe means to do so however. (If one controls all the code, updating things in-place is possible in practice if careful, using the A(1)=A(1); trick I gave in a parent post. Such code is just not guaranteed to be future-proof.)

[1] http://blogs.mathworks.com/loren/2007/03/22/in-place-operati...