The Performance tips page in the documentation gives the following example of a function that allocates memory unexpectedly:
julia> x = rand(1000);
julia> function sum_global()
s = 0.0
for i in x
s += i
end
return s
end;
The page explains that this can be fixed by providing the array as an argument instead, but it doesn’t explain what’s causing the problem in the first place.
Why does this happen? For what purpose is memory being allocated here? Why does passing x as an argument fix the problem? Is there another way to fix it without passing the array as an argument?
I’m being bitten by something similar in my own code - a function is allocating memory that really doesn’t look like it should be allocating memory. I can’t work out what I’m doing wrong so I would like to have a better general understanding of what causes memory to be allocated in this type of situation.