# What's behind the scenes of the view (...) function?

**URL:** <https://discourse.julialang.org/t/whats-behind-the-scenes-of-the-view-function/88780>\
**Category:** General Usage\
**Tags:** array, memory-allocation, views\
**Created:** [October 15, 2022, 2:36pm UTC](https://discourse.julialang.org/t/whats-behind-the-scenes-of-the-view-function/88780 "2022-10-15T14:36:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 15, 2022, 2:36pm UTC](https://discourse.julialang.org/t/whats-behind-the-scenes-of-the-view-function/88780/1 "2022-10-15T14:36:16Z")

</div>

Since to (almost. Which not?) All the effects the following two assignments are equivalent, why, since view is faster and cheaper, is not `[]` (ie getindex) defined in the same way?

```julia
a=[1,2,3]
b=a[1:2]
b=view(a, 1:2)

```

If the explanation isn’t too laborious, in this simple case, I’d like to know what `view()` does differently than `getindex()` to be faster and not allocate.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [October 15, 2022, 2:54pm UTC](https://discourse.julialang.org/t/whats-behind-the-scenes-of-the-view-function/88780/2 "2022-10-15T14:54:58Z")

</div>

You can see the difference between `getindex` and `view` when mutating an element of `b`:

```julia
julia> a = [1, 2, 3];

julia> b = a[1:2]
2-element Vector{Int64}:
 1
 2

julia> c = view(a, 1:2)
2-element view(::Vector{Int64}, 1:2) with eltype Int64:
 1
 2

julia> b[1] = 4
4

julia> a # Data in a is unchanged
3-element Vector{Int64}:
 1
 2
 3

julia> c[1] = 4
4

julia> a # Data in a has changed as well
3-element Vector{Int64}:
 4
 2
 3

```

Thus, `getindex` will take the values from the slice and copy it into a new array. In contrast, `view` just wraps the original array and indexes into it. Thereby, `getindex` is safer to use, i.e., as it cannot change the data it has been taken from, whereas `view` is faster as it does not need to copy.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [October 15, 2022, 3:01pm UTC](https://discourse.julialang.org/t/whats-behind-the-scenes-of-the-view-function/88780/3 "2022-10-15T15:01:57Z")

</div>

`view` wraps the array in in a `SubArray`:

```julia
julia> A = rand(3, 4)
3×4 Matrix{Float64}:
 0.479355 0.829413 0.69798 0.614651
 0.767674 0.997691 0.378664 0.77445
 0.0120585 0.260765 0.696942 0.819525

julia> V = view(A, 1:2, :)
2×4 view(::Matrix{Float64}, 1:2, :) with eltype Float64:
 0.479355 0.829413 0.69798 0.614651
 0.767674 0.997691 0.378664 0.77445

julia> typeof(V)
SubArray{Float64, 2, Matrix{Float64}, Tuple{UnitRange{Int64}, Base.Slice{Base.OneTo{Int64}}}, false}

```

You can find the parent array and the indices in the `SubArray` fields:

```julia
julia> V.indices
(1:2, Base.Slice(Base.OneTo(4)))

julia> V.parent
3×4 Matrix{Float64}:
 0.479355 0.829413 0.69798 0.614651
 0.767674 0.997691 0.378664 0.77445
 0.0120585 0.260765 0.696942 0.819525

```

---

<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:** [October 15, 2022, 7:31pm UTC](https://discourse.julialang.org/t/whats-behind-the-scenes-of-the-view-function/88780/4 "2022-10-15T19:31:07Z")

</div>

> [@rocco\_sprmnt21](#):
>
> If the explanation isn’t too laborious, in this simple case, I’d like to know what `view()` does differently than `getindex()` to be faster and not allocate.

Read the manual on what a view is: [Arrays · The Julia Language](https://docs.julialang.org/en/v1/base/arrays/#Views-(SubArrays-and-other-view-types))

See also [Could you explain what are views?](https://discourse.julialang.org/t/could-you-explain-what-are-views/17535)

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 16, 2022, 5:38am UTC](https://discourse.julialang.org/t/whats-behind-the-scenes-of-the-view-function/88780/5 "2022-10-16T05:38:55Z")

</div>

I had an unclear idea that this was the main difference. This is why I tried to make tests similar to those you illustrated, but, for some mess I made, I found the same result in both cases …
