# @allocate in REPL, different results, same code

**URL:** <https://discourse.julialang.org/t/allocate-in-repl-different-results-same-code/6756>\
**Category:** Performance\
**Tags:** memory-allocation\
**Created:** [October 29, 2017, 6:19pm UTC](https://discourse.julialang.org/t/allocate-in-repl-different-results-same-code/6756 "2017-10-29T18:19:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [October 29, 2017, 6:19pm UTC](https://discourse.julialang.org/t/allocate-in-repl-different-results-same-code/6756/1 "2017-10-29T18:19:00Z")

</div>

```julia
function ha(x)
  return (x, x)
end

function hs(x::StridedMatrix{Float64})
  return (x, x)
end

```

In a global scope and/or REPL:

```julia
x = eye(1);

@show @allocated ha(x)
@show @allocated ha(x)
@show @allocated hs(x)
@show @allocated hs(x)

```

produces

```julia
#= /Users/goretkin/tmp.jl:11 =# @allocated(ha(x)) = 0
#= /Users/goretkin/tmp.jl:12 =# @allocated(ha(x)) = 0
#= /Users/goretkin/tmp.jl:13 =# @allocated(hs(x)) = 9373
#= /Users/goretkin/tmp.jl:14 =# @allocated(hs(x)) = 32

```

(this is on master, 0.7.0, but basically the same holds for 0.6.0, though `@show @macro` prints differently I guess.)

`code_native` and `code_llvm` for `ha(x)`and `hs(x)` look identical, but the allocation is different.

If you wrap the code in a function, then all 4 `@allocated`s report zero:

```julia
function doit()
  x = eye(1);
  @show(@allocated ha(x))
  @show(@allocated ha(x))
  @show(@allocated hs(x))
  @show(@allocated hs(x))
end

doit()

```

So I guess the difference is whether the argument `x` is a local or global variable. The access of `x` when it is global must be causing some allocation (since the code\_llvm/code\_native are the same), though I don’t understand why it only affects the more specific method.

Second, I don’t understand why trying to run `doit` before `ha` and `hs` are defined, and then running it again once `ha` and `hs` are defined produces different results. Possibly `ha` and `hs` don’t get inlined?

```julia
function doit()
  x = eye(1);
  @show(@allocated ha(x))
  @show(@allocated ha(x))
  @show(@allocated hs(x))
  @show(@allocated hs(x))
end

try
  doit()
catch E
  @show E
end

function ha(x)
  return (x, x)
end

function hs(x::StridedMatrix{Float64})
  return (x, x)
end

doit()

```

(Came across this while investigating allocations of potrf, expecting it to be 0 in 0.7.0 (which it is). See: [Storage allocation in LAPACK.potrf! - #3 by dmbates](https://discourse.julialang.org/t/storage-allocation-in-lapack-potrf/82/3))

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 29, 2017, 8:25pm UTC](https://discourse.julialang.org/t/allocate-in-repl-different-results-same-code/6756/2 "2017-10-29T20:25:51Z")

</div>

> [@goretkin](#):
>
> code\_native and code\_llvm for ha(x)and hs(x) look identical, but the allocation is different.

Don’t do it in global scope. None of these are measuring the allocation of the function.

> [@goretkin](#):
>
> The access of x when it is global must be causing some allocation

No, accessing global do not cause any allocation, but the type can’t be inferred.

> [@goretkin](#):
>
> Second, I don’t understand why trying to run doit before ha and hs are defined, and then running it again once ha and hs are defined produces different results.

Because a valid but inefficient version of `doit` is compiled.

---

<div class="post-metadata">

**Author:** ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)\
**Post date:** [October 30, 2017, 12:02am UTC](https://discourse.julialang.org/t/allocate-in-repl-different-results-same-code/6756/3 "2017-10-30T00:02:46Z")

</div>

> [@yuyichao](#):
>
> No, accessing global do not cause any allocation, but the type can’t be inferred.

Where does the allocation come from, then? And why is it different between the two methods?

Thanks!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 30, 2017, 12:39am UTC](https://discourse.julialang.org/t/allocate-in-repl-different-results-same-code/6756/4 "2017-10-30T00:39:23Z")

</div>

> [@goretkin](#):
>
> Where does the allocation come from, then?

The function itself in this case.

> [@goretkin](#):
>
> why is it different between the two methods?

Because “the type can’t be inferred”
