# Why does \`./=\` allocate?

**URL:** <https://discourse.julialang.org/t/why-does-allocate/104987>\
**Category:** General Usage\
**Created:** [October 15, 2023, 8:08pm UTC](https://discourse.julialang.org/t/why-does-allocate/104987 "2023-10-15T20:08:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 15, 2023, 8:08pm UTC](https://discourse.julialang.org/t/why-does-allocate/104987/1 "2023-10-15T20:08:48Z")

</div>

I have this bit of code. I managed to avoid all allocations.

```julia
function compute!(csmatout, XYZ, tangents, feid, qpid)
        center = (0.0, 0.0, 0.0)
        xyz = (XYZ[1], XYZ[2], XYZ[3])
        csmatout[:, 1] .= xyz .- center
        csmatout[3, 1] = 0.0
        # csmatout[:, 1] ./= norm(@view(csmatout[:, 1]))
        nm = norm(@view(csmatout[:, 1]))
        for k in 1:3; csmatout[k, 1] /= nm; end
        csmatout[:, 3] .= (0.0, 0.0, 1.0)
        cross3!(@view(csmatout[:, 2]), @view(csmatout[:, 3]), @view(csmatout[:, 1]))
        # csmatout[:, 2] ./= norm(@view(csmatout[:, 2]))
        nm = norm(@view(csmatout[:, 2]))
        for k in 1:3; csmatout[k, 2] /= nm; end
        return csmatout
end

```

But, I had to replace the lines that are commented out. They would allocate. In other words, this allocates.

```julia
function compute!(csmatout, XYZ, tangents, feid, qpid)
        center = (0.0, 0.0, 0.0)
        xyz = (XYZ[1], XYZ[2], XYZ[3])
        csmatout[:, 1] .= xyz .- center
        csmatout[3, 1] = 0.0
        csmatout[:, 1] ./= norm(@view(csmatout[:, 1]))
            # nm = norm(@view(csmatout[:, 1]))
            # for k in 1:3; csmatout[k, 1] /= nm; end
        csmatout[:, 3] .= (0.0, 0.0, 1.0)
        cross3!(@view(csmatout[:, 2]), @view(csmatout[:, 3]), @view(csmatout[:, 1]))
        csmatout[:, 2] ./= norm(@view(csmatout[:, 2]))
            # nm = norm(@view(csmatout[:, 2]))
            # for k in 1:3; csmatout[k, 2] /= nm; end
        return csmatout 
end

```

I thought that the compiler would be able to divide the column without allocating. What is it that I’m misunderstanding here?

Thanks!

---

<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, 2023, 8:17pm UTC](https://discourse.julialang.org/t/why-does-allocate/104987/2 "2023-10-15T20:17:58Z")

</div>

> [@PetrKryslUCSD](#):
>
> What is it that I’m misunderstanding here?

`csmatout[:, 1] ./= nm` is equivalent to

```julia
csmatout[:, 1] .= csmatout[:, 1] ./ nm

```

so the `csmatout[:, 1]` on the right-hand side allocates. This can be fixed by putting `@views` before the lines, but it will be a lot simpler to just to put `@views` on the whole function and get rid of all the `@view` calls.

i.e. something like:

```julia
@views function compute!(csmatout, XYZ, tangents, feid, qpid)
        center = (0.0, 0.0, 0.0)
        xyz = (XYZ[1], XYZ[2], XYZ[3])
        csmatout[:, 1] .= xyz .- center
        csmatout[3, 1] = 0.0
        csmatout[:, 1] ./= norm(csmatout[:, 1])
        csmatout[:, 3] .= (0.0, 0.0, 1.0)
        cross3!(csmatout[:, 2], csmatout[:, 3], csmatout[:, 1])
        csmatout[:, 2] ./= norm(csmatout[:, 2])
        return csmatout
end

```

Isn’t it more readable without all of those `@view` calls?

PS. If you are working with lots of 3-component vectors representing XYZ coordinates (so that you know their length at compile time), I would strongly consider arrays of `SVector` from StaticArrays.jl instead.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 15, 2023, 8:20pm UTC](https://discourse.julialang.org/t/why-does-allocate/104987/3 "2023-10-15T20:20:55Z")

</div>

Ah, I forgot I should use `@view` for the lhs too.  
But `@views` sure is so much nicer!  
Ta!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 15, 2023, 10:30pm UTC](https://discourse.julialang.org/t/why-does-allocate/104987/4 "2023-10-15T22:30:58Z")

</div>

> [@stevengj](#):
>
> `csmatout[:, 1] ./= nm` is equivalent to
> 
> ```julia
> csmatout[:, 1] .= csmatout[:, 1] ./ nm
> 
> ```

This always gets me and I guess I’m not alone. I also forgot why we can’t make

```julia
ary[n:m] ./= 3

```

lowers to equivalent to having a view.

Actually, now I remember, THIS REALLY ALWAYS gets me:

```julia
ary1[n:m] .= @view ary2[n:m]

```

is enough but not for `ary1[n:m] .*=`
