# Question about allocations and generators

**URL:** <https://discourse.julialang.org/t/question-about-allocations-and-generators/62693>\
**Category:** Performance\
**Tags:** question, memory-allocation, generator\
**Created:** [June 10, 2021, 6:39pm UTC](https://discourse.julialang.org/t/question-about-allocations-and-generators/62693 "2021-06-10T18:39:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RomainPct](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romainpct/32/49995_2.png) [@RomainPct](https://discourse.julialang.org/u/RomainPct)\
**Post date:** [June 10, 2021, 6:39pm UTC](https://discourse.julialang.org/t/question-about-allocations-and-generators/62693/1 "2021-06-10T18:39:01Z")

</div>

Hello, this this question is out of curiosity, hoping to understand more about Julia.

Those three functions allocate differently:

```julia
g1(v::Vector{Int64}) = SVector{length(v), Int64}(v...)
g2(v::Vector{Int64}, n) = SVector{n, Int64}((v[i] for i=1:n)...)
g3(v::Vector{Int64}) = SVector{length(v), Int64}((v[i] for i=1:length(v))...)

```

```julia
julia> @benchmark g1($v) evals=1 samples=1
BenchmarkTools.Trial: 
  memory estimate: 640 bytes
  allocs estimate: 11

julia> @benchmark g2($v, 2) evals=1 samples=1
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0

julia> @benchmark g3($v) evals=1 samples=1
BenchmarkTools.Trial: 
  memory estimate: 736 bytes
  allocs estimate: 14

```

Could someone guide me towards understanding this precisely? Thanks!

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [June 11, 2021, 7:25pm UTC](https://discourse.julialang.org/t/question-about-allocations-and-generators/62693/2 "2021-06-11T19:25:56Z")

</div>

Allocations happen here because of Julia cannot infer the length of the SVector at compile time. When you pass in `v` and specify `length(v)` of the SVector, Julia can’t infer at compile time what `length(v)` is since it’s possible to change it’s length with functions like `push!`.

In your setup, `g2` doesnt allocate because you’re not interpolating `2`. Note that if you interpolate both arguments for `g2`,

```julia
julia> @btime g2($v,$2)
  1.242 μs (14 allocations: 736 bytes)

```

it allocates since Julia treats the `$2` as a variable and not as a constant.

This is a sort of [type instability](https://docs.julialang.org/en/v1/manual/performance-tips/#Write-%22type-stable%22-functions) - if you use `@code_warntype`, it shows you what Julia can’t infer by highlighting it in red.

 ![Screen Shot 2021-06-11 at 2.24.29 PM](https://global.discourse-cdn.com/julialang/original/3X/c/b/cb1bf5e4b99c13368314bcdea903df428a3460a1.png)  
Here, Julia can’t fully infer the type of the SVector output since the length is part of the type.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 11, 2021, 7:51pm UTC](https://discourse.julialang.org/t/question-about-allocations-and-generators/62693/3 "2021-06-11T19:51:08Z")

</div>

Note that all allocations come from not knowing the size of the output at compile time. If the size of the static vector to be created is known at compile time, the function does not allocate. This can be shown here, for example, in this case, where the size of the output is provided as a parameter of the type, even if the actual vector that the type contains is not static:

```julia
julia> struct Vec{N}
         v::Vector{Int}
       end

julia> g4(v::Vec{N}) where N = SVector{N,Int}(v.v[i] for i in 1:N)
g4 (generic function with 1 method)

julia> @btime g4(v) setup=(v=Vec{3}(rand(Int,3)))
  2.189 ns (0 allocations: 0 bytes)

```

(which means, more generally, that creating static vectors generally does not allocate if you can provide to the function where the allocations occur the size of the vectors as one parameter of the input types).
