# Very strange performance issue of a simple code

**URL:** https://discourse.julialang.org/t/very-strange-performance-issue-of-a-simple-code/25347
**Category:** General Usage
**Tags:** performance
**Created:** [June 16, 2019, 7:38pm UTC](https://discourse.julialang.org/t/very-strange-performance-issue-of-a-simple-code/25347 "2019-06-16T19:38:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Dandan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandan/32/8935_2.png) [@Dandan](https://discourse.julialang.org/u/Dandan)
#### Post date: [June 16, 2019, 7:38pm UTC](https://discourse.julialang.org/t/very-strange-performance-issue-of-a-simple-code/25347/1 "2019-06-16T19:38:41Z")

</div>

I’ve just spot a very strange behavior of this simple Julia code

```julia
function solve_inner(N)

  invs = ones(Int32, N)
  for q=1:N
    invs[q] = 0
  end
    
  function inner(n)
    return 0
  end
  
  function wrapper(n)
    return inner(n)
  end 

  for i = 2:N
      s = inner(i)
      # s = wrapper(i)
  end

  return 0
end

function solve_wrapper(N)

  invs = ones(Int32, N)
  for q=1:N
    invs[q] = 0
  end
  
  function inner(n)
    return 0
  end

  function wrapper(n)
    return inner(n)
  end 

  for i = 2:N
      # s = inner(i)
      s = wrapper(i)
  end
  
  return 0
end

N = 10^7
@time solve_inner(N)
@time solve_wrapper(N)

```

It gives the following results

```julia
  0.223420 seconds (10.03 M allocations: 192.456 MiB, 3.94% gc time)
  0.034134 seconds (27.43 k allocations: 39.591 MiB, 39.92% gc time)

```

As you can see, direct call of inner function in a for loop uses huge number of allocations in comparison to calling inner function via wrapper function. Any explanations?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [June 16, 2019, 8:00pm UTC](https://discourse.julialang.org/t/very-strange-performance-issue-of-a-simple-code/25347/2 "2019-06-16T20:00:32Z")

</div>

Do you know what is stranger? If you remove the `wrapper` method from inside `solve_inner` (that does not use it anyway, the time become the same)

```julia
using BenchmarkTools

function solve_inner(N)
  invs = ones(Int32, N)
  for q=1:N
    invs[q] = 0
  end
    
  function inner(n)
    return 0
  end
  
  for i = 2:N
      s = inner(i)
  end

  return 0
end

function solve_wrapper(N)
  invs = ones(Int32, N)
  for q=1:N
    invs[q] = 0
  end
  
  function inner(n)
    return 0
  end

  function wrapper(n)
    return inner(n)
  end 

  for i = 2:N
      s = wrapper(i)
  end
  
  return 0
end

N = 10^7
@btime solve_inner(N)
@btime solve_wrapper(N)

```

gives

> 20.755 ms (2 allocations: 38.15 MiB)  
> 20.694 ms (2 allocations: 38.15 MiB)

---

<div class="post-metadata">

### Author: ![Dandan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandan/32/8935_2.png) [@Dandan](https://discourse.julialang.org/u/Dandan)
#### Post date: [June 16, 2019, 8:23pm UTC](https://discourse.julialang.org/t/very-strange-performance-issue-of-a-simple-code/25347/3 "2019-06-16T20:23:50Z")

</div>

This is strange too. Though in the real code that I was working on I removed the wrapper and put its code into the for loop. Still got this strange performance hit.

---

<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 16, 2019, 8:47pm UTC](https://discourse.julialang.org/t/very-strange-performance-issue-of-a-simple-code/25347/4 "2019-06-16T20:47:48Z")

</div>

This is [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276). You can check with `@code_warntype` that the `Box` is there in the first case.

However, I am trying this on a later commit and I get:

```julia
julia> @btime solve_inner(N)
  11.124 ms (2 allocations: 38.15 MiB)
0

julia> @btime solve_wrapper(N)
  11.163 ms (2 allocations: 38.15 MiB)
0

```

so it seems fixed. Might be fixed in the 1.2-rc1 release but needs to be tested.

---

<div class="post-metadata">

### Author: ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)
#### Post date: [June 16, 2019, 8:55pm UTC](https://discourse.julialang.org/t/very-strange-performance-issue-of-a-simple-code/25347/5 "2019-06-16T20:55:08Z")

</div>

I just tried. It is solved on 1.2-rc1.
