# Performance closures with changing value of closed over variables

**URL:** <https://discourse.julialang.org/t/performance-closures-with-changing-value-of-closed-over-variables/59671>\
**Category:** Performance\
**Tags:** closure\
**Created:** [April 20, 2021, 2:59pm UTC](https://discourse.julialang.org/t/performance-closures-with-changing-value-of-closed-over-variables/59671 "2021-04-20T14:59:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 20, 2021, 2:59pm UTC](https://discourse.julialang.org/t/performance-closures-with-changing-value-of-closed-over-variables/59671/1 "2021-04-20T14:59:40Z")

</div>

Similar things have been discussed in other threads, but this particular example was surprising to me:

```julia
julia> function main(x)
         g(x,y) = x + y
         f(x,inner_g) = inner_g(x)
         y = 1
         f(x, x -> g(x,y))
         y = 2
         f(x, x -> g(x,y))
       end
main (generic function with 1 method)

julia> @code_warntype main(1)
Variables
  #self#::Core.Const(main)
  x::Int64
  #84::var"#84#88"{var"#g#85"}
  #83::var"#83#87"{var"#g#85"}
  y::Core.Box
  f::var"#f#86"
  g::var"#g#85"

Body::Any

```

That raises the problem of not being able to use the same function twice inside a function without changing the variable names, which is quite strange (changing the second `y` to `z` solves the instability).

The type of `y` has not even changed, and the function becomes type-unstable. Is this this issue as well? [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276)

From this example: [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276#issuecomment-599996895) I guess that the issue is the same.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 20, 2021, 4:58pm UTC](https://discourse.julialang.org/t/performance-closures-with-changing-value-of-closed-over-variables/59671/2 "2021-04-20T16:58:36Z")

</div>

As far as I can see, it is the same issue reported above.
