# How to return a non-allocating view from a function?

**URL:** <https://discourse.julialang.org/t/how-to-return-a-non-allocating-view-from-a-function/111946>\
**Category:** Performance\
**Created:** [March 21, 2024, 7:14pm UTC](https://discourse.julialang.org/t/how-to-return-a-non-allocating-view-from-a-function/111946 "2024-03-21T19:14:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [March 21, 2024, 7:14pm UTC](https://discourse.julialang.org/t/how-to-return-a-non-allocating-view-from-a-function/111946/1 "2024-03-21T19:14:50Z")

</div>

Hello!

I have a function in which I do a small amount of operations, before returning a view of the final result as such:

```julia
# some operations ..
    ParticleRanges = view(ParticleSplitterLinearIndices, ParticleSplitter)
    UniqueCells = view(Cells,ParticleRanges[1:end-1])

    return ParticleRanges, UniqueCells 
end

```

I am not sure why this allocates? Is there a way to avoid such allocations, but still being able to return these variables referring to this kind of view?

I am pretty sure I can avoid allocations completely by using for example `ParticleSplitterLinearIndices[ParticleSplitter]` directly in the next function, but there is some very nice code coherence by returning the view explictly.

Kind regards

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [March 21, 2024, 7:21pm UTC](https://discourse.julialang.org/t/how-to-return-a-non-allocating-view-from-a-function/111946/2 "2024-03-21T19:21:50Z")

</div>

It’s a little hard to know without knowing more about the what is going on in your code, but my first guess would be that the call `ParticleRanges[1:end-1]` allocates. Can you try `view(ParticleRanges,1:lastindex(ParticleRanges)-1)` instead?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [March 21, 2024, 7:33pm UTC](https://discourse.julialang.org/t/how-to-return-a-non-allocating-view-from-a-function/111946/3 "2024-03-21T19:33:58Z")

</div>

Hi!

I gave it a try but unfortunately this would increase allocations in general. `ParticleSplitter` is a vector of `Bool`s, with true or false values. In combination with `ParticleSplitterLinearIndices = LinearIndices(ParticleSplitter)` it is used to construct the `ParticleRanges` variable. `ParticleRanges` is then used to index into the `UniqueCells` and construct this object.

Perhaps the code is just a bit too complex and I need to find another way, I hoped I could just get away with a view somehow…

Kind regards

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [March 21, 2024, 8:11pm UTC](https://discourse.julialang.org/t/how-to-return-a-non-allocating-view-from-a-function/111946/4 "2024-03-21T20:11:39Z")

</div>

> [@Ahmed\_Salih](#):
>
> I am not sure why this allocates?

Because you construct a copy of `ParticleRanges`

> [@Ahmed\_Salih](#):
>
> ```julia
> ParticleRanges[1:end-1]
> 
> ```

I’d try

```julia
UniqueCells = @views Cells[ParticleRanges[1:end-1]]

```

Maybe that still allocates. I don’t know how well a view works in this specific case.
