# Problem with creating view of vectors in vector of vectors (it keeps allocating)

**URL:** <https://discourse.julialang.org/t/problem-with-creating-view-of-vectors-in-vector-of-vectors-it-keeps-allocating/116825>\
**Category:** Performance\
**Tags:** memory-allocation, vector\
**Created:** [July 9, 2024, 1:40pm UTC](https://discourse.julialang.org/t/problem-with-creating-view-of-vectors-in-vector-of-vectors-it-keeps-allocating/116825 "2024-07-09T13:40:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [July 9, 2024, 1:40pm UTC](https://discourse.julialang.org/t/problem-with-creating-view-of-vectors-in-vector-of-vectors-it-keeps-allocating/116825/1 "2024-07-09T13:40:54Z")

</div>

I have a problem where Julai keeps allocating, causing something I think should be fast to be slow. Simple example:

```julia
vec_vec = [1:100 for i in 1:100]
@time vals = [vec for vec in vec_vec] # Fast, few allocations
@time vals = [vec[1:end] for vec in vec_vec] # Slow, many allocations.
@time vals = [(@view vec[1:end]) for vec in vec_vec] # Slow, many allocations.
@time vals = [[v for v in vec] for vec in vec_vec] # Slow, many allocations.

```

In practise I got a a vector of vectors, and I want to create a new vector where each value is a sub-vector of the vector at the corresponding index. In some cases I would also like to reshape this to a matrix. However, this turns out to be slow due to allocations, as in the example above. I was hoping using `@view` would solve it but that does not seem to be the case

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 9, 2024, 1:51pm UTC](https://discourse.julialang.org/t/problem-with-creating-view-of-vectors-in-vector-of-vectors-it-keeps-allocating/116825/2 "2024-07-09T13:51:11Z")

</div>

The problem is that you are [benchmarking with global variables](https://docs.julialang.org/en/v1/manual/performance-tips/#Performance-critical-code-should-be-inside-a-function). Put the code in a function. Use `@btime` from BenchmarkTools.jl to get more accurate timing statistics (and [interpolate global variables](https://juliaci.github.io/BenchmarkTools.jl/stable/manual/#Interpolating-values-into-benchmark-expressions)). When I do this, the allocations disappear:

```julia
julia> using BenchmarkTools

julia> f1(vec_vec) = [vec[1:end] for vec in vec_vec]
f1 (generic function with 1 method)

julia> f2(vec_vec) = @views [vec[1:end] for vec in vec_vec]
f2 (generic function with 1 method)

julia> @btime f1($vec_vec);
  211.219 ns (1 allocation: 1.77 KiB)

julia> @btime f2($vec_vec);
  220.346 ns (1 allocation: 1.77 KiB)

```

Note that even `f1` does not allocate here (except for a single allocation of the returned array), because a range `1:100` is a very special kind of vector that is not explicitly stored, and slices `vec[1:end]` of ranges also return ranges without allocating.

If you instead have a vector of regular heap-allocated vectors (`Vector`), e.g. returned by `rand(100)`, then you will see a big difference in allocation from using `@views`:

```julia
julia> vec_vec2 = [rand(100) for i in 1:100];

julia> @btime f1($vec_vec2);
  6.208 μs (101 allocations: 88.38 KiB)

julia> @btime f2($vec_vec2);
  243.415 ns (1 allocation: 4.12 KiB)

```

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [July 9, 2024, 3:20pm UTC](https://discourse.julialang.org/t/problem-with-creating-view-of-vectors-in-vector-of-vectors-it-keeps-allocating/116825/3 "2024-07-09T15:20:58Z")

</div>

That is great, thanks a lot and for the explanation!
