# Inline macro

**URL:** <https://discourse.julialang.org/t/inline-macro/68595>\
**Category:** Performance\
**Tags:** question\
**Created:** [September 22, 2021, 5:18pm UTC](https://discourse.julialang.org/t/inline-macro/68595 "2021-09-22T17:18:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliascarv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliascarv/32/22152_2.png) [@eliascarv](https://discourse.julialang.org/u/eliascarv)\
**Post date:** [September 22, 2021, 5:18pm UTC](https://discourse.julialang.org/t/inline-macro/68595/1 "2021-09-22T17:18:09Z")

</div>

When should I use the `@inline` macro to optimize performance?

---

<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:** [September 22, 2021, 5:35pm UTC](https://discourse.julialang.org/t/inline-macro/68595/2 "2021-09-22T17:35:55Z")

</div>

TLDR: for short functions where it will be faster if the compiler to “copy-paste” the definition of the function into it’s caller instead of making a function call. That said, the compiler generally does a pretty good job of doing this on its own, so I wouldn’t worry about it much if you are relatively new to Julia. It is generally a fairly minor factor.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [September 22, 2021, 6:18pm UTC](https://discourse.julialang.org/t/inline-macro/68595/3 "2021-09-22T18:18:57Z")

</div>

Off topic, I’m curious about how one can define an inline function _without using any macro_? Following the result of `@macroexpand(@inline f(x) = x)`, I came up with

```julia
eval(quote
   f(x) = begin
       $(Expr(:meta, :inline))
       x
   end
end)

```

But this looks crazy. Is there a better way?

---

<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:** [September 22, 2021, 6:20pm UTC](https://discourse.julialang.org/t/inline-macro/68595/4 "2021-09-22T18:20:40Z")

</div>

This one is easy. Just

```julia
eval(quote
   @inline f(x) = begin
       x
   end
end)

```

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [September 22, 2021, 6:53pm UTC](https://discourse.julialang.org/t/inline-macro/68595/5 "2021-09-22T18:53:19Z")

</div>

Can you do it without using any `@` character in the code? That was what I wanted to find out (again, just for curiosity).

---

<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:** [September 22, 2021, 6:54pm UTC](https://discourse.julialang.org/t/inline-macro/68595/6 "2021-09-22T18:54:29Z")

</div>

I don’t think so, but also why?

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [September 22, 2021, 7:00pm UTC](https://discourse.julialang.org/t/inline-macro/68595/7 "2021-09-22T19:00:49Z")

</div>

Actually I’m just trying to learn the basics of macros, by reading the expanded code of all sorts of built-in macros like `@inline` and `@enum`. I’m a bit surprised that there are things like `Expr(:meta, ...)` and `Exp(:toplevel, ...)`, which cannot be easily written down as “normal” Julia code. Macros written down by a beginner like me can always be replaced by normal-looking explicit code, so I’m trying to understand why the built-in macros have more magic in them.

---

<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:** [September 22, 2021, 7:11pm UTC](https://discourse.julialang.org/t/inline-macro/68595/8 "2021-09-22T19:11:35Z")

</div>

That makes sense. The basic answer is that `Base` needs some functionality that requires custom support from the language. In these cases, the typical approach is to add something to Julia’s AST syntax, and make a macro that inserts that type of AST node as a fron-end to the compiler internals.

In effect, the causality is backwards. It’s not that `Base` implements lots of built-in macros, it’s that if you want a built-in macro, it has to live in Base.

Also, `@enum` isn’t special. You could implement it yourself if you wanted, it’s just a little complicated.
