<p dir="auto">Hello,</p>
<p dir="auto">Originally seen when using FunctionWrappers. <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/yuyichao/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/yuyichao">@yuyichao</a> was nice enough to provide a MWE, that shows non-repeatably inefficient code. I am out of my depth here for such julia internals, just relaying the message :)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; using BenchmarkTools

julia&gt; f3(x, y) = x - x + 0.5
f3 (generic function with 1 method)

julia&gt; g3(f, x, y) = f3(x, y)
g3 (generic function with 1 method)

julia&gt; p3 = @cfunction(g3, Float64, (Ref{typeof(f3)}, Float64, Float64))
Ptr{Nothing} @0x00007f8405b31650

julia&gt; @benchmark ccall($p3, Float64, (Ref{typeof(f3)}, Float64, Float64), f3, 1.0, 2.0)
BenchmarkTools.Trial:
  memory estimate:  48 bytes
  allocs estimate:  3
  --------------
  minimum time:     66.488 ns (0.00% GC)
  median time:      68.980 ns (0.00% GC)
  mean time:        74.425 ns (0.45% GC)
  maximum time:     510.097 ns (73.69% GC)
  --------------
  samples:          10000
  evals/sample:     977


julia&gt; p3 = @cfunction(g3, Float64, (Ref{typeof(f3)}, Float64, Float64))
Ptr{Nothing} @0x00007f8405b42ec0

julia&gt; @benchmark ccall($p3, Float64, (Ref{typeof(f3)}, Float64, Float64), f3, 1.0, 2.0)
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     4.527 ns (0.00% GC)
  median time:      4.554 ns (0.00% GC)
  mean time:        4.965 ns (0.00% GC)
  maximum time:     44.207 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1000"><pre class="notranslate"><code class="notranslate">julia&gt; using BenchmarkTools

julia&gt; f3(x, y) = x - x + 0.5
f3 (generic function with 1 method)

julia&gt; g3(f, x, y) = f3(x, y)
g3 (generic function with 1 method)

julia&gt; p3 = @cfunction(g3, Float64, (Ref{typeof(f3)}, Float64, Float64))
Ptr{Nothing} @0x00007f8405b31650

julia&gt; @benchmark ccall($p3, Float64, (Ref{typeof(f3)}, Float64, Float64), f3, 1.0, 2.0)
BenchmarkTools.Trial:
  memory estimate:  48 bytes
  allocs estimate:  3
  --------------
  minimum time:     66.488 ns (0.00% GC)
  median time:      68.980 ns (0.00% GC)
  mean time:        74.425 ns (0.45% GC)
  maximum time:     510.097 ns (73.69% GC)
  --------------
  samples:          10000
  evals/sample:     977


julia&gt; p3 = @cfunction(g3, Float64, (Ref{typeof(f3)}, Float64, Float64))
Ptr{Nothing} @0x00007f8405b42ec0

julia&gt; @benchmark ccall($p3, Float64, (Ref{typeof(f3)}, Float64, Float64), f3, 1.0, 2.0)
BenchmarkTools.Trial:
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     4.527 ns (0.00% GC)
  median time:      4.554 ns (0.00% GC)
  mean time:        4.965 ns (0.00% GC)
  maximum time:     44.207 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1000
</code></pre></div>
<p dir="auto">Cheers!</p>
<p dir="auto">Copied from: <a href="https://discourse.julialang.org/t/performance-and-memory-regression-with-function-wrappers-in-julia-1-4/44589" rel="nofollow">https://discourse.julialang.org/t/performance-and-memory-regression-with-function-wrappers-in-julia-1-4/44589</a></p>
<h3 dir="auto">Setup:</h3>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; using BenchmarkTools

julia&gt; using FunctionWrappers: FunctionWrapper

julia&gt; w = let closed_over=([1], [2])
         FunctionWrapper{typeof(closed_over), Tuple{}}(() -&gt; closed_over)
       end;"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-k">using</span> BenchmarkTools

julia<span class="pl-k">&gt;</span> <span class="pl-k">using</span> FunctionWrappers<span class="pl-k">:</span> FunctionWrapper

julia<span class="pl-k">&gt;</span> w <span class="pl-k">=</span> <span class="pl-k">let</span> closed_over<span class="pl-k">=</span>([<span class="pl-c1">1</span>], [<span class="pl-c1">2</span>])
         <span class="pl-c1">FunctionWrapper</span><span class="pl-c1">{typeof(closed_over), Tuple{}}</span>(() <span class="pl-k">-&gt;</span> closed_over)
       <span class="pl-k">end</span>;</pre></div>
<h3 dir="auto">Results</h3>
<p dir="auto">Julia 1.3.1:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; @btime $w()
  5.435 ns (0 allocations: 0 bytes)"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">@btime</span> <span class="pl-k">$</span><span class="pl-c1">w</span>()
  <span class="pl-c1">5.435</span> ns (<span class="pl-c1">0</span> allocations<span class="pl-k">:</span> <span class="pl-c1">0</span> bytes)</pre></div>
<p dir="auto">Julia 1.4.2:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; @btime $w()
  9.919 ns (1 allocation: 16 bytes)"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">@btime</span> <span class="pl-k">$</span><span class="pl-c1">w</span>()
  <span class="pl-c1">9.919</span> ns (<span class="pl-c1">1</span> allocation<span class="pl-k">:</span> <span class="pl-c1">16</span> bytes)</pre></div>
<p dir="auto">Julia 1.5.0</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; @btime $w()
  10.112 ns (1 allocation: 32 bytes)"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">@btime</span> <span class="pl-k">$</span><span class="pl-c1">w</span>()
  <span class="pl-c1">10.112</span> ns (<span class="pl-c1">1</span> allocation<span class="pl-k">:</span> <span class="pl-c1">32</span> bytes)</pre></div>
<p dir="auto">Julia 1.6.0-DEV.607</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; @btime $w()
  11.846 ns (1 allocation: 32 bytes)"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">@btime</span> <span class="pl-k">$</span><span class="pl-c1">w</span>()
  <span class="pl-c1">11.846</span> ns (<span class="pl-c1">1</span> allocation<span class="pl-k">:</span> <span class="pl-c1">32</span> bytes)</pre></div>
<p dir="auto">This turns out to be the source of a significant performance regression in Parametron.jl (which was designed to allocate zero memory).</p>
<p dir="auto">Here's my system info:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; versioninfo()
Julia Version 1.5.0
Commit 96786e22cc (2020-08-01 23:44 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)"><pre class="notranslate"><code class="notranslate">julia&gt; versioninfo()
Julia Version 1.5.0
Commit 96786e22cc (2020-08-01 23:44 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
</code></pre></div>
<p dir="auto"><a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/yuyichao/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/yuyichao">@yuyichao</a> pointed to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="586971885" data-permission-text="Title is private" data-url="https://github.com/yuyichao/FunctionWrappers.jl/issues/14" data-hovercard-type="issue" data-hovercard-url="/yuyichao/FunctionWrappers.jl/issues/14/hovercard" href="https://github.com/yuyichao/FunctionWrappers.jl/issues/14">yuyichao/FunctionWrappers.jl#14</a> as a related issue. I cannot reproduce the MWE from <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="586971885" data-permission-text="Title is private" data-url="https://github.com/yuyichao/FunctionWrappers.jl/issues/14" data-hovercard-type="issue" data-hovercard-url="/yuyichao/FunctionWrappers.jl/issues/14/hovercard?comment_id=603379495&amp;comment_type=issue_comment" href="https://github.com/yuyichao/FunctionWrappers.jl/issues/14#issuecomment-603379495">yuyichao/FunctionWrappers.jl#14 (comment)</a> on Julia 1.5, but the slowness and extra allocation of my example here is still present in Julia 1.5.0.</p>