# Allocation when assigning to a slice

**URL:** <https://discourse.julialang.org/t/allocation-when-assigning-to-a-slice/73381>\
**Category:** General Usage\
**Tags:** memory-allocation\
**Created:** [December 20, 2021, 4:30pm UTC](https://discourse.julialang.org/t/allocation-when-assigning-to-a-slice/73381 "2021-12-20T16:30:59Z")\
**Posts on this page:** 1\
**Showing post:** 3

<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:** [December 20, 2021, 4:37pm UTC](https://discourse.julialang.org/t/allocation-when-assigning-to-a-slice/73381/3 "2021-12-20T16:37:36Z")

</div>

> [@lmiq](#):
>
> (although I think that shouldn’t be necessary)

It’s totally necessary, because:

```julia
z[56:end] .-= 1

```

is equivalent to

```julia
z[56:end] .= z[56:end] .- 1

```

and the slice `z[56:end]` on the right-hand side [allocates a copy](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-views).

I also find it a lot clearer in most cases to just put `@views` on the whole function rather than on individual lines.

```julia
@views function test4(z)
    z[56:end] .-= 1
end

```

giving

```julia
julia> @btime test4($z);
  1.681 μs (0 allocations: 0 bytes)

```

---

_[View the full topic](https://discourse.julialang.org/t/allocation-when-assigning-to-a-slice/73381)._
