<p dir="auto">From julia-users, ref <a href="https://groups.google.com/d/msg/julia-users/IsRlOLGeEs8/RRwrs3u2AQAJ" rel="nofollow">https://groups.google.com/d/msg/julia-users/IsRlOLGeEs8/RRwrs3u2AQAJ</a></p>
<p dir="auto">Shortened version:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; @which pi*pi
*(x::Irrational, y::Irrational) at irrationals.jl:88

julia&gt; module Erratic2
           typealias DummyUnion Union{Irrational,AbstractFloat}
           abstract DummyReal &lt;: Real
           import Base.*
           *(x::DummyUnion, y::DummyReal) = &quot;wow&quot;
       end
Erratic2

julia&gt; pi*pi
------------------------------------------------------------------------------------------
ErrorException                                          Stacktrace (most recent call last)
[#1] — *(::Irrational{:π}, ::Irrational{:π})
       ⌙ at promotion.jl:256

* not defined for Irrational{:π}

julia&gt; @which pi*pi
*{T&lt;:Number}(x::T, y::T) at promotion.jl:256"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">@which</span> <span class="pl-c1">pi</span><span class="pl-k">*</span><span class="pl-c1">pi</span>
<span class="pl-k">*</span>(x<span class="pl-k">::</span><span class="pl-c1">Irrational</span>, y<span class="pl-k">::</span><span class="pl-c1">Irrational</span>) at irrationals<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">88</span>

julia<span class="pl-k">&gt;</span> <span class="pl-k">module</span> Erratic2
           typealias DummyUnion Union{Irrational,AbstractFloat}
           abstract DummyReal <span class="pl-k">&lt;:</span> <span class="pl-c1">Real</span>
           <span class="pl-k">import</span> Base<span class="pl-k">.*</span>
           <span class="pl-k">*</span>(x<span class="pl-k">::</span><span class="pl-c1">DummyUnion</span>, y<span class="pl-k">::</span><span class="pl-c1">DummyReal</span>) <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>wow<span class="pl-pds">"</span></span>
       <span class="pl-k">end</span>
Erratic2

julia<span class="pl-k">&gt;</span> <span class="pl-c1">pi</span><span class="pl-k">*</span><span class="pl-c1">pi</span>
<span class="pl-k">------------------------------------------------------------------------------------------</span>
ErrorException                                          Stacktrace (most recent call last)
[<span class="pl-c"><span class="pl-c">#</span>1] — *(::Irrational{:π}, ::Irrational{:π})</span>
       ⌙ at promotion<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">256</span>

<span class="pl-k">*</span> not defined <span class="pl-k">for</span> Irrational{<span class="pl-c1">:π</span>}

julia<span class="pl-k">&gt;</span> <span class="pl-c1">@which</span> <span class="pl-c1">pi</span><span class="pl-k">*</span><span class="pl-c1">pi</span>
<span class="pl-k">*</span>{T<span class="pl-k">&lt;:</span><span class="pl-c1">Number</span>}(x<span class="pl-k">::</span><span class="pl-c1">T</span>, y<span class="pl-k">::</span><span class="pl-c1">T</span>) at promotion<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">256</span></pre></div>
<p dir="auto">This arises because of the following reordering of "old" methods (highlighting from <code class="notranslate">kdiff3</code>):<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/1525481/18381927/426104a6-7644-11e6-902f-2f63e0f95785.png"><img src="https://cloud.githubusercontent.com/assets/1525481/18381927/426104a6-7644-11e6-902f-2f63e0f95785.png" alt="methmul" style="max-width: 100%;"></a></p>
<p dir="auto">Note that the method at <code class="notranslate">promotion.jl:256</code> is now above the method at <code class="notranslate">irrationals.jl:88</code>.</p>
<p dir="auto">Hi all,</p>
<p dir="auto">The following code seems to allocate a lot of memory:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function test!(arr::Array{(Int, Int)})
    for i = 1:10_000_000
        arr[1] = arr[2]
    end
end

a = [(1, 1), (2, 2)]
test!(a)"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">test!</span>(arr<span class="pl-k">::</span><span class="pl-c1">Array{(Int, Int)}</span>)
    <span class="pl-k">for</span> i <span class="pl-k">=</span> <span class="pl-c1">1</span><span class="pl-k">:</span><span class="pl-c1">10_000_000</span>
        arr[<span class="pl-c1">1</span>] <span class="pl-k">=</span> arr[<span class="pl-c1">2</span>]
    <span class="pl-k">end</span>
<span class="pl-k">end</span>

a <span class="pl-k">=</span> [(<span class="pl-c1">1</span>, <span class="pl-c1">1</span>), (<span class="pl-c1">2</span>, <span class="pl-c1">2</span>)]
<span class="pl-c1">test!</span>(a)</pre></div>
<p dir="auto">Here are the results of <code class="notranslate">@time test!(a)</code> (using HEAD on the master branch, commit <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/JuliaLang/julia/commit/ce29d785098564b61af0c65113595b1af7ba0acb/hovercard" href="https://github.com/JuliaLang/julia/commit/ce29d785098564b61af0c65113595b1af7ba0acb"><tt>ce29d78</tt></a>) :</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="elapsed time: 0.439144953 seconds (320013808 bytes allocated, 40.35% gc time)"><pre class="notranslate"><code class="notranslate">elapsed time: 0.439144953 seconds (320013808 bytes allocated, 40.35% gc time)
</code></pre></div>
<p dir="auto">On the other hand, using a composite type works as expected:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="immutable Pair{T1, T2}
  a::T1
  b::T2
end

function test!(arr::Array{Pair{Int, Int}})
    for i = 1:10_000_000
        arr[1] = arr[2]
    end
end

a = [Pair(1, 1), Pair(2, 2)]
test!(a)"><pre class="notranslate"><code class="notranslate">immutable Pair{T1, T2}
  a::T1
  b::T2
end

function test!(arr::Array{Pair{Int, Int}})
    for i = 1:10_000_000
        arr[1] = arr[2]
    end
end

a = [Pair(1, 1), Pair(2, 2)]
test!(a)
</code></pre></div>
<p dir="auto">Here <code class="notranslate">@time test!(a)</code> yields</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="elapsed time: 0.022649237 seconds (80 bytes allocated)"><pre class="notranslate"><code class="notranslate">elapsed time: 0.022649237 seconds (80 bytes allocated)
</code></pre></div>
<p dir="auto">Am I missing something obvious? It seems that each assignment triggers an allocation in the case of tuples (adding <code class="notranslate">@time</code> before the assignment suggests a 32 bytes allocation). Why is that? Is this documented somewhere?</p>
<p dir="auto">(I ran into this problem when trying to use a heap to extract the top <code class="notranslate">n</code> elements from a list of tuples without sorting it first, as done in Python's <code class="notranslate">heapq.nlargest</code> method)</p>