user1691278
user1691278

Reputation: 1885

Strange allocation in Julia

Consider this function

function test_function(state, M, new_number)
    M[state..., 1] = new_number
end

In my program, I want to run this function multiple times and each time, the arguments would be new (sometimes provided by the user).

To test the memory allocation and speed of this function, I wrote this loop


function test_for_loop(loop_num)
    state = [1, 1, 1 ]
    M     = randn(10,10,10,10)
    new_number = 3.0
    for i in 1:loop_num
        test_function(state, M, new_number)
    end 
end 

Note that test_for_loop is written purely to test the efficiency of test_function being applied multiple times. In reality, in each application, state, M, and new_number are not known in advance.

I used @allocated to find the memory allocation. As I increased loop_num, the memory allocation went up. I'm not sure why so I used --track-allocation. It turns out M[state...] = new_number is taking a huge amount of memory allocation. Can someone please explain why? Why is it allocating extra memory, and how can I reduce the memory allocation such that it doesn't increase with loop_num?

Upvotes: 5

Views: 156

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69819

The reason is:

state = [1, 1, 1]

which is a vector, which does not specify how long it is in its type.

Change it to e.g. a tuple (as tuple has known compile-time size):

state = (1, 1, 1)

and the allocation issue will be solved:

julia> function test_for_loop(loop_num)
           state = (1, 1, 1)
           M     = randn(10,10,10,10)
           new_number = 3.0
           for i in 1:loop_num
               test_function(state, M, new_number)
           end
       end
test_for_loop (generic function with 1 method)

julia> @time test_for_loop(10^8)
  0.118755 seconds (2 allocations: 78.188 KiB)

Upvotes: 6

Related Questions