# Very Mysterious Memory Allocation

**URL:** <https://discourse.julialang.org/t/very-mysterious-memory-allocation/4511>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 28, 2017, 6:15am UTC](https://discourse.julialang.org/t/very-mysterious-memory-allocation/4511 "2017-06-28T06:15:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nitin\_Arora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitin_arora/32/1174_2.png) [@Nitin\_Arora](https://discourse.julialang.org/u/Nitin_Arora)\
**Post date:** [June 28, 2017, 6:15am UTC](https://discourse.julialang.org/t/very-mysterious-memory-allocation/4511/1 "2017-06-28T06:15:13Z")

</div>

I had a similar issue couple of years and looks like its back. See the MWE function below:

```julia
function getdW{T<:AbstractFloat}(k::T,work::AbstractVector{T})
     local sqrt2l = 1.41421356237309510
     local K2c0 = 0.7542472332656508
     local K2c1 = -0.2
     local K2c2 = 0.08081220356417687
     local K2c3 = -0.031746031746031744
     local K2c4 = 0.012244273267299524
     local K2c5 = -2.0/429.0
     local K2c6 = 8.0 * 1.41421356237309510/6435.0
     local K2c7 = -8.0/12155.0
     local K2c8 = 8.0*1.41421356237309510/46189.0
     local k00 = 0.3535533905932738
     local k02 = 3.00*k00*0.25
     local k03 = -2.00/3.0
     local k04 = 15.0*1.41421356237309510/128.0
     local k05 = -2.0/5.0
     local k06 = 35.0*1.41421356237309510/512.0
     local k07 = -8/35.0
     local k08 = 315.0*1.41421356237309510/8192.0
     local sqrt2by4 = sqrt2l*0.25
     local dk01 = 3.00*sqrt2by4/2.00
     local dk02 = -2.0
     local dk03 = 15.00*sqrt2l/32.0
     local dk04 = -2.0

     local dK2c0 = -1.0/5.0
     local dK2c1 = 4.0*sqrt2l/35.0
     local dK2c2 = -2.0/21.0
     local dK2c3 = 8.0*sqrt2l/231.0
     local dK2c4 = -10.0/429.0
     local dK2c5 = 16.0*sqrt2l/2145.0
     local dK2c6 = -56.0/12155.0
     local dK2c7 =	64.0*sqrt2l/46189.0

    W::T = 0.0
    dW::T = 0.0
    t2::T = k*k
    m = (2.0-t2)
    bym = 1.0/m

    if k<=-1E-3
      t3 = acos(t2 - 1.0)
      t6 = 2.0*π - t3
      W	=	(t6 * sqrt(bym) - k)*bym
      dW = (-2.0+3.0*W*k)*bym
    elseif k >= 1.4342135623730952
      t7 = (k+1.0) * (k-1.00)
      t3 = log(t7 + sqrt(t7*t7-1.0))
      t4 = t7-1.0
      t5 = 1.0/t4
      W	= (-t3 * sqrt(t5) + k)*t5
      dW = (-2.0+3.0*W*k)*bym
  elseif k>=1E-3 && k < 1.3942135623730951::T
        t3 = acos(t2 - 1.0)
        t6 = t3
        W = (t6 * sqrt(bym) - k)*bym
        dW = (-2.0+3.0*W*k)*bym
    elseif k<1E-3 # series for k ~= 0 improve convergence
      xt = π
      t3 = t2*k
      t4 = t2*t2
      t5 = t4*k
      t6 = t3*t3
      t7 = t5*t2
      t8 = t4*t4
      W = xt*k00 - k + (xt*k02)*t2 + k03*t3 + (xt*k04)*t4 + k05*t5 + (xt*k06)*t6 + k07*t7 + (xt*k08)*t8
      dW = -1.00 + xt*dk01*k +dk02*t2 + xt*dk03*t3 + dk04*t4
    elseif k < 1.4342135623730952
      xt ::T = k-1.41421356237309510
      t2 = xt*xt
      t3 = t2*xt
      t4 = t3*xt
      t5 = t4*xt
      t6 = t5*xt
      t7 = t6*xt
      t8 = t7*xt
      W = K2c0 + K2c1*k + K2c2*t2 + K2c3*t3 + K2c4*t4 + K2c5*t5 + K2c6*t6 + K2c7*t7 + K2c8*t8
      dW = dK2c0 + dK2c1*xt+ dK2c2*t2 + dK2c3*t3 + dK2c4*t4 + dK2c5*t5 + dK2c6*t6 + dK2c7*t7
    end
    work[1] = W::T
    work[2] = dW::T
    return nothing
end

```

now look at the following calls:

```julia
const work = Vector{Float64}(2)

julia> @btime getdW(-1.2,work)
  29.962 ns (0 allocations: 0 bytes)

julia> @btime getdW(1.2,work)
  30.806 ns (0 allocations: 0 bytes)

julia> @btime getdW(1.414,work)
  86.559 ns (14 allocations: 224 bytes)

julia> @btime getdW(0.0001,work)
  47.341 ns (7 allocations: 112 bytes)

```

The function seems to allocate memory based on which branch it is in !  
This makes no sense to me as the @code\_warntype shows no type instabilities and the branches are statically types. I opened a similar issue an year or so ago on Julia google forums and it seemed to be fixed but the problem has cropped up again more severely this time.  
Does anyone know whats going on ? I plan to call this function millions of times and not sure how to get around this weird performance issue.

I am on Julia 0.6.

Thanks!

---

<div class="post-metadata">

**Author:** ![Nitin\_Arora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitin_arora/32/1174_2.png) [@Nitin\_Arora](https://discourse.julialang.org/u/Nitin_Arora)\
**Post date:** [June 28, 2017, 7:12am UTC](https://discourse.julialang.org/t/very-mysterious-memory-allocation/4511/2 "2017-06-28T07:12:42Z")

</div>

Upon further investigation, looks like the memory allocation happens if the code executes either of the last two `elseif` blocks. They dont seem to be allocating any memory so I am not sure know whats happening.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 28, 2017, 7:21am UTC](https://discourse.julialang.org/t/very-mysterious-memory-allocation/4511/3 "2017-06-28T07:21:51Z")

</div>

The lines are too long so you’re hitting the splat penalty. Add some parentheses.

---

<div class="post-metadata">

**Author:** ![Nitin\_Arora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitin_arora/32/1174_2.png) [@Nitin\_Arora](https://discourse.julialang.org/u/Nitin_Arora)\
**Post date:** [June 28, 2017, 7:27am UTC](https://discourse.julialang.org/t/very-mysterious-memory-allocation/4511/4 "2017-06-28T07:27:00Z")

</div>

> [@ChrisRackauckas](#):
>
> The lines are too long so you’re hitting the splat penalty. Add some parentheses.

Oh, I wasn’t even aware of such a penalty. Let me try that.

thanks

---

<div class="post-metadata">

**Author:** ![Nitin\_Arora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nitin_arora/32/1174_2.png) [@Nitin\_Arora](https://discourse.julialang.org/u/Nitin_Arora)\
**Post date:** [June 28, 2017, 7:31am UTC](https://discourse.julialang.org/t/very-mysterious-memory-allocation/4511/5 "2017-06-28T07:31:11Z")

</div>

Awesome… that did it. Thanks for this tip.  
Is this penalty documented somewhere ?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 28, 2017, 7:37am UTC](https://discourse.julialang.org/t/very-mysterious-memory-allocation/4511/6 "2017-06-28T07:37:26Z")

</div>

> [@Nitin\_Arora](#):
>
> Awesome… that did it. Thanks for this tip.  
> Is this penalty documented somewhere ?

Not sure. But I hope it actually just gets “fixed” soon enough that it doesn’t matter. Right now the cutoff is 16 operations, which is a little too easy to accidentally run into. Issue which links to a bunch of other issues + PR:

> <https://github.com/JuliaLang/julia/issues/22370>
>
> It has been discussed before that some \`@infer\_the\_crap\_out\_of\_this\` would be ve…ry useful:
> 
> https://github.com/JuliaLang/julia/pull/16734#issuecomment-223842270
> 
> The \`MAX\_TUPLETYPE\_LEN\` is low enough that it's pretty easy to run into issues with it. When any code generation or optimization is occurring, the generated code tends to have these issues. See:
> 
> https://github.com/JuliaLang/julia/issues/22255
> https://github.com/JuliaLang/julia/issues/22275
> 
> The problem there seems to be that even if you put parenthesis to avoid this issue, \`@fastmath\` can re-order operations and broadcast generates its own anonymous functions which might do this, meaning that you're prone to problems with any large line. In addition I see that many times types with large numbers of fields/parameters won't infer well:
> 
> https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/blob/master/src/solve.jl#L284
> 
> and don't have an MWE for that yet but believe something similar is going on here.
> 
> It would be nice to be able to selectively tell the compiler "I know this line is longer than normal, please handle it anyways". If anything, it would at least make debugging easier if there was a way to selectively change this, i.e. a one-word macro to test "yes, this is an inference issue due to the recursion limit".

> <https://github.com/JuliaLang/julia/pull/22545>
>
> One proposal for https://github.com/JuliaLang/julia/issues/22370. I took @vtjnas…h 's changes, put a nice simple number on them, and also upped the splatting length. Not entirely sure how to argue specifics here.

The PR won’t actually go anywhere until someone who knows the internals well takes it up, but I hope it gets something started.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 28, 2017, 7:39am UTC](https://discourse.julialang.org/t/very-mysterious-memory-allocation/4511/7 "2017-06-28T07:39:05Z")

</div>

There was a PR to document it at [https://github.com/JuliaLang/julia/pull/15211](https://github.com/JuliaLang/julia/pull/15211) but it got reverted due to some problems with it and the corrections never made it back in.
