Puzzle with memory allocation

The line in the below code if inrange(... allocates for some reason a lot of memory. For the life of me I can’t figure out why. Any ideas?

        -     # Initialize the output list
230953488     vlist= zeros(FInt, size(v,1)); nn= 0;
        - 
        - 
        -     # Process the different options
    80448     if box != nothing
        0         sdim = size(v,2)
    46624         dim = Int(round(length(box)/2.))::FInt;
        0         @assert dim == sdim "Dimension of box not matched to dimension of array of vertices"
  1613833         abox = FFltVec(vec(box))
        0         inflatebox!(abox, inflatevalue)
        0         if sdim == 3
   660495             bx1, bx2, bx3, bx4, bx5, bx6 = FFltVec(abox)
        0             for j in 1:size(v,1)
2769886576                 if  inrange(bx1,bx2,v[j, 1]) &&
        -                     inrange(bx3,bx4,v[j, 2]) &&
        -                     inrange(bx5,bx6,v[j, 3])
        0                     nn = nn + 1; vlist[nn] = j;
        -                 end
        -             end
        -         else

The function used above is defined as @inline inrange(rangelo,rangehi,x) = (rangelo <= x <= rangehi). v is matrix of double precision numbers.

I wonder if this is the problem. does FFltVec return a Vector? If so, then the compiler may not be able to infer the types of bx1, … bx6 since the number of elements in FFltVec is not visible to the compiler.

Have you run @code_warntype?

1 Like

Nice. I was looking at it for too long, I think. Thanks!