Understanding Vector allocation

I am trying to scale vector of length N into range of 0 to 1. sometimes the allocation is 1 and sometimes allocation becomes 2. I cannot understand why that is happening.

if x is length(1000) 1 allocation happening for output
if x is length 10,000 2 allocation happening.
why? may be that is how vector works?

sample_code:

function scale(X::Vector)::Vector
    scale_out = Vector{Float32}(undef, size(X,1))
    # Applying formula (x - mean) / std_dev
    return scale_out

Can you give a MWE?

this is basically caused by is pooling smaller allocations together, but asking the OS directly for larger ones. you don’t need to worry about it.

4 Likes

until 2048 it gets 1 allocation, after 2048 it goes to 2 allocation. I have not checked further…

1 Like

It shouldn’t ever be more than 2.

1 Like

if I do vector{float64}(undef, size(X,1)) will this be stack allocated or heap allocated??? I have read in cpp that std::array is stack allocated but I can’t find stack allocated array in julia except staticarrays.jl which is suitable only for small arrays(quoted in their github)

Heap allocated. If you are using large arrays, probably you want preallocation.

4 Likes