# Allocation when applying in place multiplication to matrix of SVector

**URL:** https://discourse.julialang.org/t/allocation-when-applying-in-place-multiplication-to-matrix-of-svector/96074
**Category:** Performance
**Tags:** question
**Created:** [March 14, 2023, 6:02pm UTC](https://discourse.julialang.org/t/allocation-when-applying-in-place-multiplication-to-matrix-of-svector/96074 "2023-03-14T18:02:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![yiminlin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiminlin/32/14676_2.png) [@yiminlin](https://discourse.julialang.org/u/yiminlin)
#### Post date: [March 14, 2023, 6:02pm UTC](https://discourse.julialang.org/t/allocation-when-applying-in-place-multiplication-to-matrix-of-svector/96074/1 "2023-03-14T18:02:40Z")

</div>

I am trying to apply in-place matrix multiplication to a matrix of SVector. However, there are 3 allocations if I apply `mul!` directly. If I apply the multiplication over each column, the allocations disappear. Here is the example:

```julia
A = zeros(SVector{3,Float64},10,100)
B = zeros(SVector{3,Float64},100,100)
T = ones(100,10)
function test(B,T,A)
    for j = 1:100
        @views mul!(B[:,j],T,A[:,j])
    end
end
# B = TA
@btime mul!($B,$T,$A)
@btime test($B,$T,$A)
 90.117 μs (3 allocations: 24.62 KiB)
 46.061 μs (0 allocations: 0 bytes)

```

For reference, if I use regular matrices, there is no allocation:

```julia
A = zeros(Float64,10,100)
B = zeros(Float64,100,100)
T = ones(100,10)
function test(B,T,A)
    for j = 1:100
        @views mul!(B[:,j],T,A[:,j])
    end
end
# B = TA
@btime mul!($B,$T,$A)
@btime test($B,$T,$A)
  3.650 μs (0 allocations: 0 bytes)
  9.995 μs (0 allocations: 0 bytes)

```

Do someone knows why this happens? Thanks in advance!

---

<div class="post-metadata">

### Author: ![Liozou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liozou/32/35118_2.png) [@Liozou](https://discourse.julialang.org/u/Liozou)
#### Post date: [March 15, 2023, 1:49pm UTC](https://discourse.julialang.org/t/allocation-when-applying-in-place-multiplication-to-matrix-of-svector/96074/2 "2023-03-15T13:49:15Z")

</div>

Profiling shows that the allocations happen here, if you want to inquire how the internals of that function work:

> <https://github.com/JuliaLang/julia/blob/7ba7e326293bd3eddede81567bbe98078e81f775/stdlib/LinearAlgebra/src/matmul.jl#L873-L874>

and

> <https://github.com/JuliaLang/julia/blob/7ba7e326293bd3eddede81567bbe98078e81f775/stdlib/LinearAlgebra/src/matmul.jl#L894-L894>

I don’t know much about the LinearAlgebra code so I don’t think I can give you more pointers than that unfortunately!

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 15, 2023, 2:51pm UTC](https://discourse.julialang.org/t/allocation-when-applying-in-place-multiplication-to-matrix-of-svector/96074/3 "2023-03-15T14:51:56Z")

</div>

I don’t think that `mul!` guarantees that the multiplication is non-allocating, but only that the output is updated in place.
