# Memory Allocation when using mul! with sparse arrays and views

**URL:** <https://discourse.julialang.org/t/memory-allocation-when-using-mul-with-sparse-arrays-and-views/111276>\
**Category:** Performance\
**Tags:** blas, linearalgebra, memory-allocation, sparsearrays\
**Created:** [March 7, 2024, 4:45am UTC](https://discourse.julialang.org/t/memory-allocation-when-using-mul-with-sparse-arrays-and-views/111276 "2024-03-07T04:45:56Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [March 7, 2024, 7:33am UTC](https://discourse.julialang.org/t/memory-allocation-when-using-mul-with-sparse-arrays-and-views/111276/2 "2024-03-07T07:33:59Z")

</div>

I think this might be the same as

> [@Why does \`mul!(u, A, v)\` allocate when \`A\` is sparse and \`u, v\` are views?](https://discourse.julialang.org/t/why-does-mul-u-a-v-allocate-when-a-is-sparse-and-u-v-are-views/105995):
>
> I’m wondering if there is a way to avoid allocations in the following scenario: julia\> using BenchmarkTools, LinearAlgebra, SparseArrays julia\> A = sprand(2, 3, 0.5); julia\> u, v = rand(2), rand(3); julia\> @btime mul!($u, $A, $v); 21.139 ns (0 allocations: 0 bytes) julia\> u, v = view(rand(2, 3), :, 1), view(rand(2, 3), 1, :); julia\> @btime mul!($u, $A, $v); 33.731 ns (1 allocation: 48 bytes) If it helps, the size of the allocation does not increase with the size of the arrays

However, note that to benchmark functions properly, you need to use BenchmarkTools.jl and [interpolate variables](https://juliaci.github.io/BenchmarkTools.jl/stable/manual/#Interpolating-values-into-benchmark-expressions) with a dollar sign. Otherwise your allocation count will be biased by the presence of global variables.

---

_[View the full topic](https://discourse.julialang.org/t/memory-allocation-when-using-mul-with-sparse-arrays-and-views/111276)._
