It looks like type inference doesn't happen correctly for the nested function (for reference: I put both sets of declarations inside separate functions, and then looked at the output of @code_typed). I believe this is related to the following open issue: https://github.com/JuliaLang/julia/issues/4428
On Fri, Sep 5, 2014 at 11:15 PM, Peter Colberg <pe...@colberg.org> wrote: > Hi, > > Given the following immutable type, > > immutable Point > x::Float64 > y::Float64 > end > > the first code sinks allocations of values of that type, > > f() = Point(2.0, 3.0) > > let > x = Array(Point, 1000000) > @time for i = 1:length(x) > x[i] = f() > end > end > > ## elapsed time: 0.004287101 seconds (0 bytes allocated) > > while the second code does not sink allocations: > > let > g() = Point(2.0, 3.0) > > let > x = Array(Point, 1000000) > @time for i = 1:length(x) > x[i] = g() > end > end > end > > ## elapsed time: 0.074520601 seconds (40181616 bytes allocated, 12.42% > gc time) > > Why do the calls to the local function allocate temporaries? > > Peter >