# Performance when chaining multiple macros

**URL:** <https://discourse.julialang.org/t/performance-when-chaining-multiple-macros/73173>\
**Category:** Performance\
**Tags:** question, performance, macros\
**Created:** [December 15, 2021, 11:29pm UTC](https://discourse.julialang.org/t/performance-when-chaining-multiple-macros/73173 "2021-12-15T23:29:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [December 15, 2021, 11:29pm UTC](https://discourse.julialang.org/t/performance-when-chaining-multiple-macros/73173/1 "2021-12-15T23:29:31Z")

</div>

I’m aware that macros such as `@muladd` from `MuladdMacro.jl`, `@views`, and `@inbounds` are occasionally very useful for performance. **Might there be any problems with combining them in general?** For example, is the following chaining of macros valid?

```julia
using MuladdMacro
A = rand(100, 100)
B = rand(100, 100)
C = rand(100, 1)
@muladd @views @inbounds @. for j = 1:100
    A[:, j] = C * C + B[:, j] * A[:, j] # Write C^2 = C * C so that @muladd works 
end 

```

Or are there limitations on how far we can chain these individual macros for performance without seeing negative effects?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 16, 2021, 12:15am UTC](https://discourse.julialang.org/t/performance-when-chaining-multiple-macros/73173/2 "2021-12-16T00:15:37Z")

</div>

Macros transform code, and there’s no way to guarantee that a series of transformations will be useful (or even valid). You can check with `@macroexpand`, though:

```julia
julia> @macroexpand @muladd @views @inbounds @. for j = 1:100
           A[:, j] = C * C + B[:, j] * A[:, j] # Write C^2 = C * C so that @muladd works
       end
quote
    $(Expr(:inbounds, true))
    local var"#2016#val" = for j = 1:100
                #= REPL[106]:2 =#
                A[:, j] .= (muladd).((Base.maybeview)(B, :, j), (Base.maybeview)(A, :, j), (*).(C, C))
            end
    $(Expr(:inbounds, :pop))
    var"#2016#val"
end

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 16, 2021, 12:22am UTC](https://discourse.julialang.org/t/performance-when-chaining-multiple-macros/73173/3 "2021-12-16T00:22:42Z")

</div>

On the other hand, they only impose a performance loss at compilation time. So, if they are used in the body of a function called often with the same parameter types, especially to speed-up loops, then the performance loss should be minimal in comparison to their gains.
