# Inplace multiplication of sub-matrices without allocations

**URL:** https://discourse.julialang.org/t/inplace-multiplication-of-sub-matrices-without-allocations/91534
**Category:** Performance
**Tags:** performance, linear-algebra, allocations
**Created:** [December 11, 2022, 9:23pm UTC](https://discourse.julialang.org/t/inplace-multiplication-of-sub-matrices-without-allocations/91534 "2022-12-11T21:23:18Z")
**Posts on this page:** 1
**Showing post:** 9

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [December 11, 2022, 10:19pm UTC](https://discourse.julialang.org/t/inplace-multiplication-of-sub-matrices-without-allocations/91534/9 "2022-12-11T22:19:23Z")

</div>

Out of curiosity, why do we use `view(A, :, :, dim)` instead of `View(A)[:, :, dim]`? It seems like much of the motivation for the `@view` macro is to allow the `begin` and `end` keywords which are valid in `[]` square brackets, but if we simply had an indexable type we get those back.

Playing around, it seems like this should be doable:

```julia
struct View{A<:AbstractArray} a::A end
Base.getindex(v::View, args...) = view(v.a, args...)
Base.axes(v::View, args...) = axes(v.a, args...)
View(A)[:, :, end] # this just works

```

Applying it here:

```julia
julia> function foo6!(A,B,C,dim)
           mul!(View(C)[:,:,dim], View(A)[:,:,dim], B)
           nothing
       end
foo6! (generic function with 1 method)

julia> @btime foo6!(A,B,C,1)
  330.808 ns (0 allocations: 0 bytes)

```

---

_[View the full topic](https://discourse.julialang.org/t/inplace-multiplication-of-sub-matrices-without-allocations/91534)._
