Why does this function allocate memory?

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.

the problem is type instability. the compiler can’t figure out what type i is, so it has to box it. this means it can’t figure out what method of + gets used, so it has to do a runtime dispatch. them it doesn’t know what type that returns so it has to box s also.

2 Likes
const x = rand(1000)
1 Like
x::Vector{Float64} = rand(1000)

Note that this require Julia >= v1.8

1 Like

Thanks, these responses are helpful. (But they make it clear that the problem in my code is something different. I’ll try to isolate the issue and post separately if I don’t manage to fix it.)

1 Like

A somewhat common source of mysterious allocations is the infamous issue 15276. It might be worth checking up in case you are using closures.