<p dir="auto">I notice this that doing simply operations on an array can allocate a lot of memory when I don't think it would need to. For example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; function foo(a::AbstractArray, value::Int)
           for i in 1:length(a)
               a[i] += value
           end
       end
foo (generic function with 1 method)

julia&gt; len = 100000000; a = collect(1:len);

julia&gt; foo(a, 20); #compile

julia&gt; @time foo(a, 20);
  0.116317 seconds (4 allocations: 160 bytes)

julia&gt; @time a[:] += 20;
  1.871425 seconds (13 allocations: 1.490 GB, 6.84% gc time)

julia&gt; @time a += 20;
  0.542140 seconds (8 allocations: 762.940 MB, 0.33% gc time)

julia&gt; @time a + 20;
  1.095872 seconds (8 allocations: 762.940 MB, 7.90% gc time)"><pre class="notranslate"><code class="notranslate">julia&gt; function foo(a::AbstractArray, value::Int)
           for i in 1:length(a)
               a[i] += value
           end
       end
foo (generic function with 1 method)

julia&gt; len = 100000000; a = collect(1:len);

julia&gt; foo(a, 20); #compile

julia&gt; @time foo(a, 20);
  0.116317 seconds (4 allocations: 160 bytes)

julia&gt; @time a[:] += 20;
  1.871425 seconds (13 allocations: 1.490 GB, 6.84% gc time)

julia&gt; @time a += 20;
  0.542140 seconds (8 allocations: 762.940 MB, 0.33% gc time)

julia&gt; @time a + 20;
  1.095872 seconds (8 allocations: 762.940 MB, 7.90% gc time)
</code></pre></div>
<p dir="auto">From the example above, <code class="notranslate">foo,(a, 20)</code>, <code class="notranslate">a[:] += 20</code>, and <code class="notranslate">a += 20</code> are all doing the same thing, but my foo function is much faster and takes a fraction of the allocated memory.</p>
<p dir="auto">Should julia's memory management be able to identify those cases and avoid the extra allocation?</p>
<p dir="auto">The following code fails with an <code class="notranslate">InexactError()</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="X = [1,2,3]
Y = [4 5]
broadcast(atan2, X, Y)"><pre class="notranslate"><code class="notranslate">X = [1,2,3]
Y = [4 5]
broadcast(atan2, X, Y)
</code></pre></div>
<p dir="auto">whereas</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[atan2(x,y) for x in X, y in Y ]"><pre class="notranslate"><code class="notranslate">[atan2(x,y) for x in X, y in Y ]
</code></pre></div>
<p dir="auto">(albeit producing an array of type <code class="notranslate">Any</code>), while</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="atan2([1,2,3],[4,5,6])"><pre class="notranslate"><code class="notranslate">atan2([1,2,3],[4,5,6])
</code></pre></div>
<p dir="auto">produces an array of <code class="notranslate">Float64</code>.</p>
<p dir="auto">Can we improve the type inference so that all three cases can generate <code class="notranslate">Float64</code> arrays?  Note that this is needed for <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="20043830" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/4363" data-hovercard-type="issue" data-hovercard-url="/JuliaLang/julia/issues/4363/hovercard" href="https://github.com/JuliaLang/julia/issues/4363">#4363</a> (for <code class="notranslate">@vectorize_2arg</code> to use <code class="notranslate">broadcast</code>).</p>