# When does macro expansion take place/use macro inside hot loop?

**URL:** https://discourse.julialang.org/t/when-does-macro-expansion-take-place-use-macro-inside-hot-loop/40586
**Category:** New to Julia
**Created:** [June 1, 2020, 10:20pm UTC](https://discourse.julialang.org/t/when-does-macro-expansion-take-place-use-macro-inside-hot-loop/40586 "2020-06-01T22:20:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)
#### Post date: [June 1, 2020, 10:20pm UTC](https://discourse.julialang.org/t/when-does-macro-expansion-take-place-use-macro-inside-hot-loop/40586/1 "2020-06-01T22:20:46Z")

</div>

I have a function inside a nested loop, and I’m cleaning it up with `@unpack` from UnPack.jl for about 20 parametrs/pre-allocated matrix:

```julia
@unpack a,b,c = p #parameters...
@unpack m1,m2,m3 = p #pre-allocated matrices for things like mul!()

```

`@code_llvm` suggests that the hand-written version (`a=p.a`, `b=p.b`, etc) generates more or less the same code as the cleaner `@unpack` version, with only a few comments such as

```julia
; ┌ @ /Users/user/.julia/packages/UnPack/1IjJI/src/UnPack.jl:101 within `macro expansion'
; │┌ @ /Users/user/.julia/packages/UnPack/1IjJI/src/UnPack.jl:34 within `unpack'

```

testing with `@benchmark` suggests there’s very little performance difference. Just to understand the inner workings, is having a macro equivalent to the expanded version from the compiler’s point of view, and there would be no performance penalties to use macros inside the nested/hot loop? Thanks!

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [June 1, 2020, 10:23pm UTC](https://discourse.julialang.org/t/when-does-macro-expansion-take-place-use-macro-inside-hot-loop/40586/2 "2020-06-01T22:23:43Z")

</div>

[JuliaCon 2017 | AoT or JIT: How Does Julia Work? | Jameson Nash - YouTube](https://www.youtube.com/watch?v=7KGZ_9D_DbI) is very useful for understanding the Julia compiler in general. The specific answer is that macros operate on code\_lowered, which happen before the rest of the compiler. As such, macros have 0 performance cost compared to the code they generate.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 2, 2020, 6:33am UTC](https://discourse.julialang.org/t/when-does-macro-expansion-take-place-use-macro-inside-hot-loop/40586/3 "2020-06-02T06:33:15Z")

</div>

A slight correction: Macros don’t actually work on the lowered representation but on the abstract syntax tree right after code is parsed and lowering only happens afterwards. What this means is that macros always just work on the raw syntax before any expansion of e.g. dot broadcasting to function calls happen. There is currently no way to retrieve the AST of the source code of functions, but you can inspect the AST of blocks of code with `Meta.@dump`.
